美文网首页
4.2 Strategy with Signals

4.2 Strategy with Signals

作者: wanggs66 | 来源:发表于2020-04-23 19:50 被阅读0次

    除了Strategy之外,Backtrader还提供了Siganls这种方式用于策略回测。用户通过add Siganls之后,其他的完全交给后台执行。
    Example:

    class MySignal(bt.Indicator):
        lines = ('signal',)
        params = (('period', 30),)
    
        def __init__(self):
            self.lines.signal = self.data -   bt.indicators.SMA(period=self.p.period)
    
    
    import backtrader as bt
    
    data = bt.feeds.OneOfTheFeeds(dataname='mydataname')
    cerebro.adddata(data)
    cerebro.add_signal(bt.SIGNAL_LONGSHORT, MySignal)
    cerebro.run()
    

    Initial FAQ

    • How is the volume of buy/sell operations determined?
      A cerebro instance adds automatically a FixedSize sizer to strategies. The end user can change the sizer to alter the policy with cerebro.addsizer

    • How are orders executed?
      The execution type is Market and the validity is Good Until Canceled.

    Signals indications

    The signals delivers indications when queried with signal[0] and the meaning is:

    • 0 -> long indication

    • < 0 -> short indication

    • == 0 -> No indication

    The example does simple arithmetic with self.data - SMA and:

    • Issues a long indication when the data is above the SMA

    • Issues a short indication when the data is below the SMA

    Signals Types

    ** Main Grpup **:

    • LONGSHORT: 同时包含long和short方向的信号,long多头直接转向short空头,资金一直在市场中

    • LONG:

      • long indications are taken to go long
      • short indications are taken to close the long position(当signal值大于0时,进入多头,小于0时推出多头). But :
        • if a LONGEXIT signal is in the system it will be used to exit the long.(LONGEXIT也会让策略退出多头)
        • if a SHORT signal is available and no LONGEXIT is available, it will be used to close a long before a short.(SHORT可以使策略退出多头并创建空头)
    • SHORT:

      • short indications are taken to go short
    • long indications are taken to close the short position. But:

      • If a SHORTEXIT (see below) signal is in the system it will be used to exit the short(SHORTEXIT 可以使策略退出空头,类似于 long indications)

      • If a LONG signal is available and no SHORTEXIT is available , it will be used to close a short before opening a long(LONG 可以使策略退出空头并创建多头)

    Exit Group

    This 2 signals are meant to override others and provide criteria for exitins a long / short position

    • LONGEXIT: short indications are taken to exit long positions

    • SHORTEXIT: long indications are taken to exit short positions

    Accumulation and Order Concurrency

    • Accumulation: even if already in the market, the signals would produce new orders which would increase the possition in the market 可以在持有多头的同时继续增加多头仓位

    • Concurrency: new orders would be generated without waiting for the execution of other orders 订单可以被加入订单队列,即使存在未执行的订单

    To avoid this the default behavior is:

    • To Not Accumulate

    • To Not allow Concurrency

    Should any of these two behaviors be wished, this can be controlled via cerebro with:

    • cerebro.signal_accumulate(True) (or False to re-disable it)

    • cerebro.signal_concurrency(True) (or False to re-disable it)

    相关文章

      网友评论

          本文标题:4.2 Strategy with Signals

          本文链接:https://www.haomeiwen.com/subject/yxcnihtx.html