美文网首页
5.1 Using Indicators

5.1 Using Indicators

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

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

Indicators in action

  • Indicators在Strategy中的init中初始化,并且会在next调用之前提前计算好
  • Indicators 的值在next中被使用

init vs next

  • init中对lines进行相关的操作会产生其他的lines对象
  • 在next中涉及lines对象的操作往往是两个lines中某个时点值的操作,最后会得到一个常规的python类型的值,如float或bools.
    【注】当逻辑比较复杂或涉及多个操作时,把这些逻辑和计算封装到Indicator中是一种比较好的选择。

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()

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

  • 在backtrader中, Indicator 不需要从父类中集成参数或者通过什么注册的方法来生成,直接通过简单的操作(如: sma - ema)即可产生
  • ExponentialMovingAverage 不传递参数的时候,默认的是使用的父类(或者说主环境)中的第一个data.

Indicator Plotting

First and foremost:

  • Declared Indicators get automatically plotted (if cerebro.plot is called)
  • lines objects from operations DO NOT GET plotted (like close_over_sma = self.data.close > self.sma)

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中的相关参数:

  • plot (default: True): 是否画图
  • subplot(default): 是否在一个子串口中展示indicator,部分indicator如moving average该参数默认为False
  • plotname(default: ' '): 画图时使用的名称,默认使用class.name,并注意python中的操作符不能作为名称使用。
  • plotabove(default: False) : 是否将Indicator画在data图之上
  • plotlinelabels(default: False): 是否展示line的名称,如我们计算了RSI的SimpleMovingAverage,通常展示的名称为SimpleMovingAverage,如果将此参数设置为True的话,则会显示底层line的名称(RSI)
  • plotymargin(default: 0.0):设定与上下界的距离(丹玉等于0,小于1的小数表示)
  • plotyticks(default: []): 用于控制是否展示y坐标,如果是[]的话,会自动计算一个,也可以通过设置为[20.0, 50.0, 80.0]的方式进行设置
  • plothlines(default: []): 是否在indicator
    坐标轴上画一些水平线,如果为[], 则不画,如果为[20.0, 80.0]则画两条,还可通过upperband和lowerband参数进行设置
  • plotylines(default: [])类似于plothines 和plotyticks
  • plotforce(default: False): 如果某些指标你认为必须展示而没有在图片中展示出来,你可以在万不得已的情况下将此参数设置为True

相关文章

网友评论

      本文标题:5.1 Using Indicators

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