Data feed format
I'm new to backtrader.
I tried to read data feed with index of timestamp number(rather than standard datetime format), but doesn't work, the phraser doesn't re-format timestamp automatically.
So I want to know how to format the data feed format from reading side with changing the input source?
datapath = 'lib/binance-BTCUSDT-15m.csv' data = btfeeds.BacktraderCSVData( dataname=datapath) cerebro.adddata(data)
Timestamp,Open,High,Low,Close,Volume 1611658800000,32247.2,32249.98,32105.36,32139.29,616.047753 1611659700000,32139.29,32186.61,31807.19,31968.65,804.456726
Seems found method here, needs to configure btfeeds.GenericCSVData or override GenericCSVData
I tried with this ,
dataframe = pandas.read_csv(datapath, header=1, parse_dates=True, index_col=0) data = bt.feeds.PandasData( dataname=dataframe, nocase=True, timeframe=bt.TimeFrame.Minutes, )
but it returns error:
dt = tstamp.to_pydatetime() AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'
Anyone could help?
Thanks for your reply.
sorry, but I don't meant to spam here.
I did read the docs and sample codes. I defineda parser to convert this timestamp to date/time format, still doesn't work. It seems firstly I need to figure out the timestamp type of source data feed.
def parsedate(timestamp): if type(timestamp) is str: timestamp = int(timestamp) # put up with timestamp being in string format if timestamp > 1e10: timestamp // 100000 # put up with timestamp being in microseconds return datetime.utcfromtimestamp(float(timestamp)).replace(tzinfo=tz.tzutc())
vladisld last edited by
It seems the timestamp you are using is in millisecond format - this format is not directly supported by backtrader ( nor by regular python datetime class btw).
One way to overcome this is to somehow change the format to the seconds precision - either offline or on the fly.
btc = pd.read_csv("your_data.csv", index_col='Timestamp', parse_dates=['Timestamp'], date_parser=lambda x: datetime.utcfromtimestamp(int(x)/1000) ) data = bt.feeds.PandasData( dataname=btc, openinterest=-1 )
Thanks! I tried it worked.