Backtrader Community

    • 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/

    Limit order trade entry not correct

    General Discussion
    4
    6
    339
    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.
    • K
      kr last edited by

      Hello, I am in need of help to understand limit orders and why the following code doesn't execute like what the docs says.
      Here is my strategy:

      class backtestCSV(bt.feeds.GenericCSVData):
          params = (
              ('datetime', 0),
              ('open', 1),
              ('high', 2),
              ('low', 3),
              ('close', 4),
              ('volume', 5),
              ('openinterest', -1),
              ('timeframe', bt.TimeFrame.Ticks),
              ('compression', 1),
              ('dtformat', '%Y-%m-%d %H:%M:%S.%f'),
          )
      
      class testStrategy(bt.Strategy):
          def __init__(self):
              self.bought = False
      
          def notify_order(self, order):
              print("ORDER:", self.datetime.datetime(ago=0), order.exectype, order.status, self.data0.close[0])
      
          def notify_trade(self, trade):
              print("TRADE:", self.datetime.datetime(ago=0), trade.status, self.data0.close[0])
      
          def next(self):
              if self.bought == False:
                  entry_price = 2.0
                  self.buy(exectype=bt.Order.Limit, price=entry_price)
                  self.bought = True
                  print("BUY:", self.datetime.datetime(ago=0), entry_price)
      

      and here is the data:

      2019-01-01 00:00:00.950,1.00368,1.00368,1.00368,1.00368,29000
      2019-01-01 00:00:01.176,1.00368,1.00368,1.00368,1.00368,81000
      2019-01-01 00:00:02.248,1.00367,1.00367,1.00367,1.00367,50000
      2019-01-01 00:00:02.506,1.00368,1.00368,1.00368,1.00368,94000
      2019-01-01 00:00:02.608,1.00369,1.00369,1.00369,1.00369,56000
      2019-01-01 00:00:02.709,1.00370,1.00370,1.00370,1.00370,57000
      2019-01-01 00:00:05.864,1.00370,1.00370,1.00370,1.00370,93000
      2019-01-01 00:00:05.966,1.00372,1.00372,1.00372,1.00372,64000
      2019-01-01 00:00:06.068,1.00374,1.00374,1.00374,1.00374,57000
      2019-01-01 00:00:06.173,1.00374,1.00374,1.00374,1.00374,95000
      2019-01-01 00:00:06.274,1.00375,1.00375,1.00375,1.00375,38000
      2019-01-01 00:00:06.434,1.00375,1.00375,1.00375,1.00375,45000
      2019-01-01 00:00:07.360,1.00375,1.00375,1.00375,1.00375,93000
      2019-01-01 00:00:09.092,1.00374,1.00374,1.00374,1.00374,01000
      2019-01-01 00:00:09.143,1.00372,1.00372,1.00372,1.00372,63000
      2019-01-01 00:00:09.296,1.00372,1.00372,1.00372,1.00372,76000
      2019-01-01 00:00:09.499,1.00372,1.00372,1.00372,1.00372,28000
      2019-01-01 00:00:09.601,1.00372,1.00372,1.00372,1.00372,56000
      2019-01-01 00:00:09.982,1.00372,1.00372,1.00372,1.00372,56000
      2019-01-01 00:00:10.084,1.00372,1.00372,1.00372,1.00372,58000
      2019-01-01 00:00:10.186,1.00373,1.00373,1.00373,1.00373,68000
      2019-01-01 00:00:10.287,1.00373,1.00373,1.00373,1.00373,19000
      2019-01-01 00:00:12.072,1.00372,1.00372,1.00372,1.00372,65000
      2019-01-01 00:00:12.174,1.00370,1.00370,1.00370,1.00370,25000
      2019-01-01 00:00:13.738,1.00371,1.00371,1.00371,1.00371,76000
      2019-01-01 00:00:13.960,1.00370,1.00370,1.00370,1.00370,92000
      2019-01-01 00:00:14.076,1.00371,1.00371,1.00371,1.00371,76000
      2019-01-01 00:00:14.372,1.00372,1.00372,1.00372,1.00372,93000
      2019-01-01 00:00:14.474,1.00371,1.00371,1.00371,1.00371,39000
      2019-01-01 00:00:14.575,1.00371,1.00371,1.00371,1.00371,12000
      2019-01-01 00:00:14.802,1.00370,1.00370,1.00370,1.00370,25000
      2019-01-01 00:00:15.886,1.00371,1.00371,1.00371,1.00371,94000
      2019-01-01 00:00:15.987,1.00372,1.00372,1.00372,1.00372,85000
      2019-01-01 00:00:16.088,1.00371,1.00371,1.00371,1.00371,94000
      2019-01-01 00:00:16.804,1.00373,1.00373,1.00373,1.00373,62000
      2019-01-01 00:00:16.905,1.00374,1.00374,1.00374,1.00374,69000
      2019-01-01 00:00:17.007,2.00375,2.00375,2.00375,2.00375,86000
      2019-01-01 00:00:17.136,2.00374,2.00374,2.00374,2.00374,47000
      2019-01-01 00:00:17.237,2.00374,2.00374,2.00374,2.00374,94000
      2019-01-01 00:00:17.795,2.00375,2.00375,2.00375,2.00375,87000
      2019-01-01 00:00:18.226,2.00375,2.00375,2.00375,2.00375,44000
      

      And here is STDOUT

      BUY: 2019-01-01 00:00:01.176004 2.0
      ORDER: 2019-01-01 00:00:02.247996 2 1 1.00367
      ORDER: 2019-01-01 00:00:02.247996 2 2 1.00367
      ORDER: 2019-01-01 00:00:02.247996 2 4 1.00367
      TRADE: 2019-01-01 00:00:02.247996 1 1.00367
      

      Can somebody tell me why the trade happens at 00:00:02 when the price is still at 1.00. Shouldn't happen at 00:00:17 when the price has reached 2.0, which is the price I set for the limit order?

      B 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        IIUC, you've just submitted the limit buy order with a limit price set to 2.0 - meaning the order should be executed at the price <=2.0 - which is exactly what has happened at the next bar. Anything wrong with that ?

        1 Reply Last reply Reply Quote 1
        • K
          kr last edited by

          I was reading this post:
          https://backtest-rookies.com/2018/08/31/backtrader-bracket-orders/

          and it said this:
          Stop Entry, Stop Loss and Take Profit
          Sometimes we may want to enter a position at a price which is worse than what we can have right now. We might choose to do this when looking for confirmation that price will continue moving in a certain direction. Another reason might be to try and catch a breakout. To enter with a stop order we just change the exectype like so: self.sell_bracket(limitprice=short_tp, price=entry, stopprice=short_stop, exectype=bt.Order.Limit)

          I guess this is incorrect. You are saying this is not how to do entry at a worse price. Is this even possible then?

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

            @kr the text you shown is not from the official docs. Link to official docs - Docs - Orders - Creation/Execution. If you want the order to be executed at set price or worse, than use stop order. Limit order executes on the set price or better.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            K 1 Reply Last reply Reply Quote 1
            • K
              kr @ab_trader last edited by

              @ab_trader that works perfectly. Thank you very much!

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators @kr last edited by backtrader

                @kr said in Limit order trade entry not correct:

                    def notify_trade(self, trade):
                        print("TRADE:", self.datetime.datetime(ago=0), trade.status, self.data0.close[0])
                

                and

                Adding to the answers above.

                t

                @kr said in Limit order trade entry not correct:

                ORDER: 2019-01-01 00:00:02.247996 2 4 1.00367
                TRADE: 2019-01-01 00:00:02.247996 1 1.00367
                

                That's NOT the trade time or the order execution time. That's the current datetime kept by the strategy by tracking the latest timestamp reported by the different data feeds.

                Order execution data is kept in the attribute order.executed.

                See: Docs - Order - https://www.backtrader.com/docu/order/

                Furthermore, an order has different states notified and the code is accounting for none to understand when the execution is reported as Complete.

                The same with trade.

                See: Docs - Trade - https://www.backtrader.com/docu/trade/

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors