4.2 Strategy with Signals

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

除了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

Signals indications

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

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

Signals Types

** Main Grpup **:

Exit Group

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

Accumulation and Order Concurrency

To avoid this the default behavior is:

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

上一篇下一篇

猜你喜欢

热点阅读