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/

    Why won't order.Accept? Is it because I need volume?

    General Code/Help
    4
    7
    120
    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.
    • Adham Suliman
      Adham Suliman last edited by

      This is my first week working with Backtrader, and I must say, it's been a ton of fun. The documentation is incredible and there seems to be a strong community supporting it. On to my problem.

      My script is working perfectly when I'm using bt.feeds.YahooFinanceData() as my data source. The problem stems using my own data source which contains the following columns: datetime, open, high, low, and close. I currently DON'T have volume. My data is in 30 minute intervals and I'm currently reading it in using the following code:

      data = bt.feeds.GenericCSVData(
          dataname=datapath,
          # Do not pass values before this date
          fromdate=datetime.datetime(2019, 12, 5, 16, 30),
          # Do not pass values after this date
          todate=  data0.index.max().to_pydatetime(),
          datetime=0,
          open=1,
          high=2,
          low=3,
          close=4,
          volume=-1,
          timeframe=bt.TimeFrame.Minutes,
          compression=30)
      

      So what's my problem? Once some long conditions are met, my script should submit a buy limit order:

      if self.datalow[0] > self.ema_l[0]:
            # previous close less than the previous close
            # BUY, BUY, BUY!!! (with all possible default parameters)
               self.log('BUY CREATE, %.2f' % self.dataopen[0])
               self.order = self.buy(exectype = bt.Order.Limit,
               price = self.dataclose[0], 
               valid = datetime.now() + timedelta(hours = 2))   
      

      I separated my order.status for both order.Submitted and order.Accepted within my notify_order function:

          def notify_order(self, order):
              # https://www.backtrader.com/docu/order-creation-execution/order-creation-execution/
              # Check the order status. 
              # If it is submitted/accepted, leave the fucntion 
              if order.status in [order.Submitted]:
                  self.log(f"Order submitted but not complete. Price at {self.order.price}")
                  return
              
              if order.status in [order.Accepted]:
                  self.log(f"Order accepted but not complete. Price at {order.price}")
                  return
      

      As can be seen in the log. The order is submitted but never accepted. Why would this happen? Could it be because I don't have a volume column and Backtrader needs to know that there is some amount of volume there to be able to make the trade?

      2020-10-07T01:30:00, BUY CREATE, 1583.70
      2020-10-07T02:00:00, Order submitted but not complete. Price at 1583.7
      2020-10-07T02:00:00, Close, 1584.10
      2020-10-07T02:00:00, BUY CREATE, 1584.10
      2020-10-07T02:30:00, Order submitted but not complete. Price at 1584.1
      2020-10-07T02:30:00, Close, 1585.80
      2020-10-07T02:30:00, BUY CREATE, 1585.80
      2020-10-07T03:00:00, Order submitted but not complete. Price at 1585.8
      2020-10-07T03:00:00, Close, 1589.00
      

      Thanks for the help!

      1 Reply Last reply Reply Quote 0
      • E
        EMR last edited by

        Do you have the same issue if you add a dummy volume column (let's say 100 each line) to your own data source ?

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

          Excerpt from docs:

          Order Execution Logic
          The broker uses 2 main guidelines (assumptions?) for order execution.

          • The current data has already happened and cannot be used to execute an order.
          • Volume does not play a role
          • 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
          Adham Suliman 1 Reply Last reply Reply Quote 0
          • Adham Suliman
            Adham Suliman @EMR last edited by

            @EMR, Already tried this, and it, unfortunately, produced the same results.

            1 Reply Last reply Reply Quote 0
            • Adham Suliman
              Adham Suliman @ab_trader last edited by

              @ab_trader As can be seen in the last output block, there is a buy order at 1:30 at $ 1583.70. The order is submitted but not complete. It proceeds to close at a higher price. Why would it be incapable of buying at the closed price?

              PS
              I do have coc (cheat on close) set to True.

              1 Reply Last reply Reply Quote 0
              • Adham Suliman
                Adham Suliman last edited by

                Is there a way to delete this topic? I was able to resolve the problem.

                run-out 1 Reply Last reply Reply Quote 1
                • run-out
                  run-out @Adham Suliman last edited by

                  @Adham-Suliman said in Why won't order.Accept? Is it because I need volume?:

                  Is there a way to delete this topic? I was able to resolve the problem.

                  No, this is not an editable forum.

                  RunBacktest.com

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