如何将backtrader csv reader转换为backtrader datafeed?我尝试过:
尝试1 :(用GenericCSV替换数据Feed)
all_data=bt.feeds.GenericCSVData(
#my csv params here
)
for s, df in all_data.items(): #THIS LINE READS IN CSV AND ERRORS
#do stuff
“ Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst”对象没有属性“ items”
尝试2 :(将GenericCSV转换为Datafeed)
all_data=bt.feeds.GenericCSVData(
#my csv params here
)
all_datafeed = bt.feeds.PandasData(dataname=all_data)
错误:“ Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst”对象没有属性“ columns”
尝试3 :(读取csv并转换为数据供稿)
df=pd.read_csv('/home/abc/EUR_USD.csv',header=0,parse_dates=True)
all_datafeed = bt.feeds.PandasData(dataname=df)
for df in all_datafeed.items():
print(df)
“ Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst”对象没有属性“ items”
csv摘录:
time,oask,hask,lask,cask,obid,hbid,lbid,cbid,volume
2002-05-06 20:00:00 UTC,0.9184,0.9184,0.9181,0.9184,0.9181,0.9181,0.9181,0.9181,1
2002-05-07 20:00:00 UTC,0.9155,0.9155,0.9152,0.9155,0.9152,0.9152,0.9152,0.9152,1
2002-05-08 20:00:00 UTC,0.9045,0.9045,0.9042,0.9045,0.9042,0.9042,0.9042,0.9042,1
答案 0 :(得分:0)
# Create a Data Feed
data = bt.feeds.GenericCSVData(
dataname='filepath.csv',
fromdate=datetime.datetime(2018, 1, 1),
todate=datetime.datetime(2018, 12, 31),
nullvalue=0.0,
dtformat=('%Y-%m-%d'),
datetime=0,
open = 1,
high = 2,
low = 3,
close = 4,
volume =5,
openinterest=-1,
reverse=False)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
尽管这是您要完成的任务,但我不确定。
答案 1 :(得分:0)
您需要做的就是创建一个数据供稿,并将其传递给cerebro
。
我遇到了同样的问题,并通过注意以下问题解决了该问题:
datetime
的格式为%Y.%m.%d hh:mm:ss
。因此,在示例中,应使用以下内容将-
替换为.
,然后保存数据框:
import pandas as pd
df = pd.read_csv("your_csv_file.csv")
df.index = df.index.map(lambda datetime: datetime.replace("-", ".")
df.to_csv("your_csv_file.csv")
GenericCSVData
的数字是列的索引(从0开始,从左到右)| col_0 | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 | col_7 |
# in my example:
| datetime | symbol | open | high | low | close | volume btc | volume usd |
因此应按如下所示添加它:
#...
data = bt.feeds.GenericCSVData(
dataname="./dataset/btc.csv",
dtformat=('%Y.%m.%d %H:%M:%S'),
datetime=0,
open=2,
high=3,
low=4,
close=5,
)
cerebro.adddata(data)
#...