How to load csv with unix timestamp as dates?



  • 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.. ?


  • administrators

    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


  • administrators

    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.


  • administrators

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

    Added to the documentation



  • @backtrader well done to work with javascript timestamp ;)


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.