Backtrader 文档学习- Plotting
虽然回测是一个基于数学计算的自动化过程,还是希望实际通过可视化验证。无论是使用现有算法回测,还是观察数据驱动的指标(内置或自定义)。
凡事都要有人完成,绘制数据加载、指标、操作执行、现金和投资组合价值的演变,可以帮助人们更好地了解正在发生的事情,放弃/修改/创造的思考,查看图表的人对可视化信息做处理 。
这就是为什么backtrader使用matplotlib提供内置的图表功能。
1.How to plot
任何回测运行都可以通过调用方法来绘图:
cerebro.plot()
通常是BT程序执行的最后一个命令,就像下面简单的代码一样,它使用了来自backtrader源的样本数据之一。
from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader as bt class St(bt.Strategy): def __init__(self): self.sma = bt.indicators.SimpleMovingAverage(self.data) data = bt.feeds.BacktraderCSVData(dataname='../../datas/2005-2006-day-001.txt') cerebro = bt.Cerebro() cerebro.adddata(data) cerebro.addstrategy(St) cerebro.run() cerebro.plot()
图示:
图表包括3个观察者,如果没有任何交易,图示是没有意义的:
- CashValue 现金价值观察者在回测运行期间跟踪现金和组合总价值(包括现金)
- Trade 交易观察者,在交易结束时显示实际利润和损失(PNL)的
交易的定义是开仓并将头寸归零(直接或从多头转为空头或从空头转为多头) - BuySell 买卖观察器,它(在价格之上)标出发生买卖操作发生
cerebro = bt.Cerebro(stdstats=False)
或在运行时禁用
cerebro = bt.Cerebro()
…
cerebro.run(stdstats=False)
图示:
2.Plotted Elements
尽管前面的介绍中已经提到了Observers,但它们并不是绘制的唯一素。有三个对象绘图:
- 使用adddata、replaydata和resampledata向Cerebro添加数据加载
- 在策略级别声明的指标(或使用addindicator添加到cerebro中,为了实验目的,并将指标添加到虚拟策略中)
- 观察器直接添加到cerebro
观察者与策略对象Lines 同步运行,可以访问整个生态系统,能够跟踪现金和价值等变化。
3.Plotting Options
Indicators 和Observers 有几个选项可以控制在图表上的绘制方式。有3大组:
- 影响整个对象的绘图行为的选项
- 影响个别Lines 的绘图行为的选项
- 影响系统范围绘图的选项
4.Object-wide plotting options
控制指标和观察器中数据:
plotinfo = dict(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, )
在类定义中plotinfo为一个字典类型,但backtrader的类机制将其转换为一个可以继承的对象,甚至可以进行多重继承。作用:
- 如果子类将subplot=True之类的值更改为subplot=False,则类层次中更低一级的子类会,将后者作为subplot的默认值 。
参数赋值有两种方法。第一种方法的简单移动平均实例化:
sma = bt.indicators.SimpleMovingAverage(self.data, period=15, plotname='mysma')
从示例中可以看出,SimpleMovingAverage构造函数未使用的任何kwargs参数都将解析(如果可能的话),做为plotinfo 对象参数值。SimpleMovingAverage定义了参数period。意味着plotname将与plotinfo中的同名参数一致。
第二种方法:
sma = bt.indicators.SimpleMovingAverage(self.data, period=15) sma.plotinfo.plotname = 'mysma'
绘图选项:
- plot: 是否绘制对象
- subplot: 是否在数据上绘制或在独立的子图中绘制。移动平均线是在数据上绘制的示例。随机指标和相对强弱指标是在不同比例尺上的子图中绘制的示例。
- plotname: 在图表上使用的名称,而不是类名称。例如,上面的示例中使用mysma而不是SimpleMovingAverage
- plotskip(已弃用):plot的旧别名
- plotabove: 是否在数据上方绘制。否则在下方绘制。这仅在subplot=True时有效
- plotlinelabels: 是否在图表上的图例中绘制单个线的名称
示例:布林带有3条线,但指标是在数据上绘制的。将只显示单个名称BollingerBands,而不是显示3个单独线的名称(mid,top,bot),这似乎是合理的。
这种情况的用例是BuySell观察器,对于它来说,显示2条线和其标记的名称是有意义的:Buy和Sell,以便最终用户清楚地知道是什么。 - plotymargin: 添加到图表上各个子图的顶部和底部的边距
它是一个百分比,但基于1。例如:0.05-> 5% - plothlines: 包含在比例尺内的值(在比例尺内)的可迭代对象,在这些值处必须绘制水平线。
这有助于经典指标的超买,超卖区域,例如通常在RSI上绘制的线通常在70和30处绘制 - plotyticks: 包含在比例尺内的值(在比例尺内)的可迭代对象,在这些值处必须在比例尺上特别放置值刻度
例如,强制比例尺具有50以标识比例尺的中点。尽管这似乎很明显,但指标使用自动缩放机制,如果具有0-100比例尺的指标在30-95之间定期移动,则50可能不明显位于中心。 - plotyhlines: 包含在比例尺内的值(在比例尺内)的可迭代对象,在这些值处必须绘制水平线。
这可以接管plothlines和plotyticks。
如果没有定义上述任何一个,则水平线和刻度线的放置位置将完全由此值控制
如果定义了其中任何一个,则它们优先于此选项中存在的值 - plotforce : 有时,由于匹配数据提要到指标等复杂过程,自定义指标可能无法绘制。这是一种最后的手段机制,尝试强制绘图。
如果所有其他方法都失败,请使用它 - plotmaster : Indicator/Observer有一个主人,即正在使用的数据。在某些情况下,可能希望需要使用不同的主人来绘制它。
一个用例是PivotPoint 指标,它是在月度数据上计算的,但是适用于每日数据。它只有在每日数据上绘制才有意义,这是指标有意义的地方。
5.Line specific plotting options
Indicators/Observers 具有线,如何绘制这些线可以通过plotlines 对象进行设置。大多数在plotlines 中指定的选项都是直接传递给matplotlib 进行绘制的。因此,文档依赖于已完成的事情的示例。
重要提示: 选项是按Lines指定的。
其中一些选项由backtrader直接控制。这些都以下划线( _ )开头:
- _plotskip (布尔值),如果设置为True ,则表示
- _plotskip (布尔值),如果设置为True ,则表示
- _plotvalue (布尔值)控制图例是否包含最后一个绘制的值(默认为True)
- _plotvaluetag (布尔值)控制是否在右侧绘制一个带有最后一个值的标签(默认为True)
- _name (字符串)更改特定线的绘图名称
- _skipnan (布尔值,默认值:False):在绘制时跳过NaN值,允许例如在指标生成的两个远离的点之间绘制一条线,其中所有中间值都为NaN(新创建的数据点的默认值)
- _samecolor (布尔值)这将强制下一行具有与上一行相同的颜色,避免matplotlib默认机制循环遍历每个新绘制的素的颜色映射
- _method (字符串)选择matplotlib将用于素的绘图方法。如果未指定,则选择最基本的绘图方法。
例如来自MACDHisto。这里的histo线以行业事实标准的条形图形式绘制。可以在MACDHisto的定义中找到以下定义:
lines = ('histo',) plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0))
alpha 和width是matplotlib的选项
- _fill_gt / _fill_lt 允许在给定线和以下线或数字值之间进行填充:
- 另一条线
- 数字值
参数是一个包含2个素的可迭代对象,其中:
- 第一个参数
是参考线的名称(字符串)或数字值
填充将在自身值和线或数字值的值之间进行 - 第二个参数是:
带有颜色名称(matplotlib兼容)或十六进制规范的字符串(请参见matloplit示例)
或
一个可迭代对象,其中第一个素是颜色的字符串/十六进制值,第二个素是指定alpha透明度的数字值(默认值:0.20,由绘图方案中的fillalpha控制)
示例:
# Fill for myline when above other_line with colour red plotlines = dict( myline=dict(_fill_gt('other_line', 'red')) ) # Fill for myline when above 50 with colour red plotlines = dict( myline=dict(_fill_gt(50, 'red)) ) # Fill for myline when above other_line with colour red and 50% # transparency (1.0 means "no transparency") plotlines = dict( myline=dict(_fill_gt('other_line', ('red', 0.50))) )
6.Passing options to a not yet known line
使用名称_X,其中X代表从零开始的索引中的数字。这意味着选项适用于第X行
- 来自OscillatorMixIn的用例:
plotlines = dict(_0=dict(_name='osc'))
7.Some plotlines examples
BuySell 观察者具有以下内容:
plotlines = dict( buy=dict(marker='^', markersize=8.0, color='lime', fillstyle='full'), sell=dict(marker='v', markersize=8.0, color='red', fillstyle='full') )
... lines = ('pnlplus', 'pnlminus') ... plotlines = dict( pnlplus=dict(_name='Positive', marker='o', color='blue', markersize=8.0, fillstyle='full'), pnlminus=dict(_name='Negative', marker='o', color='red', markersize=8.0, fillstyle='full') )
lines = ('drawdown', 'maxdrawdown',) ... plotlines = dict(maxdrawdown=dict(_plotskip='True',))
plotlines = dict( mid=dict(ls='--'), top=dict(_samecolor=True), bot=dict(_samecolor=True), )
lines = ('percK', 'percD',) ... plotlines = dict(percD=dict(_name='%D', ls='--'), percK=dict(_name='%K'))
较慢的线percD 以虚线样式绘制。和名字的线被更改以包括花式% 符号( %K 和%D ),这不能在Python中使用名称定义
8.Methods controlling plotting
当处理指标和观察者时,支持以下方法以进一步控制绘图:
- _plotlabel (self)
应返回一个列表,其中包含将放置在指标或观察者名称括号中的标签的内容
来自RSI指标的示例:
def _plotlabel(self): plabels = [self.p.period] plabels += [self.p.movav] * self.p.notdefault('movav') return plabels
此方法返回:
- 一个int,表示为RSI配置的周期,如果默认移动平均线已更改,则为特定类
在后台,两者都将转换为字符串。在类的情况下,将输出类的名称,而不是完整的module.name组合。 - _plotinit (self)
在绘图开始时调用,以执行指标可能需要的任何特定初始化。再次,来自RSI的示例:
def _plotinit(self): self.plotinfo.plotyhlines = [self.p.upperband, self.p.lowerband]
9.System-wide plotting options
首先是cerebro中plot 的signature:
def plot(self, plotter=None, numfigs=1, iplot=True, kwargs):
意味着:
plotter :包含控制系统范围绘图的选项的对象/类
如果传递None ,则将实例化默认的PlotScheme 对象(请参见下文)
numfigs :独立图表中将绘图分解的数量
有时图表包含太多bar,如果在单个图中打包,将不容易阅读。这要求将它分解为尽可能多的独立图
iplot :如果在Jupyter Notebook 中运行,则自动绘制内联
kwargs :args将用于更改属性的值plotter ,或如果没有对象传递plotter 则创建默认的PlotScheme 。
10.PlotScheme
对象包含控制系统范围绘图的所有选项,所有选项记录在代码中:
class PlotScheme(object): def __init__(self): # to have a tight packing on the chart wether only the x axis or also # the y axis have (see matplotlib) self.ytight = False # y-margin (top/bottom) for the subcharts. This will not overrule the # option plotinfo.plotymargin self.yadjust = 0.0 # Each new line is in z-order below the previous one. change it False # to have lines paint above the previous line self.zdown = True # Rotation of the date labes on the x axis self.tickrotation = 15 # How many "subparts" takes a major chart (datas) in the overall chart # This is proportional to the total number of subcharts self.rowsmajor = 5 # How many "subparts" takes a minor chart (indicators/observers) in the # overall chart. This is proportional to the total number of subcharts # Together with rowsmajor, this defines a proportion ratio betwen data # charts and indicators/observers charts self.rowsminor = 1 # Distance in between subcharts self.plotdist = 0.0 # Have a grid in the background of all charts self.grid = True # Default plotstyle for the OHLC bars which (line -> line on close) # Other options: 'bar' and 'candle' self.style = 'line' # Default color for the 'line on close' plot self.loc = 'black' # Default color for a bullish bar/candle (0.75 -> intensity of gray) self.barup = '0.75' # Default color for a bearish bar/candle self.bardown = 'red' # Level of transparency to apply to bars/cancles (NOT USED) self.bartrans = 1.0 # Wether the candlesticks have to be filled or be transparent self.barupfill = True self.bardownfill = True # Wether the candlesticks have to be filled or be transparent self.fillalpha = 0.20 # Wether to plot volume or not. Note: if the data in question has no # volume values, volume plotting will be skipped even if this is True self.volume = True # Wether to overlay the volume on the data or use a separate subchart self.voloverlay = True # Scaling of the volume to the data when plotting as overlay self.volscaling = 0.33 # Pushing overlay volume up for better visibiliy. Experimentation # needed if the volume and data overlap too much self.volpushup = 0.00 # Default colour for the volume of a bullish day self.volup = '#aaaaaa' # 0.66 of gray # Default colour for the volume of a bearish day self.voldown = '#cc6073' # (204, 96, 115) # Transparency to apply to the volume when overlaying self.voltrans = 0.50 # Transparency for text labels (NOT USED CURRENTLY) self.subtxttrans = 0.66 # Default font text size for labels on the chart self.subtxtsize = 9 # Transparency for the legend (NOT USED CURRENTLY) self.legendtrans = 0.25 # Wether indicators have a leged displaey in their charts self.legendind = True # Location of the legend for indicators (see matplotlib) self.legendindloc = 'upper left' # Plot the last value of a line after the Object name self.linevalues = True # Plot a tag at the end of each line with the last value self.valuetags = True # Default color for horizontal lines (see plotinfo.plothlines) self.hlinescolor = '0.66' # shade of gray # Default style for horizontal lines self.hlinesstyle = '--' # Default width for horizontal lines self.hlineswidth = 1.0 # Default color scheme: Tableau 10 self.lcolors = tableau10 # strftime Format string for the display of ticks on the x axis self.fmt_x_ticks = None # strftime Format string for the display of data points values self.fmt_x_data = None
11.Colors in PlotScheme
PlotScheme 类定义了一个可以在子类中覆盖的方法,该方法返回要使用的下一个颜色:
def color(self, idx)
其中idx 是正在绘制的行的当前索引单个子图上。例如, MACD 绘制3条线,因此idx 变量只有以下值: 0 , 1 和2 。下一个图表
(可能是另一个指标)将在0 处重新开始计数。
backtrader中使用的默认颜色方案使用(如上所示), Tableau 10 Color Palette ,索引修改为:
tab10_index = [3, 0, 2, 1, 2, 4, 5, 6, 7, 8, 9]
今天的文章
Backtrader 文档学习- Plotting分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/103472.html