Order Books

Jose Portilla
A free video tutorial from Jose Portilla
Head of Data Science, Pierian Data Inc.
4.6 instructor rating • 31 courses • 2,155,357 students

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:27 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. Let's go ahead and go through the process. The first thing you end up doing is when you click that button an order gets created and an order should include the following information it needs to include buyers cell 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 stocks you want to actually buy or sell. And that is the actual symbol for example for Apple stock. You would put 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 put 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 Simbel 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 buy Apple 200 shares at the market price or sell Tesla for 100 shares at the market price or buy HMD 2000 shares but keep the limit. You're not willing to pay more than $13 and 95 cents or maybe you want to sell Nvidia's stock 150 shares. However you're not going to sell below $160 an 89 cents. So once you sent out an order that's usually to your broker what ends up happening is 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 that 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 in order book we're going to pretend that we have the order book for the New York Stock Exchange specifically for A B A C that is the Bank of America stock So A B C 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 that goes into this biase order book and they want to buy 200 shares. And the limit of their price they're willing to pay is one hundred ninety nine dollars and 95 cents. So right now there's 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 at $199 in 90 cents. Then it comes in another sell order make America 50 shares limit $199 91 cents. That another one hundred ninety nine. Ninety two cents. Again Bank of America 50 stock. So what actually ends up happening here is the person that sent that buy order for 200 shares with the bid price of $189 and ninety five cents is going to end up getting their shares from these three different people. They're first going to get 100 shares at $199 and 90 cents then the next 50 shares are going to come in and they're going to pay 1 9 9 and 91 cents for those in their last 50 shares to make up their total order of 200. It's going to end up being $199 and 92 cents for those 50 shares. So you can see they paid different prices for different portions of their shares. However all those prices ended up meeting their bid price of $189 in 95 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 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 in the ask price and you have the ID 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 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 this OK so let's take a look at the simplest example possible and this kind of the classic example of what would happen. 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 Web site and you say hey I want to buy let's say ten 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 stock's back depending if you're buying or selling. In this case since you're buying. To give you the stocks and the broker then take some sort of fee for doing that whole process for you. Now scenario number two which is kind of a broker buy 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 a 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 it all gets matched up inside of the broker. So this could all happen within the broker or ever actually reach in 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 in the exchange the broker must have provided that to you. If they do the interaction of 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 particular stock to your broker and another person is selling that same stock to a different broker broken number two. Could end up happening is that both those orders go to a dark pool first and never actually reached 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 is just a private exchange or a 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 we can discuss in a little bit. That's high frequency trading. But keep in mind that dark pools have 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 that's going to cause a big jump or movement you don't really want to do that at the exchange. Otherwise people get wind of it and they will either Jack up their price when they're selling the stock because we want to buy a million of them or the price will plummet because you're selling a bunch of stock you're offloading it all. So what the dark pool can do because of its lack of transparency to the exchanges is the entire interaction going to happen within that dark pool. So you end up affecting the stock price a whole lot. Somebody 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 reach 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. So this is all happening extremely fast and discussion about HFT as were really popularized by Michael Lewis his 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 investors Exchange. So go ahead and read that book if you kind of want a little bit of background behind the shift. He's 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 we're 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 I 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 HFT is concerned. It doesn't really affect people that are operating on the scale we are if you're just selling a couple 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 ninety thousand shares or something and you're going to buy 30 thousand shares from Bhatt's that's another exchange that as that one exchange. And then your stock exchange. You want to buy 13000 shares because the order is large enough that it probably needs to go to multiple exchanges so it ends up happening is it takes about 2 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. 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 300 milliseconds. So physically two milliseconds is extremely fast. However what happens is high frequency trading firms they bill were 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 placed in 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 a bank or institution send out your order. And they say OK that by 30 shares at the bats exchange and it's part of this large 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 bats 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 straight a fiber optic cables 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 476 microseconds. So they end up doing is they realize you're making a larger order and they beat you to the other exchanges quickly buy up and kind of raise the price just by a sense it's almost nothing 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 breach and 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 are people moving around larger sums of money or larger amounts of shares for us. It really doesn't affect this. So again that was a very simplified overview and you can check out the resource links. If this is a topic that interests you HFT 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 this in any way with what we're going to be end up coding out or how are you working with finance of Python. But all those ideas do exist in the marketplace. We 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.