Backtrader 文档学习-Indicators混合时间周期
1.不同时间周期
如果数据源在Cerebro引擎中具有不同的时间范围和不同的长度,指示器将会终止。
比如:data0是日线,data1是月线 。
pivotpoint = btind.PivotPoint(self.data1)
sellsignal = self.data0.close < pivotpoint.s1
当收盘低于s1线(第一支撑位)时为卖出信号
PivotPoint可以在更大的时间范围内工作
在以前的版本报错:
return self.array[self.idx + ago]
IndexError: array index out of range
原因是:self.data.close提供第一个bar的值,但PivotPoint(以及s1行)只有在一个完整月过去后才会有值,相当于self.data0.close的22个值。在这22个close值,s1的Line还没有值,从底层数组获取它的尝试失败,报错超出范围。
Line对象支持(ago)运算符(Python中的__call__特殊方法)来传递自身的延迟版本:
close1 = self.data.close(-1)
In this example the object close1 (when accessed via [0]) always contains the previous value (-1) delivered by close. The syntax has been reused to accomodate adapting timeframes. Let’s rewrite the above pivotpoint snippet:
对象close1(通过[0]访问时)始终包含close提供的前一个值(-1)。语法将重写以适应时间框架。重写上面的pivotpoint 片段:
pivotpoint = btind.PivotPoint(self.data1)
sellsignal = self.data0.close < pivotpoint.s1()
看看()是如何在没有参数的情况下执行的(在后台没有提供任何参数)。发生了以下情况:
- pivotpoint.s1()返回内部LinesCoupler对象,该对象遵循较大范围周期,coupler用来自实际s1的最新值填充,从默认值NaN开始 。
在后面章节中的参数说明:
PivotPoint For