time data 'x' does not match format '%Y-%m-%d %H:%M:%S'
-
Hi there,
I'm still trying to convert unix timestamp to date, but I just can't get it to work.
From
https://www.backtrader.com/docu/datafeed/#genericcsvdata
So I tried it on my code:
here's a sample of my csv:
Open Time ,Open ,High ,Low ,Close ,Volume ,Close Time ,QuoteAssetVol ,Not ,Takerbuybv , takerbuyqv , 1577836800.0,7195.24000000,7196.25000000,7178.64000000,7179.78000000,95.50913300,1577837099999,686317.13625177,1127,32.77324500,235537.29504531,0
and here's the output:
-
You need to convert the unixtimestamp to date using bt.date2num function.. Have a look at the
Binary Feed DocYou can use open time or close time depending on your choice.
-
@rajanprabu
thank you for your reply, trying it out! -
I tried using the
"VChartData Full Code" at the bottom of
https://www.backtrader.com/docu/datafeed-develop-general/datafeed-develop-general/How do I implement it? In the sample code, there are several "dataname" do I change it to the name of my file or something else? Sorry I don't really understand how to use it :/
-
@Gleetche Looking at the docstrings:
- ``dtformat``: Format used to parse the datetime CSV field. See the python strptime/strftime documentation for the format. If a numeric value is specified, it will be interpreted as follows - ``1``: The value is a Unix timestamp of type ``int`` representing the number of seconds since Jan 1st, 1970 - ``2``: The value is a Unix timestamp of type ``float``
Try using numeric value instead of format string.
-
I changed it to dtformat=2 in
data = (bt.feeds.GenericCSVData( dataname='data/1jan2020.csv', timeframe=bt.TimeFrame.Minutes, compression=5, nullvalue=0.0, dtformat=2, datetime=0, open=1, high=2, low=3, close=4, volume=5, openinterest=-1 ))
The code is working now, but back like how it originally was, only the date is being shown without the time:
-
Please check what are you printing. I did a quick test and it has time as well..
-
@Gleetche Try printing:
self.datetime.datetime()
-
Thank you very much! I got it fixed, I just changed:
the logging function fromdef log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt))
to
def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.datetime(0) print('%s, %s' % (dt.isoformat(), txt))