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, volumeWhat 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.. ?
-
separator
is a common parameter to allCSV
based data feeds. See: Docs - Data Feeds - Section: CSV Data Feeds Common parameters (theYahoo
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 chosendtformat
- Store the changed token
- Go via
super
to the original_loadline
which will now have a string to parse in the givendtformat
- Subclass
-
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
asdtformat
, which could allow supporting Javascript timestamps and others) or by providing a single method which can be overridden to parse the timestamp. -
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 ;)