Order Books

A free video tutorial from Jose Portilla
Head of Data Science at Pierian Training
Rating: 4.6 out of 5Instructor rating
59 courses
3,220,369 students
Order Books

Learn more from the full course

Python for Financial Analysis and Algorithmic Trading

Learn numpy , pandas , matplotlib , quantopian , finance , and more for algorithmic trading with Python!

16:35:31 of on-demand video • Updated December 2020

Use NumPy to quickly work with Numerical Data
Use Pandas for Analyze and Visualize Data
Use Matplotlib to create custom plots
Learn how to use statsmodels for Time Series Analysis
Calculate Financial Statistics, such as Daily Returns, Cumulative Returns, Volatility, etc..
Use Exponentially Weighted Moving Averages
Use ARIMA models on Time Series Data
Calculate the Sharpe Ratio
Optimize Portfolio Allocations
Understand the Capital Asset Pricing Model
Learn about the Efficient Market Hypothesis
Conduct algorithmic Trading on Quantopian
English [Auto]
Hello, everyone, and welcome to this lecture on order books. So let's say you've decided to either buy or sell a stock, you end up logging on to your brokerage account like Robin Hood on your phone or E-Trade, Ameritrade, etc., then you click on the stock, either want to buy or sell, and then you either pay or receive money. But what is actually happening when you click that buy or sell button? Lets go ahead and go through the process. The first thing you end up doing is when you click that button in order gets created and an order should include the following information, it needs to include buy or sell, symbol, number of shares, limit or market, and then the price. So let's actually discuss each of these. First off, you need to indicate whether you want to buy the stock or sell the stock. The next thing is to actually say what stock you want to actually buy or sell, and that is the actual symbol, for example, for Apple stock, you would input AAPL, then the next piece of information that's needed is the number of shares. How many shares of Apple do you actually want to buy or sell? The next piece of information that's needed is limit or market. So what does that actually mean? Well, if you have a limit order, then you end up also passing in a price, and that is the price limit that you're either going to buy or sell out. So let's imagine that you're going to buy a stock and you input a limit with that. That basically indicates that you're willing to buy a piece of apple in this case or whatever stock you want to buy a certain number of shares of a stock, but you're only willing to pay up to a limit. So that is the limit price that you're willing to pay up to. Now for a sell order, it's essentially the same thing, except the limit is how low you're willing to go to sell it. So you end up saying buy or sell symbol, number of shares, limit and then price. Now, typically, you're just going to do what's known as a market order. And in that case, there's no price needed. You're just going to either buy or sell at whatever the current market will accept. So some example orders may look like this, you could say by Apple, 200 shares at the market price or sell Tesla. Four hundred shares at the market price or by AMD 2000 shares. But keep the limit. You're not willing to pay more than 13 dollars and 95 cents. Or maybe you want to sell Invidia stock 150 shares. However, you're not going to sell below 160 dollars and 99 cents. So once you sent out an order that's usually to your broker, what ends up happening is it actually goes to an exchange and a for a larger order, it can go to multiple exchanges. But we're going to discuss more on that later for our scale. It typically is all handled even within a broker or to an exchange or something called a dark pool, which again, we'll talk about later. But once an exchange actually receives your order, it goes into an order book. So every exchange has their own order book. All right. So let's actually build out an order book. We're going to pretend that we have the order book for the New York Stock Exchange, specifically for baozi, that is the Bank of America stock. So bank is the ticker and we have our order book. The New York Stock Exchange just opened and we're going to get our first buy order. So the very first order that comes in is a buy order for Bank of America stock. So it goes into the back order book and they want to buy 200 shares. And the limit of their price that they're willing to pay is one hundred ninety nine dollars and ninety five cents. So right now, there is no one selling. It's still the very first order that came into the market. And we need to wait till someone comes in and they want to sell that stock. So, boom, we get our first sell order and this person selling Bank of America 100 shares and the limit they're willing to sell is one hundred ninety nine dollars and 90 cents. Then it comes in another sell order, Bank of America, 50 shares limit one hundred ninety nine dollars, 91 cents, then another one hundred eighty nine. Ninety two cents again. Bank of America, 50 stock. So it actually ends up happening. Here is the person that sent that buy order for 200 shares with the bid price of one hundred ninety nine dollars and ninety five cents is going to end up getting their shares from these three different people. They're first going to get a hundred shares at one hundred and eighty nine dollars and 90 cents. Then the next 50 shares are going to come in and they're going to pay one ninety nine and ninety one cents for those in their last 50 shares to make up. Their total order of 200 is going to end up being one hundred ninety nine dollars and 92 cents for those 50 shares. So you can see they paid different prices for different portions of their shares. However, all of those prices ended up meeting their bid price of one hundred ninety nine dollars and ninety five cents. So that's the very basics of how a very simple order book would work. Now, keep in mind, for a very popular stock, there's hundreds, if not thousands of orders coming in all the time. So this would happen very, very quickly. So what are the real order book actually look like? Well, usually you have to pay for a particular service to see the book. But here we can get kind of an idea for some Nasdaq book viewer. And there you can see an example. You see the bid price and the ask price and you have the bid basically telling you the marketplace provider here. And you can see the different exchanges, Nasdaq, et cetera. And they're kind of lining up by not just color, but also by the bid and ask price in the center. So on your left hand side, you can see the buy orders for Google or Alphabet. And then on the right hand side, you can see the sell orders and how they're matching up. So that's essentially what a book looks like in real life. All right, so the question now arises, how does an order actually get to the exchange and this is actually now a little more physical. So we're going to do now is walk through the steps and we're also going to explain how something called HFT or high frequency trading firms actually works. And what they end up doing is they attempt something called latency arbitrage. So let's talk about all of this. OK, so let's take a look at the simplest example possible in this kind of the classic example of what would happen if it's not very common that it happens this way. But this kind of classically the idea of the stock exchange and how it's connected to you. So for a simple example of you wanting to issue a buy order, you end up contacting your broker. And this is usually done through an app or a website and you say, hey, I want to buy, let's say, 10 units of Apple stock. Your broker then gets in that order that we just discussed earlier, and then they send it to one of the exchanges and it goes into their order book. You get matched up for a person selling stock and then it comes back to your broker and then they give you the money back or the stocks back, depending if you're buying or selling in this case and you're buying the give you the stocks and the broker then takes some sort of fee for doing that whole process for you. Now, scenario number two, which is kind of a broker sell, is a little more realistic of what's going to happen now, your broker doesn't just have you as their client, they may have other clients. And what happens all the time is that you want to buy stock and your broker has another client who wants to sell the same stock. So what they end up doing is the entire process we discussed earlier, except they all gets matched up inside of the broker. So this can all happen within the broker without ever actually reaching the exchange. However, keep in mind, by law, at least in the United States, the broker must provide the same price you could have gotten on the exchange. So whatever the best prices and the exchange, the broker must have provided that to you if they do the interaction all within the broker. Let's take a look at a third example that involves what's known as a dark pool. Let's imagine that you send a buy order for a particular stock to your broker and another person is selling that same stock to a different broker broker. Number two, what could end up happening is that both those orders go to a dark pool first and never actually reach the exchanges. So a dark pool pays brokers to see orders before they hit the exchange. Now, dark pools, it's a really ominous sounding term, but essentially what it is, it's just a private exchange or forum for trading securities. But unlike stock exchanges, dark pools aren't really accessible to the public. They're not transparent. So that's why their name is dark pool, because you can't actually see what's going on inside them unless you're the institution that owns them. Now, because of their lack of transparency, there is some potential conflicts of interest by their owners and there can even be some predatory trading practices using HFT, which are going to discuss in a little bit. That's high frequency trading. But keep in mind that dark pools, they've actually been around since the 1980s. And one of the main purposes they serve is if you're a large institutions such as a bank and you want to sell a million shares of a particular stock or some really large share number of shares, it's going to cause a big jump or movement. You don't really want to do that at the exchange. Otherwise people will get wind of it and they will either jack up their price when they're selling you the stock because you want to buy a million of them or the price will plummet because you're selling a bunch of stock you're offloading at all. So what the dark pool can do because of its lack of transparency to the exchanges is the entire interaction can happen within the dark pool. So you don't end up affecting the stock price a whole lot. Something to keep in mind is when discussing dark pools in 2014, which is only three years ago, about 40 percent of all U.S. stock trades didn't actually ever reach an exchange. And about 15 percent of those trades actually occurred in a dark pool. So dark pools, definitely more and more of these trades are happening inside the dark pools and never actually reaching the exchanges themselves. So keep in mind, moving forward into the future, dark pools are becoming more and more important part of this entire process. And so finally, you may have heard of the term high frequency trading or HFT and basically HFT firms, what they end up doing is they take advantage of latency differences due to geographical distances. And these times of latency are on the order of microseconds. So this is all happening extremely fast. And discussion about HFT were really popularized by Michael Lewis's book called Flash Boys, and that book is about a banker or a trader, Brad Katsuyama, who was working at the Royal Bank of Canada, and he ended up starting the investor's exchange. So go ahead and read that book. If you kind of want a little bit of background behind HFT, how they develop what their purpose was. But it's really more a story about this person, Brad Katsuyama, and how the investors exchange got started. I read it. It was pretty good. I recommend that, though we're going to do is going to briefly explain the basic idea of HFT. But keep in mind, this is an area that changes extremely fast technology and HFT does serve a purpose. And we're going to explain that later on. It's not all bad, but let's go ahead and explain the idea first. So let's imagine that you are a large institution or a bank as far as he's concerned, it doesn't really affect people that are operating on the scale we are. If you're just selling a couple of thousand dollars or even a couple of thousand shares, that's not enough for HFT is actually going to affect you. It will affect a very large institution or very large orders. So if you're a large bank or an institution and you're putting out an order that's large enough that it actually needs multiple exchanges to fulfill the order. So we imagine that you're a bank and you're sending in an order for 90000 shares of something and you're going to buy 30000 shares from bats. That's another exchange that Nasdaq one exchange and then New York Stock Exchange. So you want to buy thirty thousand shares because the order is large enough that it probably needs to go to multiple exchanges. So what ends up happening is it takes about two milliseconds between the order first reaching bats, and that's physically the closest exchange to this bank and then versus reaching the last exchange. That's the New York Stock Exchange, which happens to be the first away. Now, keep in mind, things are moving here at the speed of light. So two milliseconds is a very short time frame. For reference, if you blink your eye, that takes about three hundred milliseconds. So physically, a two milliseconds is extremely fast. However, what happens is high frequency trading firms, they build what are called co-located servers, and that's basically literally placing their servers as close to the order book server as possible. Now, because this HFT firm has built this co-located server, it's actually literally placing their servers as close to the order book server as possible. So they're retrieving that information first. And what ends up happening is you, as the banker institution, send out your order and they say, OK, I'm going to buy 30 shares at the BATS exchange and it's part of this larger order. And you're going to need to buy a 30 K from Nasdaq and 30 K from the New York Stock Exchange. The HFT happens to have a co-located server at that stock exchange and they realize this huge orders coming in. So because of that and due to their technology microwave towers, whatever happens to be a straight a fiber optic cables, et cetera, they have a speed advantage on you. And what they can end up doing is while it takes you two milliseconds to reach the New York Stock Exchange, they can actually beat you to the New York Stock Exchange and they take around four hundred seventy six microseconds. So what they ended up doing is they realize you're making a larger order and they beat you to the other exchanges quickly by up and kind of raise the price. Just by a sense, it's almost nothing but for a larger order, since they know the orders coming in, they're virtually guaranteed to make money here. And by the time your order ends up breaching the other exchanges, the price has jumped up a tiny bit. And that's how an HFT firm can make money. Now, again, this is kind of a very simplified version of this, and it really only affects larger institutions or people moving around larger sums of money or larger amounts of shares. For us, it really doesn't affect us. So, again, that was a very simplified overview and you can check out the resource links, if this is a topic that interests you, HFA in general is not really relevant for our scale or approach to trading. It just happens to exist in the marketplace. So you should be aware of it. All right, I hope you found those topics interesting again, they don't really affect us in any way with what we're going to be end up cutting out or how are they working with finance, with Python. But all those ideas do exist in the marketplace, and you should be aware of them as we continue on through this course. In the next lecture, we're going to discuss short selling and how that actually works. Thanks. And I'll see you there.