参数 params
在XXXStrategy(bt.Strategy)中定义类变量 params,形如
params = (
('myparam', 27),
('exitbars', 5),
)
然后就可以在策略中使用self.params
addsize
cerebro.addsizer(bt.sizers.FixedSize, stake=10)
注:设置固定的赌注
指标
The backtrader platform assumes that the Strategy has the indicator in
place for a good reason, to use it in the decision making process. And
it makes no sense to try to make decisions if the indicator is not yet
ready and producing values.
注:指标(indicator)放在 __init__
里的原因是用它决策过程,如果指标没有准备好、没有产生值,就没有做决策的意义。
self.next
next will be 1st called when all indicators have already reached the minimum needed period to produce a value.
注:self.next 是当所有指标完成了最小的必要的周期的计算以后,第一个被调用的方法。
注册指标
Even if indicators are not explicitly added to a member variable of the strategy (like self.sma = MovingAverageSimple…), they will autoregister with the strategy and will influence the minimum period for next and will be part of the plotting.
注: 即使没有刻意的把指标赋给策略的成员变量,它们也会自动注册给策略。另外它们会影响调用self.next
因为最小周期的原因。另外它们也是绘图的一部分。
Let’s Optimize (优化)
Instead of calling addstrategy to add a stratey class to Cerebro, the call is made to optstrategy. And instead of passing a value a range of values is passed.
注:优化的时候,是用cerebro.optstrategy
代替cerebro.addstrategy
,另外添加参数maperiod=range(10, 31)