backtrader:绘图时,怎样让指标线叠加到k线上,而不是显示在独立图中

发布于 2021-10-10 11:38

有朋友在backtrader策略中,定义了一些指标,执行绘图时,发现指标线显示在k线下面的独立子图中,如下图所示,他想要指标叠加显示在k线上(即和k线显示在同一副子图中)。

其实,backtrader中可以对指标的绘图属性进行定制,下面的代码通过语句self.move_average.plotinfo.subplot = False,将移动均线显示叠加在k上,若设为True,则移动均线会显示在另一个单独的子图中。其他指标都可以模拟这种设置进行控制。

# 创建策略类
class SmaCross(bt.Strategy):
# 定义参数
params = dict(period=5 # 移动平均期数
)

def __init__(self):
# 移动平均线指标
self.move_average = bt.ind.MovingAverageSimple(
self.datas[0].close, period=self.params.period)
self.move_average.plotinfo.subplot = False
......

cerebro.plot()

plotinfo对象还有如下属性可以设置,对绘图进行更多控制。

  • plot: whether the object has to be plotted

  • subplot: whether to plot along the data or in an independent subchart. Moving Averages are an example of plotting over the data. Stochastic and RSI are examples of things plotted in a subchart on a different scale.

  • plotname: name to use on the chart instead of the class name. As in the example above mysma instead of SimpleMovingAverage

  • plotskip (deprecated): and old alias of plot

  • plotabove: whether to plot above the data. Else plot below. This has only effect if subplot=True

  • plotlinelabels: whether to plot the names of the individudal lines along the data in the legend on the chart when subplot=False
    Example: The Bollinger Bands have 3 lines but the indicator is plotted on top of the data. It seems sensible to have the legend only display a single name like BollingerBands rather than having the name of the 3 individual lines displayed (midtopbot)
    A use case for this is the BuySell observer for which it makes sense to display the name of the 2 lines and its markers: Buy and Sell to make it clear for the end user what is what.

  • plotlinevalues: controls whether the legend for the lines in indicators and observers has the last plotted value. Can be controlled on a per-line basis with _plotvalue for each line

  • plotvaluetags: controls whether a value tag with the last value is plotted on the right hand side of the line. Can be controlled on a per-line basis with _plotvaluetag for each line

  • plotymargin: margin to add to the top and bottom of individual subcharts on the graph
    It is a percentage but 1 based. For example: 0.05 -> 5%

  • plothlines: an iterable containing values (within the scale) at which horizontal lines have to be plotted.
    This for example helps for the classical indicators with overboughtoversold areas like the RSI which usually has lines plotted at 70 and 30

  • plotyticks: an iterable containing values (within the scale) at which value ticks have to specifically be placed on the scale
    For example to force the scale to have a 50 to identify the mid point of the scale. Although this seems obvious, the indicators use an auto-scaling mechanism and the 50 may not be obviously be in the centre if an indicator with a 0-100 scale moves between 30-95 on a regular basis.

  • plotyhlines: an iterable containing values (within the scale) at which horizontal lines have to be plotted.
    This can take over both plothlines and plotyticks.
    If none of the above are defined, then where to place horizontal lines and ticks will be entirely controlled by this value
    If any of the above are defined they have precedence over the values present in this option

  • plotforce: sometimes and thus the complex process of matching data feeds to indicators and bla, bla, bla … a custom indicator may fail to plot. This is a last resort mechanism to try to enforce plotting.
    Use it if all else fails

  • plotmaster: an Indicator/Observer has a master which is the data on which is working. In some cases plotting it with a different master may be wished needed.
    A use case is the PivotPoint indicator which is calculated on Monthly data but is meant for Daily data. It only makes sense to plot it on the daily data which is where the indicator makes sense.

  • plotylimited: currently only applies to data feeds. If True (default), other lines on the data plot don’t change the scale. Example: Bollinger Bands (top and bottom) may be far away from the actual absolute minimum/maximum of the data feed. With \plotlimited=True, those bands remain out of the chart, because the data controls the scaling. If set toFalse`, the bands affects the y-scale and become visible on the chart
    A use case is the PivotPoint indicator which is calculated on Monthly data but is meant for Daily data. It only makes sense to plot it on the daily data which is where the indicator makes sense.

  • 以上各个属性的默认值如下:

                plot=True,
subplot=True,
plotname='',
plotskip=False,
plotabove=False,
plotlinelabels=False,
plotlinevalues=True,
plotvaluetags=True,
plotymargin=0.0,
plotyhlines=[],
plotyticks=[],
plothlines=[],
plotforce=False,
plotmaster=None,
plotylimited=True,

本文来自网络或网友投稿,如有侵犯您的权益,请发邮件至:aisoutu@outlook.com 我们将第一时间删除。

相关素材