Index error when importing resampled data
-
Strategy code
def next(self): self.count += 1 print(self.count) print('*' * 5, 'NEXT:', bt.num2date(self.data.datetime[0]), self.data._name, self.data.open[0], self.data.high[0], self.data.low[0], self.data.close[0], self.data.volume[0], bt.TimeFrame.getname(self.data._timeframe), len(self.data)) if not self.position: if self.data.close[0] > self.sma[0]: print("BUY %.2f" % self.data.close[0]) self.order = self.buy() else: if self.dataclose[0] < self.sma[0]: print("SELL %.2f" % self.data.close[0]) self.order = self.sell()
Importing Datafeed
data = btfeeds.GenericCSVData(dataname='cdataNov.csv',dtformat=('%Y-%m-%d'),tmformat=('%H:%M:%S'), datetime=1, high=3, low=4, open=2, close=5, volume=-1, openinterest=-1) cerebro.adddata(data) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.addstrategy(TestStrategy) cerebro.run()
Error
Traceback (most recent call last): Starting Portfolio Value: 100000.00 File "C:/Users/GEO/PycharmProjects/untitled/one.py", line 93, in <module> cerebro.run() File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies data.preload() File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\feed.py", line 685, in preload while self.load(): File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\feed.py", line 476, in load _loadret = self._load() File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\feed.py", line 707, in _load return self._loadline(linetokens) File "C:\Users\GEO\PycharmProjects\untitled\venv\lib\site-packages\backtrader\feeds\csvgeneric.py", line 105, in _loadline dtfield = linetokens[self.p.datetime] IndexError: list index out of range
What can I do to fix this error?
-
You can start with posting the whole script, which can be easily run by somebody who wants to help. Pieces you posted are not full.
-
The error happens when loading the data ... which is the only thing you don't provide.
@Cosmin-George said in Index error when importing resampled data:
data = btfeeds.GenericCSVData(dataname='cdataNov.csv',dtformat=('%Y-%m-%d'),tmformat=('%H:%M:%S'), datetime=1, high=3, low=4, open=2, close=5, volume=-1, openinterest=-1)
Else ... how can anyone one if that matches?
Additionally and as pointed out by @ab_trader
@ab_trader said in Index error when importing resampled data:
You can start with posting the whole script
There is a tendency to post random parts of the code and there is no soul on this earth who can do something with that. You will argue, as many other have done, that your script is rather complex and bla, bla, bla.
Exactly, when one runs into a problem, one creates a very simple script to isolate the problem instead of working with a complex script.
@Cosmin-George said in Index error when importing resampled data:
importing resampled data
Furthermore you mentioned resampled data and you do the following ...
@Cosmin-George said in Index error when importing resampled data:
cerebro.adddata(data)
And that's for sure not resampling.
-
The rest of the code is the default getting started code.
I noticed there was some issues with the data, after fixing, I'm getting another error
File "C:\Users\GEO\AppData\Local\Programs\Python\Python37-32\lib\_strptime.py", line 362, in _strptime data_string[found.end():]) ValueError: unconverted data remains: 00:02:13
I tried adding different timeframe like minutes ticks etc same error
data = btfeeds.GenericCSVData(dataname='finally.csv',dtformat=('%Y-%m-%d'),tmformat=('%H:%M:%S'),timeframe=bt.TimeFrame.Seconds, compression=1,datetime=1, high=3, low=4, open=2, close=5, volume=-1, openinterest=-1)
timestamp Open High Low Close 0 2019-11-01 00:02:13 9153.5 9154 9147.5 9147.5 1 2019-11-01 00:06:33 9147.5 9148 9123.5 9123.5
-
After removing the following arguments the code ran, lol.
dtformat=('%Y-%m-%d'),tmformat=('%H:%M:%S'),