I'm trying to load a custom data feed from a csv file that I've generated called "impact.csv" through the script below "backtrader_impact.py".
However, at "cerebro.adddata(data)", I receive an IndexError: list index out of range.
File "C:\...\backtrader\feeds\csvgeneric.py", line 151, in _loadline
csvfield = linetokens[csvidx]
IndexError: list index out of range
backtrader_impact.py:
import backtrader as bt
class ImpactCSVData(bt.feeds.GenericCSVData):
lines = ('impact_score',)
params = (
('dtformat', '%Y/%m/%d %H:%M:%S'), # Datetime format
('datetime', 0), # Index of the datetime column in the CSV
('impact_score', 1), # Index of the impact_score column in the CSV
('timeframe', bt.TimeFrame.Minutes), # Timeframe (adjust accordingly)
)
data = ImpactCSVData(
dataname='alpaca_backtrader/impact.csv', # Path to your CSV file
)
cerebro = bt.Cerebro()
cerebro.adddata(data)
# cerebro.addstrategy(YourStrategy)
cerebro.run()
# cerebro.plot()
impact.csv:
datetime,impact_score
2023/01/13 00:00:00,96
2023/01/13 00:15:00,75
2023/01/13 00:30:00,58
I've tried a bit of debugging by adding some print statements in backtrader\feeds\csvgeneric.py (in backtrader package itself):
for linefield in (x for x in self.getlinealiases() if x != 'datetime'):
# Get the index created from the passed params
csvidx = getattr(self.params, linefield)
if csvidx is None or csvidx < 0:
# the field will not be present, assignt the "nullvalue"
print(f"Processing line tokens: {linetokens}, with csvidx: {csvidx}")
csvfield = self.p.nullvalue
else:
# get it from the token
# FIXME: failing here. trying to access index 4 of list of 2 items
print(f"Processing line tokens: {linetokens}, with csvidx: {csvidx}")
csvfield = linetokens[csvidx]
if csvfield == '':
# if empty ... assign the "nullvalue"
print(f"Processing line tokens: {linetokens}, with csvidx: {csvidx}")
csvfield = self.p.nullvalue
What is printed:
Processing line tokens: ['2023/01/13 00:00:00', '96'], with csvidx: 4
For some reason it is trying to access index 4 of linetokens that is just ['2023/01/13 00:00:00', '96'] with a length of 2.
Would appreciate your help! Thank you.