Backtrader 量化回测实践(5)—— Datafeed 增加列数据
1. 需求
在官网上的拓展示例是 GenericCSVData ,没有拓展pandas数据加载的示例,实际中数据库读取到pandas中,从pandas中加载到Cerebro中,需要解决pandas拓展Cerebro的数据加载的默认列。
from backtrader.feeds import GenericCSVData
class GenericCSV_PE(GenericCSVData):
# Add a 'pe' line to the inherited ones from the base class
lines = ('pe',)
# openinterest in GenericCSVData has index 7 ... add 1
# add the parameter to the parameters inherited from the base class
params = (('pe', 8),)
实际上需要是 PandasData 拓展列数据。
直接增加pandas的数据列:
Backtrader 使用 datafeed 加载数据,dataframe 格式如下:
Index([‘open’, ‘high’, ‘low’, ‘close’, ‘pre_close’, ‘pct_chg’, ‘volume’,
‘amount’, ‘turnover_rate’, ‘volume_ratio’],
dtype=‘object’)
加载数据报错: