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/

    Problem with DateTime format

    General Discussion
    2
    3
    331
    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.
    • G
      Giebels last edited by

      Hi,

      I'm just getting into the use of backtrader and I'm struggling to get my DateTime format to work. I want to write a python script that prints all closing values of for example the 5 minute chart on a given day.

      The date and time in the csv file that I load are in "2020-11-02 22:30:00" format. Whenever I run the program dates work just fine, however time is always stuck on the same value:

      Screenshot_1.png

      I have written the following code:

      import backtrader as bt
      import datetime
      
      class PrintClose(bt.Strategy):
      
          def __init__(self):
              self.dataclose = self.datas[0].close
      
          def log(self, txt, dt=None):
              dt = self.datas[0].datetime.datetime()
              print(f'{dt.isoformat()} {txt}') #Print date and close
      
          def next(self):
              self.log('Close: ', self.dataclose[0])
      		
      #Instantiate Cerebro engine
      cerebro = bt.Cerebro()
      
      data = bt.feeds.GenericCSVData(
          dataname='BTCUSD_PERP-5m-2020-11_Corrected.csv',
          fromdate=datetime.datetime(2020, 11, 1),
          todate=datetime.datetime(2020, 11, 2),
          nullvalue=0.0,
          dtformat=("%Y-%m-%d  %H:%M:%S"),
          timeformat=("%H:%M:%S"),
          time=-1,
          datetime=0,
          high=2,
          low=3,
          open=1,
          close=4,
          volume=5,
          openinterest=-1
      )
      
      cerebro.adddata(data)
      cerebro.addstrategy(PrintClose)
      cerebro.run()
      
      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @Giebels last edited by

        @giebels try to specify the correct timeframe and compression for you data feed. If not specified, it defaults to daily prices, and it seems your data are in minutes time format.

        1 Reply Last reply Reply Quote 0
        • G
          Giebels last edited by

          Thanks for the help! That solved the problem.

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