GitHub 中的问题实际上是在推动文档部分的完成,或者説明我了解我是否backtrader 具有我从一开始就设想的易用性和灵活性以及在此过程中做出的决定。
在本例中为问题 #9。
这个问题最终似乎可以归结为:
- 最终使用者是否可以轻松地扩展现有机制,以 lines 的形式添加额外的资讯,这些资讯会传递到其他现有的价格资讯点,如
open、high等?
据我所知,问题的答案是:是的
海报似乎有以下要求(来自第6期):
-
正在解析为 CSV 格式的数据来源
-
用于
GenericCSVData载入资讯此通用 csv 支援是为回应此问题#6 而开发的
-
一个额外的栏位,显然包含需要沿着解析的CSV数据传递的P / E资讯
让我们以 CSV Data Feed 开发和 GenericCSVData 示例帖子为基础。
步骤:
-
假设在解析的CSV数据中设置了P / E资讯
-
用作
GenericCSVData基类 -
扩展存在 lines (open/high/low/close/volumen/openinterest)
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),)
工作已经完成...
稍后,在策略中使用此 data feed 时:
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)
...
绘制额外的市盈率 line
显然,在data feed中没有对额外line的自动绘图支援。
最好的替代方案是在该 line 上执行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)
...