Backtrader Community

    • 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/

    How to load csv with unix timestamp as dates?

    General Code/Help
    3
    5
    3124
    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.
    • S
      Serp last edited by Serp

      Hi,

      I'm rellay new to algorithmic trading and will test around with this backtester, but to do this I need to know how to import a csv file not meeting the standard look.

      The csv file looks like this:
      unix timestamp, close, volume

      What should I use for "dtformat" in GenericCSVData ?

      And can I use parameters like "seperator" also in this GenericCSVData? I'm not sure, cause here https://www.backtrader.com/docu/datafeed.html it is written under the "Yahoo" feed, and I don't use Yahoo. So I'm not sure if those params are also valid for GenericCSVData.. ?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

        separator is a common parameter to all CSV based data feeds. See: Docs - Data Feeds - Section: CSV Data Feeds Common parameters (the Yahoo mention is a leftover from the original version, to be corrected)

        There is no support for a Unix timestamp right now, but the following should work

        • Subclass CSVGenericData
        • Override _loadline
        • Change the value of the token containing the Unix timestamp to a datetime and then back to a date string according to your chosen dtformat
        • Store the changed token
        • Go via super to the original _loadline which will now have a string to parse in the given dtformat

        Docs - CSV Data Feed Development

        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by

          This is probably the only needed thing in the subclass (untested)

              def _loadline(self, linetokens):
                  dtfield = linetokens[self.p.datetime]
                  dtime = datetime.utcfromtimestamp(int(dtfield))
                  linetokens[self.p.datetime] = dtime.strftime(self.p.dtformat)
                  return super(MyClassName, self)._loadline(linetokens)
          

          The next release in any case will get some though as to best integrate that directly (probably by supporting passing an int as dtformat, which could allow supporting Javascript timestamps and others) or by providing a single method which can be overridden to parse the timestamp.

          1 Reply Last reply Reply Quote 1
          • B
            backtrader administrators last edited by

            Small release 1.9.38.116 issued which contains support code to do it without subclassing.

            Added to the documentation

            R 1 Reply Last reply Reply Quote 2
            • R
              remroc @backtrader last edited by

              @backtrader well done to work with javascript timestamp ;)

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