一、核心组件
(1)数据加载(Data Feed):将交易策略的数据加载到回测框架中。
(2)交易策略(Strategy) strategy.py:该模块是编程过程中最复杂的部分,需要设计交易决策,得出买入/卖出信号。
(3)回测框架设置( Cerebro)cerebro.py:需要设置(i)初始资金(ii)佣金(iii)数据馈送(iv)交易策略(v)交易头寸大小。
(4)运行回测:运行Cerebro回测并打印出所有已执行的交易。
(5)评估性能(Analyzers):以图形和风险收益等指标对交易策略的回测结果进行评价。
二、策略的生命周期
1.Conception: init
任何类在生成的时候都是先调用这一初始化构造函数
This is obviously invoked during instantiation: indicators will be created here and other needed attribute. Example:
def __init__(self):
self.sma = btind.SimpleMovingAverage(period=15)
2.Birth: start
start方法在cerebro告诉strategy,是时候开始行动了,也就是说,通知策略激活的时候被调用。
The world (cerebro) tells the strategy is time to start kicking. A default empty method exists.
3.Childhood: prenext
有些技术指标,比如我们提到的MA,存在一个窗口,也就是说,需要n天的数据才能产生指标,那么在没有产生之前呢?这个prenext方法就会被自动调用。
indicators declared during conception will have put constraints on how long the strategy needs to mature: this is called the minimum period. Above init created a SimpleMovingAverage with a period=15.As long as the syst