4.1 Strategy

2020-04-22  本文已影响0人  wanggs66

Strategy 是backtrader的核心,是策略逻辑实现部分。
Strategy生命周期由methods构成:

  1. Conception: init
    Strategy初始化时调用该方法,indicators以及其他一些属性可以在此实现:
    Example:

    def init(self):
    self.sma = btind.SimpleMovingAverage(period=15)

  2. Birth: start
    Tells the strategy to start.

  3. Childhood: prenext
    在指标的构造过程中有一些限制(一定数量的数据),通常指标构造过程中存在一个最小期限的概念,如上面init中SimpleMovingAverage中设定的period
    当系统中的bars小于15时,会执行prenext方法(默认实现为不做任何操作)

  4. Adulthood: next
    一旦系统达到minum period,系统有足够的的缓存数据开始产生指标数据,strategy足够成熟开始执行。
    在从prenext 到next之间会执行nextstart 操作,用于标识切换过程,一般默认prenext就是next方法

  5. Reproduction None
    Strategies 通常不会真的重复运行,通常是在optimizing操作中设定不同的参数时起作用。

  6. Death: stop
    系统告诉strategy重置时间到,可以开始整理系统中的相关结果和数据,默认什么都不操作。

一些提醒消息的方法:

See Cerebro for an explanation on the store notifications. These will delivered to the strategy even if they have also been delivered to a cerebro instance (with an overriden notify_store method or via a callback)

在next中一些下单函数:

How to Buy/Sell/Close

Buy 和 Sell 会产生orders,产生Order(或子类)的实例,该实例可以作为该订单的一个引用,ref是该订单的唯一标识。

Parameters:

上一篇 下一篇

猜你喜欢

热点阅读