1

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'
a7dc
  • 3,323
  • 7
  • 32
  • 50
  • 1
    Check if all your rows in csv are correct length. – h4z3 Apr 26 '22 at 08:17
  • If I set datetime=-1 I get a different error, so it's something to do with that I'm guessing? – a7dc Apr 26 '22 at 08:20
  • 1
    Could you have empty line(s) end of your file? And changing datetime to -1 doesn't neccessary be anyhow related to this. It may cause problem and terminate your software before it gets to the initial out-of-range problem. – ex4 Apr 26 '22 at 08:54

1 Answers1

1

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

Xu Qiushi
  • 1,111
  • 1
  • 5
  • 10