Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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 Calculation Question that No One Asked.

    General Discussion
    2
    3
    54
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Jeffrey C F Wong
      Jeffrey C F Wong last edited by

      I have been playing with bracket orders for my simple crossover strategy and all the trade entry time and execution price is matching up with Tradingview's result which is great news. However as I examine each trade, the P&L and closing time is quite off.

       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)
      
      
      RESULTS:
      Open Time             Close Time            Entry  Mins  PnL(Ticks)
      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
                      
      

      What I noticed is that with backtrader's bracket order, the [main, stop, limit] are all calculated based on the previous candle's Close price. So now the parent order [main] is executed based on market price, hence the next candle's open price. Because othewise, all the P&L should either be 0.025 or -0.1 Whereas for Tradingview, the children orders [stop, limit] are calculated based on the parent order's [main] executed price. Looking below, you can see that all the P&L are consistent.

      be9d4927-4a9f-40f1-83df-573e03cb8b60-image.png

      Anyone got a clue on how to replicate this behavior in backtrader's bracket order?

      1 Reply Last reply Reply Quote 0
      • Jeffrey C F Wong
        Jeffrey C F Wong last edited by

        @backtrader, @ab_trader , @run-out you guys seems to be the most active gurus on this community, if you can please shine some light on my question, it'd be greatly appreciated.

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          @Jeffrey-C-F-Wong said in Bracket Order Calculation Question that No One Asked.:

          Anyone got a clue on how to replicate this behavior in backtrader's bracket order?

          If you want to use bracket order with the main market order executed on the open, than you can use cheat-on-open and issue the bracket order during next_open() call. If I remember correctly, than during next_open() the only price delivered is current open, so your stop and limit prices should be calculated correctly.

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }