backtrader中Indicator是在回测中进行行情指标编写的重要模块。由于针对性的介绍文章较少,因此在学习及应用的过程中经常需要不断回看backtrader文档来进行回顾。本文对Indicator进行简要介绍。
Indicator的基本定义与性质
Indicator即指标,常用的有简单移动平均(SMA),指数移动平均(EMA),MACD,RSI等等。在backtrader 中,Indicator常在Strategy模块中进行定义,或作为其他的indicator的indicator出现。在backtrader的数据流中,Indicator是具有特别性质的line对象。
在backtrader中,Indicator与其他line对象不同,具有自身的一些独特性质:
•Indicator可以作为数据源传给其他的Indicator进行计算
•Indicator没有时间属性(非时间序列),因此可以同时作用于不同周期的数据源
•Indicator计算结果是幂等的
•Indicator的只输出于当前时间点,不可进行回溯输出
•Indicator可定义自身的可视化属性,并在backtrader回测结果中展示
Indicator可以简单的理解为指标计算的工具。然而实际上,在backtrader的体系中,对历史数据切片进行分析的算法,如果不需要进行回溯输出,都可以写成Indicator。
Indicator常用的重要代码块
1.backtrader目录下indicator.py中的Indicator类是backtrader中所有技术指标类的基类,使定义技术指标的类能够被backtrader系统识别为Indicator,在系统中以Indicator 的性质进行计算以及可视化。在自定义Indicator时,常直接将其引为父类。
2.backtrader目录下的Indicators目录中:
•basicops.py 定义了关许多指标的基本算法类
•许多经典技术指标的源码文件,如macd.py, sma.py, ols.py, rsi.py等等
backtrader文档中Indicator的相关文章:
文档说明[1]
Recursive Indicator(递归指标), 以EMA为例子[2]
Dynamic Indicator(动态指标), 以动态最高价为例[3]
References
[1] 文档说明: https://backtrader.com/docu/induse/
[2] Recursive Indicator(递归指标), 以EMA为例子: https://backtrader.com/blog/posts/2018-01-27-recursive-indicators/recursive-indicator/
[3] Dynamic Indicator(动态指标), 以动态最高价为例: https://backtrader.com/blog/posts/2018-02-06-dynamic-indicator/dynamic-indicator/