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/

    4 hour time intervals

    General Code/Help
    date-time
    3
    6
    2005
    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
      kreut last edited by

      Hi,

      I'm just getting used to Backtrader and I'm trying to use the Quick Start guide just slightly tweaked. Basically, my data is in 4 hour intervals, but I can't quite see how to get Backtrader to keep things at the 4 hour level. In particular, I'm using:

      data = btfeeds.GenericCSVData(
      dataname='my.csv',
      fromdate=datetime.datetime(2000, 1, 1),
      todate=datetime.datetime(2018, 12, 31),
      nullvalue=0.0,
      dtformat=('%Y-%m-%d %H:%M:%S'),
      datetime=0,
      high=1,
      low=2,
      open=3,
      close=4,
      volume=-1,
      openinterest=-1
      )

      Then, I'm using:

      def log(self, txt, dt=None):
      ''' Logging function for this strategy'''
      dt = dt or self.datas[0].datetime.date(0)
      print('%s, %s' % (dt.strftime('%Y-%m-%d %H:%M:%S'), txt))

      However, my print out is looking like:

      2017-12-06 00:00:00, Close, 0.00000994
      2017-12-06 00:00:00, Close, 0.00001009
      2017-12-06 00:00:00, Close, 0.00000940

      i.e. the hours are all 00:00:00. Any help would be appreciated.

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

        FAQ - Session Start/End Times and TimeFrame/Compression

        You need to set time frame and compression.

        • 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
        K 1 Reply Last reply Reply Quote 1
        • K
          kreut @ab_trader last edited by

          @ab_trader Definitely a step in the right direction! I've updated my code:

          data = btfeeds.GenericCSVData(
              dataname='ada.csv',
              fromdate=datetime.datetime(2000, 1, 1),
              todate=datetime.datetime(2018, 12, 31),
              timeframe=bt.TimeFrame.Minutes, compression=240,
              nullvalue=0.0,
              dtformat=('%Y-%m-%d %H:%M:%S'),
              datetime=0,
              high=1,
              low=2,
              open=3,
              close=4,
              volume=-1,
              openinterest=-1
          )
          

          and now, my buy creates/buy executes are acting as expected (the timing seemed to be "off" before). However, the printout remains unchanged; backtrader still seems to ignore my hours at least in the cosmetic sense. Is there another setting that I need to add to make sure the time component gets printed out?

          Thanks!

          1 Reply Last reply Reply Quote 1
          • A
            ab_trader last edited by

            Can you show full script and data sample?

            • 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
            1 Reply Last reply Reply Quote 0
            • F
              fintrading @kreut last edited by

              @kreut said in 4 hour time intervals:

              You have to call self.datas[0].datetime.datetime(0) in your logging method:

              def log(self, txt, dt=None):
                  ''' Logging function for this strategy'''
                  dt = dt or self.datas[0].datetime.datetime(0)
                  print('%s, %s' % (dt.strftime('%Y-%m-%d %H:%M:%S'), txt))
              K 1 Reply Last reply Reply Quote 1
              • K
                kreut @fintrading last edited by

                @fintrading Got it! Thanks...

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