Index out of range error using CSV file
If anybody knows how to fix this I would very much appreciate it.
I'm trying to implement a CSV file but I keep getting this error:
Traceback (most recent call last): File "c:\Users\dnzug\Zugba_Projects\Projects\backtest.py", line 46, in <module> cerebro.run() File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies data.preload() File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\feed.py", line 688, in preload while self.load(): File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\feed.py", line 479, in load _loadret = self._load() File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\feed.py", line 710, in _load return self._loadline(linetokens) File "C:\Users\dnzug\Zugba_Projects\work_env\lib\site-packages\backtrader\feeds\csvgeneric.py", line 148, in _loadline csvfield = linetokens[csvidx] IndexError: list index out of range
I'm using this class to implement the CSV file:
class CSV(bt.feeds.GenericCSVData): params = ( ('dtformat', '%Y-%m-%d'), ('datetime', 0), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', -1), ('openintrest', -1) )
feed = CSV(dataname ="ETH_data.csv")
The CSV file I am using:
datetime,open,high,low,close 2020-05-04,209.73,210.29,195.2,206.65 2020-05-05,206.64,210.88,201.33,204.8 2020-05-06,205.58,210.76,198.61,198.88 2020-05-07,197.84,215.0,197.12,212.68 2020-05-08,213.01,216.25,207.12,211.6 2020-05-09,210.96,214.5,208.68,211.55 2020-05-10,210.0,210.0,174.43,188.27 2020-05-11,188.38,193.5,175.76,186.42 2020-05-12,185.94,192.06,185.94,189.9 2020-05-13,190.0,201.19,188.72,199.12 2020-05-14,200.1,205.97,196.06,203.1 2020-05-15,203.48,204.15,191.2,194.23 2020-05-16,193.41,203.27,193.41,200.51 2020-05-17,200.38,209.99,199.94,207.74 2020-05-18,208.35,217.01,208.03,214.49 2020-05-19,214.17,215.82,209.49,214.51 2020-05-20,214.46,215.6,205.96,210.0 2020-05-21,210.0,211.5,191.84,198.52
It's also weird that I can implement the CSV file with no problems using this method:
data = bt.feeds.GenericCSVData( dataname='ETH_data.csv', dtformat=('%Y-%m-%d'), datetime=0, high=2, low=3, open=1, close=4, volume=-1, openinterest=-1, )
but I would like to expand the file so this is not a practical solution.
Thanks in advance :)
vladisld last edited by
@zugba seems strange indeed. Are you sure exactly the same csv data file was used in both implementations ? The error suggests that there is a problem with the data itself.
@vladisld Yes I am certain that I am using the same CSV file
Here is the whole code
cerebro = bt.Cerebro() class CSV(bt.feeds.GenericCSVData): params = ( ('dtformat', '%Y-%m-%d'), ('datetime', 0), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', -1), ('openintrest', -1) ) data = bt.feeds.GenericCSVData( dataname='ETH_data.csv', dtformat=('%Y-%m-%d'), datetime=0, high=2, low=3, open=1, close=4, volume=-1, openinterest=-1, ) feed = CSV(dataname ="ETH_data.csv") cerebro.adddata(feed) #cerebro.addstrategy(strategy) cerebro.run() cerebro.plot(volume = False)
vladisld last edited by
@zugba hmm, currently the only way I could see is to debug the
_loadlinemethod in csvgeneric.py file ( setting the breakpoint upon IndexError exception raise may help) and see what values you get in
csvidx- this way you may get an idea where the wrong data line is coming from. Unless others may have a better idea of cause.
@zugba Could you show the last lines of the file (e.g. the output of
tail ETH_data.csvfrom the terminal)? Maybe there is a bad trailing line that is messing it up.
run-out last edited by
@zugba Thank you for your code. I copied and ran your code using both your data and a sample of backtrader data. I used both the standard csv loader and your custom class.
All ran smoothly with backtrader data. The only error was when I use your data with your custom class. This suggest an error in your data. Try using a different data source.
Here's the data I used. 2006-volume-day-001.txt
@run-out Thanks! I used a different data source and it worked
vcfriend last edited by
IndexError: list index out of range
The way Python indexing works is that it starts at 0, so the first number of your list would be . Index Error: List index out of range means you are trying to get an item in a list that doesn't exist. It means that you are referring to n-th element of the python list, while the length of the list is smaller than n. whenever you get this type of error please cross check with items that comes between/middle in range, and insure that their index is not last if you get output then you have made perfect error that mentioned.
An index in Python refers to a position within an ordered list . To retrieve an element of the list, you use the index operator () . Using indexing you can easily get any element by its position.