📜 ⬆️ ⬇️

How trading on a stock exchange actually works: A simple algorithm (part 1)



/ photo yuan2003 CC

High-frequency traders are a special caste of exchange players. Few, among them, condescend to describe the meaning of their profession in simple human language. We found the blog of Chris Stucchio, a former quantum who decided to write a brief apology for HFT .
')
This is not about justifying the profession in a social context, says Chris. It is about some intellectual foundations of the sphere of human activity, which is often misunderstood. In his post, the author tries to talk about the basis on which exchange trading and high-frequency trading is built, and with examples he explains why traders are so concerned about reducing transaction delays.

HFT mechanics


Any serious discussion about high-frequency trading has to start from scratch. Explain the mechanisms and practices underlying the HFT. The main object in it, as in the case of any operations with securities, is the order book (order book). Suppose a certain trader Mel enters the stock exchange with a desire to buy a certain amount of Blue Sun shares. He roughly represents how much he is willing to buy and at what maximum price. Next, he masters the process of converting orders and processing transactions (matching engine). This could be the BATS, ARCA or any other sites where it places an order:
BUY(owner=Mal, max_price=20.00, quantity=100) 

At this stage, Mel still did not sell anything and did not buy. He simply notified the whole world about his intention. The system takes this request and displays the information (anonymously) to all other traders. Now imagine that there is a girl trader Inara, who wants to sell 200 shares of the company at, say, $ 20.10. She also places her application, which is displayed in the register of orders. Now it looks like this:

 SELL(owner=Inara, min_price=20.10, quantity=200) ------ BUY(owner=Mal, max_price=20.00, quantity=100) 

At this point, no deal has happened yet. Mel wants to buy 20 dollars or less, Inara wants to sell for 20.10 or more. The market has shaped the difference between ask and bid at $ 0.10.

Let's complicate the task. While the first two are scratching their heads, Kylie and River enter the market. Suppose there is a new position for the sale of 200 shares at $ 20.21 and one more per 100 shares at 20.10.

Finally, Simon appears on the stock exchange, who places an order to purchase 250 shares at a price of $ 20.21. He will happily make a deal with Inara, Kylie and River. They all requested less of its price. The system uses two basic rules that determine who will sell how much to whom:

  1. Price: the best price quote always wins.
  2. Time: if prices are equal, the one who placed the order earlier wins.

So, at the time of the appearance of the last buyer, the order register looked like this:

 SELL(owner=Kaylee, min_price=20.21, quantity=200) <- Trades third SELL(owner=River, min_price=20.10, quantity=100) <- Trades second SELL(owner=Inara, min_price=20.10, quantity=200) <- Trades first ------ BUY(owner=Mal, max_price=20.00, quantity=100) 

After Simon placed his order for the purchase of 250 shares, the mechanism will distribute it as follows:


Everything, the buyer received that volume of actions which wanted. The register will now look like this:

 SELL(owner=Kaylee, min_price=20.21, quantity=200) SELL(owner=River, min_price=20.10, quantity=50) ------ BUY(owner=Mal, max_price=20.00, quantity=100) 

Since Kylie does not want to give a good price, her order remains unfilled. This is the basic mechanism of trading on the exchange. In practice, of course, everything is more complicated. Details appear, other types of orders appear, besides just limit orders. But for the purposes of this article this will be enough. In more detail about the mechanism of trading it is possible to read here and here . But remember, no book will give you a complete picture. Alternatively, you can delve into the documentation of the exchange platforms themselves, then you can make a more complete impression about how everything works on a specific site.

Market-making


In high-frequency algorithmic trading, the market-making strategy is mainly used. In the simplest sense, this means that the trader is playing on both sides of the table at the same time. It does not open or close positions, focusing on the trend of the market. Instead, it exposes assets to both buy and sell at the same time. If you want to buy stocks, he will sell them for $ 20.10. If you want to sell, he will buy them from you for the price of $ 20 per piece. While he buys and sells, the price difference fluctuates slightly. His goal and profit is the same $ 0.10.

Of course, even in this case, the trader bears certain risks. No one guarantees that after buying shares at $ 20, the market will not fix at this level. If he bought 20, and the assets fell to 15 before he managed to throw them off, he would lose $ 5 per share. Therefore, such a trader must balance the risks, taking into account the profits. If he keeps the ask / bid difference too low, he will lose money, if high - no one will trade with him.

It should be understood that there is nothing new or revolutionary in the market-making strategy. Remember, movies about the stock exchange, where traders opened and closed positions with shouts and gestures. This was called " trade in the pit ." This practice was spread from ancient times to the beginning of the 2000s, when the step was 1/8 and 1/16 of a dollar.



/ photo yuan2003 CC

Automated trading systems have replaced such market makers for a very simple reason: cost reduction. Firstly, such a strategy works only with several assets at the same time; not a single person will be able to hold positions on hundreds of stocks in his head. Secondly, you save time. The system is able to generate tens and hundreds of thousands of dollars in profits annually. That is, if you put a person in its place, he, being smart enough to correctly assess the market, will work for at least a couple of tens of thousands of dollars in annual income. A mediocre server in the data center is able to manage hundreds of strategies with a profit of up to $ 50,000 per year. To do this much faster and more accurately than a man.

In other words, we are dealing with a special case of the general process of replacing people with robots.

Time and order flow


For the market maker, the essence of the game is in the movement of orders (order flow). As long as your purchases and sales are coordinated, your profit will be proportional to this:

 (# of shares traded) x (ASK PRICE - BID PRICE) 

The constancy of this ratio depends, by and large, only on the trader’s ability to assess risks. In theory, the more you sell, the higher the profit. So how to sell more?

The answer is banal: you need to stay in the top of the register of orders. The most direct way to this is to offer the best price. Let's return to our example with traders. Suppose there is also Jane, who wishes to stand at the head of a queue of buyers. He needs to offer a better price than Mel.

 SELL(owner=Kaylee, min_price=20.21, quantity=200) SELL(owner=River, min_price=20.10, quantity=50) ------ BUY(owner=Jayne, max_price=20.05, quantity=100) <-     BUY(owner=Mal, max_price=20.00, quantity=100)  , ,   . 

Now it’s time to think about balance. Since Jane has only $ 0.05 per share sold, she needs to be confident that the profit will outweigh the risks. Suppose that the margin between expected profit and loss is set at $ 20.05. In other words, there is not a single participant on the market who believes in making a profit by offering more than this amount. In this scenario, Jane will always be the first, simply because it came first.

This example shows why the reaction rate is so important. For example, at exactly 10:31 am and 30 seconds in the morning comes a new information that allows you to raise the price to 20.07 in order to make a profit. Perhaps there was a press release, where there is a hint that the company's shares will go up. In this case, both Mel and Jane will want to raise the price to 20.07. Whoever gets it done faster, he will head the list of buyers.

 SELL(owner=Kaylee, min_price=20.21, quantity=200) SELL(owner=River, min_price=20.10, quantity=50) ------ BUY(owner=Mal, max_price=20.07, quantity=100) <-   10:31:30:427 BUY(owner=Jayne, max_price=20.07, quantity=100) <-   10:31:30:639,  212  . 

In this example, Jane is late for 212 milliseconds. In general terms, this is high-frequency trading. He explains why so much effort was put into reducing the reaction time of the automated system. The one who opens the position first is more likely to make deals.

The second reason is that there is a market movement. Often, traders want to cancel their orders. At 10:31 an event occurred that indicates the price movement upwards. Rivera needs to have time to cancel his sell order at $ 20.10 to raise the price to $ 20.20. Suppose there is someone else who, on the contrary, will benefit after this event, attack Rivera’s order at a price of 20.10. River will remain in the game if it can quickly remove this order. If an attack is made on the order, the River will be forced to raise the price only to $ 20.15 in order to risk only 0.05 per share.

Why does everyone fall for one price?


An astute reader has probably wondered why Mel and Jane agree that the best bid price is 20.07? Maybe Jane's calculations show that it will be 20.075, and Mel's calculations - 20.071? Yes, most likely, both players disagree on what will be the best price. There is no reason why the programs or trading strategies of Mel and Jane will give them the same price in thousandths.

Regardless of what the strategy suggests, they cannot place orders for their best prices. The SEC Rule 612 minimum price increment rule unequivocally prohibits them to set the sale or purchase price in fractions of a cent. That is, the purchase of 100 shares for 20.07 is good, and the purchase of 100 shares at 20.075 is already illegal. By the way, until 2001 the limit was 1/16 of a dollar or $ 0.0625 (there is a similar price move for various financial instruments over other exchange platforms, for example, on the Moscow Stock Exchange ).

In the real market, with more than five participants, you can expect a large number of orders below the top register positions (the highest bid price) by 20.06, 20.05, and so on. But the majority of traders will also be close to the top in real life.

In his next posts, the author promises to raise the topic of social benefits and harms from HFT, and also to tell what is wrong with high-frequency trading today and how to fix it.

Other materials on the topic from ITinvest :


Source: https://habr.com/ru/post/306392/


All Articles