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/

    backtrader giving me identical datetime prints on every line

    General Code/Help
    date2num datetime print
    1
    2
    754
    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.
    • B
      blonc last edited by blonc

      I have checked all the logical places for this, but still backtrader things I am passing daily bars. the strategy code is as followed:

          class Master(bt.Strategy):
          
              def __init__(self):
                  self.dataclose = self.datas[0].close
                  self.dt = self.datas[0].datetime.datetime(0)
              def next(self):
                  print(self.dt, self.dataclose[0])
          
          
          def runstrat():
              cerebro = bt.Cerebro()
              cerebro.broker.setcash(550000.0)
          
              data123 = bt.feeds.MarketStore(
                  symbol='ETH',
                  query_timeframe='1Min',
                  timeframe=bt.TimeFrame.Minutes,
              )
          
              cerebro.adddata(data123)
          
              cerebro.addstrategy(Master)
          
              cerebro.run()
          
              print('finished')
          
          if __name__ == '__main__':
              runstrat()
      

      the output is as followed. The first 10 lines comes from the datafeed directly, I have a line right after it loads the datetime to print(datetime.datetime.fromtimestamp(bar[0]).isoformat()) . So I know both time and date are being passed correctly after it gets passed through the date2num function :

          ## from datafeed directly
          2018-06-03T11:13:00
          2018-06-03T11:14:00
          2018-06-03T11:15:00
          2018-06-03T11:16:00
          2018-06-03T11:17:00
          2018-06-03T11:18:00
          2018-06-03T11:19:00
          2018-06-03T11:20:00
          2018-06-03T11:21:00
          2018-06-03T11:22:00
          ## from BT strategy printing as though it is daily bars
          2018-06-03 11:22:00 615.41
          2018-06-03 11:22:00 614.88
          2018-06-03 11:22:00 614.51
          2018-06-03 11:22:00 614.71
          2018-06-03 11:22:00 615.01
          2018-06-03 11:22:00 616.1
          2018-06-03 11:22:00 616.54
          2018-06-03 11:22:00 616.74
          2018-06-03 11:22:00 616.5
          2018-06-03 11:22:00 616.11
          finished
      1 Reply Last reply Reply Quote 0
      • B
        blonc last edited by blonc

        SOLVED

        the data feed was fine, it was with in the next() have to split from datetime to date and separate time

            class Master(bt.Strategy):
            
                def __init__(self):
                    self.dataclose = self.datas[0].close
            
                def next(self):
                    # self.log('Line')
                    print(self.datas[0].datetime.date(0), self.datas[0].datetime.time(0))
            
            def runstrat():
                cerebro = bt.Cerebro()
                cerebro.broker.setcash(550000.0)
            
                data123 = bt.feeds.MarketStore(
                    symbol='BTC',
                    query_timeframe='1Min',
                    timeframe=bt.TimeFrame.Minutes,
            
                )
            
                cerebro.adddata(data123)
            
                cerebro.addstrategy(Master)
            
                cerebro.run()
            
                print('finished')
            
            if __name__ == '__main__':
                runstrat()
        

        output

            2018-06-03 12:00:00 7685.13
            2018-06-03 12:01:00 7685.13
            2018-06-03 12:02:00 7685.13
            2018-06-03 12:03:00 7685.13
            2018-06-03 12:04:00 7685.13
            2018-06-03 12:05:00 7685.12
            2018-06-03 12:06:00 7685.12
            2018-06-03 12:07:00 7681.01
            2018-06-03 12:08:00 7681.01
            2018-06-03 12:09:00 7681.01
            finished
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }