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/

    Forward dates returning values?

    General Code/Help
    2
    5
    82
    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.
    • run-out
      run-out last edited by

      The following code shouldn't be returning forward values but it is.

      def __init__(self):
              self.open = self.datas[0].open
              self.close = self.datas[0].close
      
          def next(self):
              date = self.data.datetime.date()
      
              if date > datetime.date(2006, 12, 1):
                  return
      
              print(
                  date,
                  self.datas[0].close[0],
                  self.close[3],
                  self.datas[0].open[0],
                  self.open[3],
              )
      

      Output:

      2006-01-02 3604.33 3650.24 3578.73 3652.19
      2006-01-03 3614.34 3666.99 3604.08 3650.54
      2006-01-04 3652.46 3671.78 3615.23 3667.1
      2006-01-05 3650.24 3644.94 3652.19 3671.23
      2006-01-06 3666.99 3668.61 3650.54 3645.73
      2006-01-09 3671.78 3670.2 3667.1 3667.16
      2006-01-10 3644.94 3629.25 3671.23 3670.27
      2006-01-11 3668.61 3644.41 3645.73 3628.73
      2006-01-12 3670.2 3610.07 3667.16 3639.57
      
      

      @vladisld Do I need another coffee? What's going on here?

      Full Code:

      import datetime
      import backtrader as bt
      import pandas as pd
      from extension.data import IQMinute
      from dateutil import relativedelta
      from datetime import date
      
      
      class Strategy(bt.Strategy):
          def log(self, txt, dt=None):
              """ Logging function fot this strategy"""
              dt = dt or self.data.datetime[0]
              if isinstance(dt, float):
                  dt = bt.num2date(dt)
              print("%s, %s" % (dt.date(), txt))
      
      
          def __init__(self):
              self.open = self.datas[0].open
              self.close = self.datas[0].close
      
          def next(self):
              date = self.data.datetime.date()
      
              if date > datetime.date(2006, 12, 1):
                  return
      
              print(
                  date,
                  self.datas[0].close[0],
                  self.close[3],
                  self.datas[0].open[0],
                  self.open[3],
              )
      
      if __name__ == "__main__":
          cerebro = bt.Cerebro()
          data = bt.feeds.GenericCSVData(
              dataname="data/2006-day-001.txt",
              dtformat=("%Y-%m-%d"),
              timeframe=bt.TimeFrame.Days,
              compression=1,
          )
          cerebro.adddata(data)
          cerebro.addstrategy(Strategy)
      
          cerebro.run()
      

      RunBacktest.com

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

        @run-out said in Forward dates returning values?:

        if date > datetime.date(2006, 12, 1):
        return

        I don't see the problem other than probably mixup of months and days. The code prints on each next until Dec'1 2006 - and the output confirms that.

        The print seems to be strange - but just probably because it prints in US locale ( %Y-%d-%m ) and not (%Y-%m-%d)

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

          @vladisld Hey Vlad,

          I'm using the conditional this just to stop the error at the end of the dataset, and this is not the important part..

           if date > datetime.date(2006, 12, 1):
                      return
          

          The important part is that I getting the values three days ahead of time when I should get an error from backtrader.

           self.datas[0].close[0],
           self.close[3],
          

          Note that the close column is reading ahead 3 days. 3650.24 in the third column is the close value 3 bars ahead in the second column. We are looking into the future. Should backtrader kick up an error?

           2006-01-02 3604.33 3650.24 3578.73 3652.19
          2006-01-03 3614.34 3666.99 3604.08 3650.54
          2006-01-04 3652.46 3671.78 3615.23 3667.1
          2006-01-05 3650.24 3644.94 3652.19 3671.23
          

          RunBacktest.com

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

            @run-out said in Forward dates returning values?:

            We are looking into the future. Should backtrader kick up an error?

            Why should backtrader kick up an error ? Referencing the future values are technically possible (it's questionable logically though)

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

              @vladisld I guess i haven't done that in so long I came to believe it wasn't possible.

              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