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/

    GenericCSVData - accessing time attribute

    General Code/Help
    1
    2
    83
    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.
    • C
      chubble last edited by

      I'm using GenericCSVData to import hourly bars. The source data has the format date, time, O, H, L, C, volume and I create the data feed like this:

      data = bt.feeds.GenericCSVData (
          dataname='xx-1h.csv',
          separator=";",
      
          fromdate=datetime.datetime(2017, 8, 16),
          todate=datetime.datetime(2017, 12, 31),
      
          nullvalue=0.0,
      
          dtformat=('%d/%m/%Y'),
          tmformat=('%H:%M:%S'),
      
          datetime=0,
          time=1,
          open=2,
          high=3,
          low=4,
          close=5,
          volume=6,
          openinterest=-1
      )
      

      This works to the extent that I can retrieve the date, OHLC and volume however the time portion of the datetime attribute is not being correctly set (or more likely I just do not know how to access it properly). For example, this:

              print(self.datas[0].lines.close[0])
              print(self.datas[0].lines.open[0])
              print(self.datas[0].lines.datetime.date())
              print(self.datas[0].lines.datetime.time())
      

      Produces this output:

      3.068
      3.065
      2017-09-14
      23:59:59.999989
      

      ...where the time (for this bar) should be 18:00:00. Here is the corresponding row from the source CSV file:

      14/09/2017;18:00:00;3.065;3.068;3.065;3.068;90
      

      How do I retrieve the time? Any help appreciated.

      1 Reply Last reply Reply Quote 0
      • C
        chubble last edited by

        Argh I found the solution moments after posting. For reference the timeframe needs to be set, like this (there does not seem to be a TimeFrame.Hours but Minutes does the trick):

        timeframe=bt.TimeFrame.Minutes
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors