Udemy

Order Books

A free video tutorial from Jose Portilla
Head of Data Science at Pierian Training
Rating: 4.6 out of 5Instructor rating
87 courses
4,248,342 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 Robinhood on your phone or E-Trade, Ameritrade. ET cetera. Then you click on the stock you 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 in order gets created and in 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 at. 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 buy Apple 200 shares at the market price or sell Tesla 400 shares at the market price or buy AMD 2000 shares. But keep the limit. You're not willing to pay more than $13.95. Or maybe you want to sell Nvidia stock 150 shares. However, you're not going to sell below $160.99. 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 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 back, that is the Bank of America stock. So a back 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 this back order book and they want to buy 200 shares and the limit of their price that they're willing to pay is $199.95. 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 is $199.90. Then comes in another cell order. Bank of America 50 shares limit $199.91, then another $199.92. 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 $199.95 is going to end up getting their shares from these three different people. They're first going to get 100 shares at $199.90. Then the next 50 shares are going to come in and they're going to pay $199.91 for those. Then their last 50 shares to make up their total order of 200 is going to end up being $199.92 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 $199.95. 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 is 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 through 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. Okay, so let's take a look at the simplest example possible. And this is kind of the classic example of what would happen. It's not very common that it happens this way, but this is kind of classically the idea of the stock exchange and how it's connected to you. So for our 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, 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 with 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, since you're buying to 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 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 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 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 price is on 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 we're 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 institution 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 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 it 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% of all US stock trades didn't actually ever reach an exchange, and about 15% of those trades actually occurred in a dark pool. So dark pool is 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 a 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 Nfts 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 Nfts, how they developed, what their purpose was. But it's really more a story about this person, Brad Katsuyama, and how the investor's exchange got started. I read it. It was pretty good. I recommend it. But 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 due to 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 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 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 90,000 shares of something and you're going to buy 30,000 shares from bats. That's another exchange, the Nasdaq one exchange, and then New York Stock Exchange. So you want to buy 30,000 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 furthest 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 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, okay, I'm going to buy 30 K shares at the Bats exchange and it's part of this larger order and you're going to need to buy 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 a straight fiber optics, cables, etcetera, 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 what 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. 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 reaching 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. 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 us in any way with what we're going to be end up coding out or how we're 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.