交互工具
Hikyuu是研究系统交易的Python量化框架,可以在其基础上构建自己的客户端程序。而Hikyuu库本身自带一个交互式工具,可以在Python Shell环境下进行探索,可以从在Python shell中使用 “from hikyuu.interactive.interactive” 引入该工具。该工具主要提供基于 matplotlib 的图形绘制函数,便于在交互式环境下绘制K线、指标、信号等。
使用 matplotlib 绘制图形,必须了解 matplotlib 绘图中的两个基本对象:figure、axes。窗口(figure)是坐标轴(axes)对象的容器,绘图时首先需指定窗口,在窗口中绘制相应的坐标轴,然后在坐标轴中绘制图形。在 matplotlib 中绘制图形需要指定图形在哪个坐标轴中进行绘制。当绘图函数中没有指定坐标轴时,将默认使用当前的坐标轴,如果不存在任何一个坐标轴时,matplotlib 将创建自动创建一个窗口及其包含的坐标轴。
绘图的详细示例:
绘制组合窗口
创建组合窗口的快捷函数,返回相应的 axes 对象或tuple。
- create_figure([n = 1, figsize = (10, 8)])
生成含有指定坐标轴数量的窗口,最大只支持4个坐标轴。
- 参数:
n (int) – 坐标轴数量
figsize – (宽, 高)
- 返回:
(ax1, ax2, …) 根据指定的坐标轴数量而定,超出[1,4]个坐标轴时,返回None
绘制图形的便捷函数
- ax_set_locator_formatter(axes, dates, typ)
设置指定坐标轴的日期显示,根据指定的K线类型优化X轴坐标显示
- 参数:
axes – 指定的坐标轴
dates – Datetime构成可迭代序列
typ (Query.KType) – K线类型
- adjust_axes_show(axeslist)
用于调整上下紧密相连的坐标轴显示时,其上一坐标轴最小值刻度和下一坐标轴最大值刻度显示重叠的问题
- 参数:
axeslist – 上下相连的坐标轴列表 (ax1,ax2,…)
通达信兼容绘图函数
通达信兼容绘图函数(仅支持 matplotlib 引擎)
- RGB(r, g, b)
颜色RGB值,如 RGB(255, 0, 0)
- STICKLINE(cond: Indicator, price1: Indicator, price2: Indicator, width: int = 2.0,
- empty: bool = False, color='m', alpha=1.0, kdata=None, new=False, axes=None)
在满足cond的条件下,在 price1 和 price2 之间绘制一个宽度为 width 的柱状图。
注意: cond, price1, price2 应含有数据,否则请指定 kdata 作为指标计算的上下文
- 参数:
cond (Indicator) – 条件表达式,用于确定是否绘制柱状线
price1 (Indicator) – 第一个价格
price2 (Indicator) – 第二个价格
width (int) – (optional) 柱状宽度. Defaults to 2.0.
empty (bool) – (optional): 空心. Defaults to False.
kdata (KData) – (optional): 指定的上下文K线. Defaults to None.
new (bool) – (optional): 在新窗口中绘制. Defaults to False.
axes – (optional): 在指定的坐标轴中绘制. Defaults to None.
color (str) – (optional): 颜色. Defaults to ‘m’.
alpha (float) – (optional): 透明度. Defaults to 1.0.
- DRAWBAND(val1: Indicator, color1='m', val2: Indicator = None, color2='b', kdata=None, alpha=0.2, new=False, axes=None, linestyle='-')
画出带状线
用法:DRAWBAND(val1, color1, val2, color2), 当 val1 > val2 时,在 val1 和 val2 之间填充 color1; 当 val1 < val2 时,填充 color2,这里的颜色均使用 matplotlib 颜色代码. 例如:DRAWBAND(OPEN, ‘r’, CLOSE, ‘b’)
- 参数:
val1 (Indicator) – 指标1
color1 (str) – (optional): 颜色1. Defaults to ‘m’.
val2 (Indicator) – (optional): 指标2. Defaults to None.
color2 (str) – (optional): 颜色2. Defaults to ‘b’.
kdata (KData) – (optional): 指定指标上下文. Defaults to None.
alpha (float) – (optional): 透明度. Defaults to 0.2.
new (bool) – (optional): 在新窗口中绘制. Defaults to False.
axes – (optional): 在指定的坐标轴中绘制. Defaults to None.
linestyle (str) – (optional): 包络线类型. Defaults to ‘-‘.
- PLOYLINE(cond: Indicator, price: Indicator, kdata: KData = None, color: str = 'm', linewidth=1.0, new=False, axes=None, *args, **kwargs)
在图形上绘制折线段。
用法:PLOYLINE(COND,PRICE),当COND条件满足时,以PRICE位置为顶点画折线连接。 例如:PLOYLINE(HIGH>=HHV(HIGH,20),HIGH, kdata=k)表示在创20天新高点之间画折线。
- 参数:
- DRAWLINE(cond1: Indicator, price1: Indicator, cond2: Indicator, price2: Indicator, expand: int = 0, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
在图形上绘制直线段。
用法:DRAWLINE(cond1, price1, cond2, price2, expand) 当COND1条件满足时,在PRICE1位置画直线起点,当COND2条件满足时,在PRICE2位置画直线终点,EXPAND为延长类型。 例如:DRAWLINE(HIGH>=HHV(HIGH,20),HIGH,LOW<=LLV(LOW,20),LOW,1)表示在创20天新高与创20天新低之间画直线并且向右延长
- 参数:
cond1 (Indicator) – 条件1
price1 (Indicator) – 位置1
cond2 (Indicator) – 条件2
price2 (Indicator) – 位置2
expand (int) – (optional): 0: 不延长 | 1: 向右延长 | 10: 向左延长 | 11: 双向延长. Defaults to 0.
kdata (KData) – (optional): 指定的上下文. Defaults to None.
color (str) – (optional): 指定颜色. Defaults to ‘m’.
new (bool) – (optional): 在新窗口中绘制. Defaults to False.
axes – (optional): 指定的坐标轴. Defaults to None.
- DRAWTEXT(cond: Indicator, price: Indicator, text: str, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
在图形上显示文字。
用法: DRAWTEXT(cond, price, text), 当 cond 条件满足时, 在 price 位置书写文字 text。 例如: DRAWTEXT(CLOSE/OPEN>1.08,LOW,’大阳线’)表示当日实体阳线大于8%时在最低价位置显示’大阳线’字样.
- DRAWTEXT_FIX(cond: Indicator, x: float, y: float, type: int, text: str, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
固定位置显示文字
用法:DRAWTEXT_FIX(cond,x y, text), cond 中一般需要加 ISLASTBAR,当 cond 条件满足时, 在当前指标窗口内(X,Y)位置书写文字TEXT,X,Y为书写点在窗口中相对于左上角的百分比
例如:DRAWTEXT_FIX(ISLASTBAR() & (CLOSE/OPEN>1.08),0.5,0.5,0,’大阳线’)表示最后一个交易日实体阳线 大于8%时在窗口中间位置显示’大阳线’字样.
- 参数:
cond (Indicator) – 条件
x (float) – x轴坐标
y (float) – y轴坐标
type (int) – (optional): 0 左对齐 | 1 右对齐.
text (str) – 待显示文字
kdata (KData) – (optional): 指定的上下文. Defaults to None.
color (str) – (optional): 指定颜色. Defaults to ‘m’.
new (bool) – (optional): 在新窗口中绘制. Defaults to False.
axes – (optional): 指定坐标轴. Defaults to None.
- DRAWNUMBER(cond: Indicator, price: Indicator, number: Indicator, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
画出数字.
用法:DRAWNUMBER(cond, price, number),当 cond 条件满足时,在 price 位置书写数字 number. 例如:DRAWNUMBER(CLOSE/OPEN>1.08,LOW,C)表示当日实体阳线大于8%时在最低价位置显示收盘价。
- DRAWNUMBER_FIX(cond: Indicator, x: float, y: float, type: int, number: float, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
固定位置显示数字.
用法:DRAWNUMBER_FIX(cond,x,y,type,number), cond 中一般需要加 ISLASTBAR, 当 cond 条件满足时, 在当前指标窗口内 (x, y) 位置书写数字 number, x,y为书写点在窗口中相对于左上角的百分比,type:0为左对齐,1为右对齐。
例如:DRAWNUMBER_FIX(ISLASTBAR() & (CLOSE/OPEN>1.08), 0.5,0.5,0,C)表示最后一个交易日实体阳线大于8%时在窗口中间位置显示收盘价
- 参数:
cond (Indicator) – _description_
x (float) – _description_
y (float) – _description_
type (int) – _description_
number (Indicator) – _description_
kdata (KData) – (optional): _description_. Defaults to None.
color (str) – (optional): _description_. Defaults to ‘m’.
new (bool) – (optional): _description_. Defaults to False.
axes – (optional): _description_. Defaults to None.
- DRAWSL(cond: Indicator, price: Indicator, slope: Union[Indicator, float, int], length: Union[Indicator, float, int], direct: int, kdata: KData = None, color: str = 'm', new=False, axes=None, *args, **kwargs)
绘制斜线.
用法:DRAWSL(cond,price,slope,length,diect),当 cond 条件满足时,在 price 位置画斜线, slope 为斜率, lengh为长度, direct 为0向右延伸,1向左延伸,2双向延伸。
注意: 1. K线间的纵向高度差为 slope; 2. slope 为 0 时, 为水平线; 3. slope 为 10000 时, 为垂直线, length 为向上的像素高度, direct 表示向上或向下延伸 4. slope 和 length 支持变量;
- DRAWIMG(cond: Indicator, price: Indicator, img: str, kdata: KData = None, new=False, axes=None, *args, **kwargs)
画图片
用法:DRAWIMG(cond,price,’图像文件文件名’),当条件 cond 满足时,在 price 位置画指定的图片 例如:DRAWIMG(O>C,CLOSE, ‘123.png’)。
- DRAWICON(cond: Indicator, price: Indicator, type: int, kdata: KData = None, new=False, axes=None, *args, **kwargs)
绘制内建图标
- SHOWICONS()
显示所有内置图标
- DRAWRECTREL(left: int, top: int, right: int, bottom: int, color='m', frame=True, fill=True, alpha=0.1, new=False, axes=None, *args, **kwargs)
相对位置上画矩形.
注意:原点为坐标轴左上角(0, 0),和 matplotlib 不同。 用法: DRAWRECTREL(left,top,right,bottom,color), 以图形窗口 (left, top) 为左上角, (right, bottom) 为右下角绘制矩形, 坐标单位是窗口沿水平和垂直方向的1/1000,取值范围是0—999,超出范围则可能显示在图形窗口外,矩形中间填充颜色COLOR,COLOR为0表示不填充.
例如:DRAWRECTREL(0,0,500,500,RGB(255,255,0)) 表示在图形最左上部1/4位置用黄色绘制矩形
- 参数:
left (int) – 左上角x
top (int) – 左上角y
right (int) – 右下角x
bottom (int) – 右下角y
color (str) – (optional): 指定颜色. Defaults to ‘m’.
frame (bool) – (optional): 添加边框. Defaults to False.
fill (bool) – (optional): 颜色填充. Defaults to True.
alpha (float) – (optional): 透明度. Defaults to 0.1.
new (bool) – (optional): 在新窗口中绘制. Defaults to False.
axes – (optional): 指定的坐标轴. Defaults to None.
绘制K线、指标、信号等
以下函数已设为相应类型对象的 plot 函数,如 KData kdata 可直接调用 kdata.plot(),等同于没有第一个参数的 kplot()
- kplot(kdata[, new=True, axes=None, colorup='r', colordown='g', width=0.6, alpha=1.0])
绘制K线图
- 参数:
kdata (KData) – K线数据
new (bool) – 是否在新窗口中显示,只在没有指定axes时生效
axes – 指定的坐标轴
colorup – the color of the rectangle where close >= open
colordown – the color of the rectangle where close < open
width – fraction of a day for the rectangle width
alpha – the rectangle alpha level, 透明度(0.0~1.0) 1.0为不透明
- mkplot(kdata[, new=True, axes=None, colorup='r', colordown='g', ticksize=3])
绘制美式K线图
- 参数:
kdata (KData) – K线数据
new (bool) – 是否在新窗口中显示,只在没有指定axes时生效
axes – 指定的坐标轴
colorup – the color of the lines where close >= open
colordown – the color of the lines where close < open
ticksize – open/close tick marker in points
- iplot(indicator[, new=True, axes=None, legend_on=False, text_on=False, text_color='k', zero_on=False, label=None, *args, **kwargs])
绘制indicator曲线图
- 参数:
indicator (Indicator) – indicator实例
new – 是否在新窗口中显示,只在没有指定axes时生效
axes – 指定的坐标轴
legend_on – 是否打开图例
text_on – 是否在左上角显示指标名称及其参数
text_color – 指标名称解释文字的颜色,默认为黑色
zero_on – 是否需要在y=0轴上绘制一条直线
label (str) – label显示文字信息,text_on 及 legend_on 为 True 时生效
args – pylab plot参数
kwargs – pylab plot参数,如:marker(标记类型)、markerfacecolor(标记颜色)、markeredgecolor(标记的边缘颜色)
- ibar(indicator[, new=True, axes=None, legend_on=False, text_on=False, text_color='k', label=None, width=0.4, color='r', edgecolor='r', zero_on=False, *args, **kwargs])
绘制indicator柱状图
- 参数:
indicator (Indicator) – Indicator实例
axes – 指定的坐标轴
new – 是否在新窗口中显示,只在没有指定axes时生效
legend_on – 是否打开图例
text_on – 是否在左上角显示指标名称及其参数
text_color – 指标名称解释文字的颜色,默认为黑色
label (str) – label显示文字信息,text_on 及 legend_on 为 True 时生效
zero_on – 是否需要在y=0轴上绘制一条直线
width – Bar的宽度
color – Bar的颜色
edgecolor – Bar边缘颜色
args – pylab plot参数
kwargs – pylab plot参数
- sgplot(sg[, new = True, axes = None, style = 1, kdata = None])
绘制买入/卖出信号
- 参数:
sg (SignalBase) – 信号指示器
new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
axes – 指定在那个轴对象中进行绘制
style – 1 | 2 信号箭头绘制样式
kdata (KData) – 指定的KData(即信号发生器的交易对象),如该值为None,则认为该信号发生器已经指定了交易对象,否则,使用该参数作为交易对象
- cnplot(cn[, new=True, axes=None, kdata=None])
绘制系统有效条件
- 参数:
cn (ConditionBase) – 系统有效条件
new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
axes – 指定在那个轴对象中进行绘制
kdata (KData) – 指定的KData,如该值为None,则认为该系统有效条件已经指定了交易对象,否则,使用该参数作为交易对象
- sysplot(sys[, new=True, axes=None, style=1])
绘制系统实际买入/卖出信号
- 参数:
sys (SystemBase) – 系统实例
new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
axes – 指定在那个轴对象中进行绘制
style – 1 | 2 信号箭头绘制样式
内建示例
- vl.draw(stock, query=Query(- 130), ma1_n=5, ma2_n=10, ma3_n=20, ma4_n=60, ma5_n=100, ma_type='SMA', vma1_n=5, vma2_n=10)
绘制普通K线图 + 成交量(成交金额)
- vl.draw2(stock, query=Query(- 130), ma1_n=7, ma2_n=20, ma3_n=30, ma4_n=42, ma5_n=100, vma1_n=5, vma2_n=10)
绘制普通K线图 + 成交量(成交金额)+ MACD
- el.draw(stock, query=QueryByIndex(- 130), ma_n=22, ma_w='auto', vigor_n=13)
绘制亚历山大.艾尔德交易系统图形。参见 [BOOK2]