量化框架backtrader之一文读懂可视化

简介

回测时往往想知道策略中间的运行情况,虽然可以通过最后的统计指标来一窥端倪,但对回测过程进行可视化是最符合人性的,同时通过观察回测过程也可以更好的设计&优化量化策略。Backtrader使用matplotlib库提供可视化能力

使用方法

Backtrader数据可视化非常简单,只需在run()之后调用plot()方法即可。

cerebro.run()
cerebro.plot()

plot(self, plotter=None, numfigs=1, iplot=True, **kwargs) 方法各参数含义如下:

  • plotter: 包含绘图属性的PlotScheme及其派生类对象。默认为None,如果为None,则默认的PlotScheme对象会被实例化

  • numfigs :将图形拆分成多幅图展示,默认为1

  • iplot : 在Jupyter Notebook运行则是否自动 plot inline,默认为True。如果不在jupyter中运行,该参数最好设置为False,否则容易出问题

  • *kwargs:args参数用于改变plotter属性值

可以通过两种办法来系统性控制可视化配置:

  1. 直接通过设置plot()方法的args参数,如下所示。

cerebro.plot(iplot=False,
             style='candel',  # 设置主图行情数据的样式为蜡烛图
             plotdist=0.1,    # 设置图形之间的间距
             barup = '#ff9896', bardown='#98df8a', # 设置蜡烛图上涨和下跌的颜色
             volup='#ff9896', voldown='#98df8a', # 设置成交量在行情上涨和下跌情况下的颜色
            )

2. 自定义 PlotScheme 类修改对应的参数

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

PlotScheme类定义了一个color(self, idx) 方法返回将要使用的颜色,子类可以重载,其idx参数为要绘制的line的当前index。 如MACD 绘制3条线,idx变量有0,1和2共3个值,新的指标idx会从0重新开始。默认的color scheme是Tableau 10 Color Palette ,对应的index是tab10_index = [3, 0, 2, 1, 2, 4, 5, 6, 7, 8, 9] 。可以通过在自定义 PlotScheme类重载color()方法或传递 lcolors 变量给plot 方法来改变要使用的颜色。

def color(self, idx):
        colidx = tab10_index[idx % len(tab10_index)]
        return self.lcolors[colidx]

可视化组件

Backtrader支持3大部分组件的可视化:

  • Data feeds数据源:通过 adddata、replaydata和resampledata方法导入cerebro的原始数据

  • Indicators指标:在策略类中声明或者通过 addindicator 添加的指标

  • Observers观测器对象:通过addobserver添加的观测器,如Cash和Value对象

在绘制图形时,默认是将data feeds数据源绘制在主图上,Indicators指标有的与 Data Feeds数据源一起绘制在主图上,比如均线,有的则以子图形式绘制;observers 通常绘制在子图上

可视化选项

除了上面说的通过plot()参数和自定义PlotScheme来系统控制可视化选项外,Indicators指标和Observers观测器有一些选项可以控制其绘图形式,一共有3种类型:

  • Object对象级的可视化选项 —可以影响整个对象的绘制行为,由plotinfo来控制

plotinfo = dict(plot=True, # 是否绘制
                subplot=True, # 是否绘制成子图
                plotname='', # 图形名称
                plotabove=False, # 子图是否绘制在主图的上方
                plotlinelabels=False, # 主图上曲线的名称
                plotlinevalues=True,
                plotvaluetags=True,
                plotymargin=0.0,
                plotyhlines=[],
                plotyticks=[],
                plothlines=[],
                plotforce=False,
                plotmaster=None,
                plotylimited=True,
           )

有2种方法来访问plotinfo的属性,如下所示:

# 通过参数来设置
sma = bt.indicators.SimpleMovingAverage(self.data, period=15, plotname='mysma')

# 通过属性来设置
sma = bt.indicators.SimpleMovingAverage(self.data, period=15)
sma.plotinfo.plotname = 'mysma'
  • Line线相关的可视化选项 — 可以使用plotlines对象来控制lines对象的绘图行为,plotlines中的选项会在绘图时直接传给matplotlib,如下所示。

lines = ('histo',)
plotlines = dict(histo=dict(_method='bar', alpha=0.50, width=1.0))
  • 方法控制的可视化 — 当处理indicator指标和observer观测器时,_plotlabel(self), _plotinit(self)方法可以进一步控制可视化

结论 & 交流

关注微信公众号:诸葛说talk,获取更多内容。同时还能获取邀请加入量化投资群, 与众多投资爱好者、量化从业者、技术大牛一起交流、切磋,在实战中快速提升自己的投资水平。

写文章不易,觉得本文对你有帮助的话,帮忙点个在看吧。

参考

### Backtrader 集成 Matplotlib 示例 在金融数据分析量化交易领域,`backtrader` 是一个强大的回测框架,而 `matplotlib` 则是一个广泛使用的绘图库。两者结合能够帮助用户更直观地展示交易策略的表现。 为了使 `backtrader` 能够利用 `matplotlib` 进行可视化操作,在命令行参数中加入特定选项即可完成基本配置[^1]: ```python %matplotlib inline runstrat('--plot'` 参数来激活内置的图表绘制功能,并指定要显示的技术指标(此处为 KAMA)。这使得每次运行回测时都会自动生成相应的图形化输出。 对于更加复杂的定制需求,则可以通过创建实例对象的方式进一步控制图表属性。例如调整子图布局等细节设置[^2]: ```python myind = MyIndicator(self.data, someparam=value) myind.plotinfo.subplot = True ``` 这段代码说明了怎样手动设定某个技术指标是否独立占用一个新的坐标轴区域作为其专属视窗。 除了简单的调用之外,还可以深入探索二者之间的交互方式。比如修改默认样式表、添加额外注解或是保存高质量图像件等等。下面给出一段完整的例子用于演示整个过程: ```python import backtrader as bt import datetime import matplotlib.pyplot as plt class TestStrategy(bt.Strategy): def __init__(self): self.sma = bt.indicators.SimpleMovingAverage( self.datas[0].close, period=20) def next(self): if not self.position: if self.data.close[-1] < self.sma and \ self.data.close[0] > self.sma: self.buy() elif self.data.close[-1] >= self.sma and \ self.data.close[0] <= self.sma: self.sell() if __name__ == '__main__': cerebro = bt.Cerebro() data = bt.feeds.YahooFinanceData(dataname='AAPL', fromdate=datetime.datetime(2020, 1, 1), todate=datetime.datetime(2021, 1, 1)) cerebro.adddata(data) cerebro.addstrategy(TestStrategy) # 设置初始资金量 cerebro.broker.setcash(10000.0) # 开启绘图模式并应用个性化风格 plt.style.use('ggplot') cerebro.run(runonce=False, stdstats=True).plot(style='candlestick', volume=False)[^3] plt.show() ``` 此脚本实现了基于简单移动平均线(SMA)交叉信号买卖苹果公司股票(AAPL)的一个基础策略,并且采用了蜡烛图形式呈现历史价格走势以及执行记录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值