量化交易学习(四十)backtrader文档——绘图(3)

今天这篇是backtrader文档的学习笔记。主要介绍了绘图。

官方文档链接:https://www.backtrader.com/docu/plotting/plotting/

控制绘图的方法

在处理指标和观察者时,支持以下方法来进一步控制绘图:

  • _plotlabel(self)

它应该返回一个列表,以符合标签,这些标签将放置在指标或观察者名称后面的括号之间

RSI指标示例:

1
2
3
4
def _plotlabel(self):
plabels = [self.p.period]
plabels += [self.p.movav] * self.p.notdefault('movav')
return plabels

可以看出该方法返回:

  • 返回一个表示RSI的周期的int变量,以及如果默认移动平均线已变化,返回对应的类

在后台,两者都将转换为字符串。对于类,将努力只打印类的名称而不是完整的module.name组合。

  • _plotinit(self)

在绘图开始时调用它来执行指标可能需要的任何特定初始化。再用RSI举一个例子:

1
2
def _plotinit(self):
self.plotinfo.plotyhlines = [self.p.upperband, self.p.lowerband]

这里,代码给plotyhlines赋值,以在特定y值处绘制水平线(hlines部分)。

参数值upperbandlowerband用于此目的,无法提前知道,因为参数可以由最终用户更改

系统范围的绘图选项

首先是cerebro内的签名:plot

1
def plot(self, plotter=None, numfigs=1, iplot=True, **kwargs):

简单介绍下:

  • plotter:一个对象/类,属性包含控制系统范围绘图的选项

如果设为None,则一个默认的PlotScheme对象(见下文)将被实例化

  • numfigs:一个图必须被分成多少个独立图表进行绘制

有时,图表包含太多条形图,如果都在单个图中绘制,则不容易阅读。这会根据要求将其分解为指定份数。

  • iplot:如果在 Jupyter Notebook 中运行,则自动绘制内联图

  • **kwargs: 这些参数将用于更改plotter的属性值,如果没有plotter传入,则创建默认的PlotScheme对象。

PlotScheme

该对象包含控制系统范围绘图的所有选项。这些选项记录在代码中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 中的颜色

PlotScheme类定义了一个可以在子类中重写的方法,该方法返回要使用的下一个颜色:

1
def color(self, idx)

idx是当前正在被绘制在单独子图上的线对象的索引。 例如,MACD绘制了 3 条线,因此idx变量将只有以下值:012。下一个图表(可能是另一个指标)将在0索引再次开始计数。

backtrader默认使用的配色方案(如上所示)是索引按下面修改的Tableau 10 Color Palette

1
tab10_index = [3, 0, 2, 1, 2, 4, 5, 6, 7, 8, 9]

通过重写color方法或将lcolors变量传递给plot(或在PlotScheme的子类中),可以完全更改颜色。

源代码中还包含Tableau 10 LightTableau 20调色板的定义。


这一篇就到这里啦。欢迎大家点赞、转发、私信。还没有关注我的朋友可以关注 江达小记

江达小记