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/

    data feed is fed one candle too early

    General Code/Help
    3
    5
    147
    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
      kuky last edited by

      I have a simple algo that has two input feeds: 5m and 1h.
      I read the data from csv and add them the standard way:

          data5m, data1h = BacktestDataProvider.load(data_type)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data5m)
          cerebro.adddata(data1h)
      

      The problem is, that the hourly data are fed too early.
      The next method:

          def next(self):
              self.log(
                  f"\nsimulation datetime {self.datetime.datetime(0)}\n"
                  f"5m feed datetime {self.datas[0].datetime.datetime(0)}\n"
                  f"1h feed datetime {self.datas[1].datetime.datetime(0)}\n"
              )
      

      and the output:

      simulation datetime 2021-02-22 22:30:00
      5m feed datetime 2021-02-22 22:30:00
      1h feed datetime 2021-02-22 22:00:00
      

      If the datetime in simulation is 22:30. It is not possible to receive 5m candle for time 22:30 or 1h candle for time 22:00, since (in the 1h case) you need all the data across interval 22:00 - 23:00.

      I guess, that it would work fine with just one datafeed, since the orders are executed at the open price of the next candle. However, in this case I make decisions based on the 1h candle and execute them on the next 5m candle, which is way before I get the 1h candle in reality.

      Any ideas how to solve this?

      D 1 Reply Last reply Reply Quote 1
      • D
        davidavr @kuky last edited by

        @kuky I believe BT assumes the timestamp of a bar is for the end of that time interval. Thus at the simulator time of 22:30, you are seeing the 5m bar that ended at 22:30 (i.e. 22:25-22:30) and the 1h bar that ended at 22:00 (which is the last complete 1h bar at this point).

        run-out 1 Reply Last reply Reply Quote 2
        • run-out
          run-out @davidavr last edited by

          @davidavr I think your right. The resample has this information:

          rightedge (default: True)

          Use the right edge of the time boundaries to set the time.

          If False and compressing to 5 seconds the time of a resampled bar for seconds between hh:mm:00 and hh:mm:04 will be hh:mm:00 (the starting boundary

          If True the used boundary for the time will be hh:mm:05 (the ending boundary)

          RunBacktest.com

          K 1 Reply Last reply Reply Quote 1
          • K
            kuky @run-out last edited by

            Thanks guys. I will need to change my dataset then.

            run-out 1 Reply Last reply Reply Quote 0
            • run-out
              run-out @kuky last edited by

              @kuky You can resample your data to the other edge. Keep timeframe and compression the same and just resample to rightedge = true. At least I think this would work.

              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