扩展通用CSV数据馈送 – backtrader中文教程
GitHub 中的问题实际上是推动完成文档部分或帮助我了解 backtrader 是否具有我从一开始就设想的易用性和灵活性以及沿途做出的决定。
在这种情况下是问题 #9。
这个问题似乎最终归结为:
open最终用户能否轻松扩展现有机制,以通过其他现有价格信息点(如、等)传递的线条形式添加额外信息high?
据我了解这个问题的答案是:是的
海报似乎有这些要求(来自Issue #6):
- 正在解析为 CSV 格式的数据源
- 用于
GenericCSVData加载信息此通用 csv 支持是针对此问题 #6开发的
- 一个额外的字段,显然包含需要在解析后的 CSV 数据中传递的市盈率信息
让我们以 CSV 数据馈送开发和通用 CSV 数据馈送示例帖子为基础。
脚步:
- 假设在解析的 CSV 数据中设置 P/E 信息
- 用作
GenericCSVData基类 - 用
pe - 添加参数让调用者判断P/E信息的列位置
结果:
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),)
工作完成了……
稍后以及在策略中使用此数据馈送时:
import backtrader as bt
....
class MyStrategy(bt.Strategy):
...
def next(self):
if self.data.close > 2000 and self.data.pe < 12:
# TORA TORA TORA --- Get off this market
self.sell(stake=1000000, price=0.01, exectype=Order.Limit)
...
绘制额外的市盈率线
数据馈送中的那条额外线显然没有自动绘图支持。
最好的选择是在该线上执行 SimpleMovingAverage 并将其绘制在单独的轴上:
import backtrader as bt
import backtrader.indicators as btind
....
class MyStrategy(bt.Strategy):
def __init__(self):
# The indicator autoregisters and will plot even if no obvious
# reference is kept to it in the class
btind.SMA(self.data.pe, period=1, subplot=False)
...
def next(self):
if self.data.close > 2000 and self.data.pe < 12:
# TORA TORA TORA --- Get off this market
self.sell(stake=1000000, price=0.01, exectype=Order.Limit)
...
评论被关闭。