Implementing limit and stop orders
In Chapter 10, Types of Orders and Their Simulation in Python, we considered three main types of orders: market, limit, and stop. However, so far, we have only used market orders in actual codes. Although we noted that a live trading application may not ever use stop and limit orders because they can be emulated on the client side and sent to the market as market orders when necessary, it would be definitely useful to have both types of orders implemented in the backtester to simplify the development of trading strategies.
Let’s quickly recall the essence of limit and stop orders.
A limit order is always executed at a price equal to the order price or better. This means that if the market price is currently 100 and a buy limit order is sent below the market, for example, at 99, then it will be filled only when the price becomes 99 or lower. If a buy limit order is sent above the market, for example, at 101, then it will be executed immediately...