For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

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.

    An example:

    1. Buy 1000 shares of AAPL @ 200
    2. Place a stop loss for 1000 shares @ 198
    3. Buy another 1000 shares of AAPL @ 202
    4. 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.


  • administrators

    @ard9000 said in Multiple exit stop orders at different prices:

    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.