IndexError: list index out of range backtrader

869 Views Asked by At

Sorry for the noob question!

I have a CSV that looks like:

date,volume,open,close,high,low
2020-11-12 13:38:00,100,1.85,1.85,1.85,1.85
2020-11-12 13:58:00,100,1.85,1.85,1.85,1.85
2020-11-12 14:03:00,100,1.85,1.85,1.85,1.85
...

And I'm trying to use the data with backtrader:

import backtrader as bt
# from strategies import AverageTrueRange

# Instantiate Cerebro engine
cerebro = bt.Cerebro()

data = bt.feeds.GenericCSVData(
    dataname='data/BBIG.csv',
    timeframe=bt.TimeFrame.Minutes,
    datetime=0,
    high=4,
    low=5,
    open=2,
    close=3,
    volume=1,
)

cerebro.adddata(data)

cerebro.run()

cerebro.plot(
    style='candlestick'
)

But I keep getting the following error:

IndexError: list index out of range backtrader

Any idea what I'm doing wrong?

Update:

If I change datetime to '-1' I get a different error:

ValueError: time data '1.85' does not match format '%Y-%m-%d %H:%M:%S'
1

There are 1 best solutions below

2
On

Try this, adding openinterest=-1 to GenericCSVData.

data = bt.feeds.GenericCSVData(
    dataname='data/BBIG.csv',
    timeframe=bt.TimeFrame.Minutes,
    datetime=0,
    high=4,
    low=5,
    open=2,
    close=3,
    volume=1,
    openinterest=-1,
)

I am not familiar with backtrader.

But I found the GenericCSVData in its source had the following code:

params = (
    ('nullvalue', float('NaN')),
    ('dtformat', '%Y-%m-%d %H:%M:%S'),
    ('tmformat', '%H:%M:%S'),

    ('datetime', 0),
    ('time', -1),
    ('open', 1),
    ('high', 2),
    ('low', 3),
    ('close', 4),
    ('volume', 5),
    ('openinterest', 6),
)

I just try to add openinterest=-1 and get the result.

I think the class need openinterest column in your BBIG.csv by default.

Besides, I got another problem ImportError Cannot import name 'warnings' from 'matplotlib.dates which was solved by https://stackoverflow.com/a/63974376/11004559