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

Bracket Order closing at wrong prices.



  • Hi Everyone, I am back testing bracket orders with the following ticks target for SI futures.

      def next(self):
          
            close = self.data.close[0]
        
            long_tp = close + 0.025         #####   REWARD
            long_stop = close - 0.1       #####   RISK
            
            short_tp = close - 0.025       #####   REWARD
            short_stop = close + 0.1      #####   RISK  
      
            if not self.position:
                if self.crossover:   
                    self.buy_bracket(stopprice=long_stop, limitprice=long_tp, exectype=bt.Order.Market, size=1)
                    
                if self.crossunder:
                    self.sell_bracket(stopprice=short_stop, limitprice=short_tp, exectype=bt.Order.Market, size=1)
                             
    

    However, after backtesting, the results are looking weird as the P&L are not consistent at 0.025 profit or -0.1 stoploss

    Open Time            Close Time            Entry  Candles  PnL
    2020-11-16 04:28:00   2020-11-16 05:04:00   25.11   36   0.02
    2020-11-16 05:44:00   2020-11-16 06:09:00   25.025   25   -0.095
    2020-11-16 06:17:00   2020-11-16 06:31:00   24.95   14   0.025
    2020-11-16 06:42:00   2020-11-16 06:45:00   24.99   3   0.02  
    2020-11-16 07:40:00   2020-11-16 08:31:00   24.97   51   -0.1
    2020-11-16 09:31:00   2020-11-16 09:42:00   24.945   11   0.03
    2020-11-16 10:12:00   2020-11-16 10:35:00   24.935   23   -0.095
    2020-11-16 10:40:00   2020-11-16 11:10:00   24.875   30   0.03
    2020-11-16 11:27:00   2020-11-16 11:30:00   24.905   3   0.025
    2020-11-17 02:55:00   2020-11-17 02:58:00   24.845   3   0.02
    2020-11-17 04:24:00   2020-11-17 04:38:00   24.795   14   0.025
    2020-11-17 05:41:00   2020-11-17 06:13:00   24.76   32   -0.105
    2020-11-17 06:16:00   2020-11-17 06:23:00   24.69   7   0.02
    2020-11-17 07:14:00   2020-11-17 07:21:00   24.73   7   0.025
    2020-11-17 07:27:00   2020-11-17 07:32:00   24.71   5   0.02
    

    I painstakingly gone thru the CSV for each trade and I just can't seem to find why it fills at such random prices. For example, the first winning short trade:

    3fb113d1-3b09-4b8d-92ef-ac59a6547096-image.png

    With an entry price of 25.11, it would only make sense to take profit at 25.085 (25.11-0.025) . However, you can see that 5:04 doesn't have 25.085 for OHLC. So why did it close the trade at 25.09? Am I missing some important parameters in the bracket definition?



  • Could you run the backtest printing data for order creation and execution, along with ohlcv? This will help us to see what's going on.



  • @run-out Thank you for getting back. I reviewed the CSV again and noticed these trades are correct because it calculated the crossover based on the previous Close price, and then entered at the next Open price. If I used Cheat-on-close, all the P&L are perfect. However, now my question is, how can I specify the Take Profit and Stop Loss ticks based on the parent's executed price instead of previous Close?



  • Well the the three orders are issued simultaneouly, so you can't create the take profit and stop loss on the executed price of the entry order, as it has not happened yet.

    Someone else my know a built in way, but I've issued orders manually in the past after the fill of the entry order, at which point you can use the executed price.



  • To your second question. I actually believe that the kind of tick trading you're talking of, using bid/ask, is not supported on backtrader.



  • My bad, I didn't read the question properly. I think what you're asking lends itself to creating a seperate stop/limit order once in the market.


Log in to reply
 

});