Multiple exit stop orders at different prices
I have a strategy that I'm running live with our FIX implementation and noticed there is a difference in the way the stops are executed live and with the default BT broker.
- Buy 1000 shares of AAPL @ 200
- Place a stop loss for 1000 shares @ 198
- Buy another 1000 shares of AAPL @ 202
- Place a stop loss order for the second 1000 shares @ 201
For a long position, the stop order looks like this in our code:
# live trading stop_order = self.sell(data_min, price=stop_price, exectype=bt.Order.Stop, size=size) # backtesting stop_order = self.close(data_min, price=stop_price, exectype=bt.Order.Stop, size=size)
In this case, size = 1000. For backtesting, if I use self.sell or self.buy, the default BT broker doesn't take it as a stop loss order, it takes it as a stop entry and executes the order immediately. It works fine with self.close instead
In live trading, this works as expected. If I look at live orders on the FIX side, there will be a stop order for 1000 @ 198 and a second order for 1000 @ 201.
In backtesting, if the #4 stop is hit, it sells all 2000 shares and ignores the size= parameter. Is this how this works in the default BT broker or is there an additional parameter required.
In backtesting, if the #4 stop is hit, it sells all 2000 shares and ignores the size= parameter.
This is purely your assumption. Where is the log that shows that a single order has sold 2000 shares? Orders are evaluated individually and not based on your existing position.
Of course you have failed to try something very simple to verify that:
- Buy 100
- Place a stop loss for 100
- Buy 100
- Wait until the stop is hit and see if 100 or 200 are sold.