5.1 Using Indicators

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

Indicators 会在Strategy以及其他indicators中使用。

Indicators in action

init vs next

Example:

class MyStrategy(bt.Strategy):

    def __init__(self):

        sma1 = btind.SimpleMovingAverage(self.data)
        ema1 = btind.ExponentialMovingAverage()

        close_over_sma = self.data.close > sma1
        close_over_ema = self.data.close > ema1
        sma_ema_diff = sma1 - ema1

       buy_sig = bt.And(close_over_sma, close_over_ema, sma_ema_diff > 0)

    def next(self):

        if buy_sig:
            self.buy()

从上面的例子我们可以看出:

Indicator Plotting

First and foremost:

There is an auxiliary LinePlotterIndicator which plots such operations if wished with the following approach:

close_over_sma = self.data.close > self.sma
LinePlotterIndicator(close_over_sma, name='Close_over_SMA')

The name parameter gives name to the single line held by this indicator.

Controlling plotting

可在Indicator的创建中通过添加plotinfo变量(dict或OrderDict)来控制plot的相关信息。
也可以通过对indicator中plotinfo中的相关属性的进行赋值:
如:
myind = MyIndicator(self.data, someparam=value)
myind.plotinfo.subplot = True

或:
myind = MyIndicator(self.data, someparams=value, subplot=True)

Plotinfo中的相关参数:

上一篇 下一篇

猜你喜欢

热点阅读