hikyuu 交互式工具(绘图)


【hikyuu.interactive.interactive是一个交互式工具
可以从在Python shell中使用 “from hikyuu.interactive.interactive” 引入该工具
主要提供基于 matplotlib 的图形绘制函数,便于在交互式环境下绘制K线、指标、信号等。】

【使用 matplotlib 绘制图形,必须了解 matplotlib 绘图中的两个基本对象:figure、axes。
窗口(figure)是坐标轴(axes)对象的容器,
绘图时首先需指定窗口,在窗口中绘制相应的坐标轴,然后在坐标轴中绘制图形。
在 matplotlib 中绘制图形需要指定图形在哪个坐标轴中进行绘制。
当绘图函数中没有指定坐标轴时,将默认使用当前的坐标轴,
如果不存在任何一个坐标轴时,matplotlib 将创建自动创建一个窗口及其包含的坐标轴。】

接口:
1.绘制组合窗口(创建窗口):
create_figure([n = 1figsize = (108)])
生成含有指定坐标轴数量的窗口,最大只支持4个坐标轴
参数:
  • n (int) – 坐标轴数量

figsize – (宽, 高)

返回:

(ax1, ax2, …) 根据指定的坐标轴数量而定,超出[1,4]个坐标轴时,返回None

2.绘制图形

  • ax_set_locator_formatter(axes, dates, typ)

设置指定坐标轴的日期显示,根据指定的K线类型优化X轴坐标显示


参数:
  • axes – 指定的坐标轴
  • dates – Datetime构成可迭代序列
  • typ (Query.KType) – K线类型
  • adjust_axes_show(axeslist)



用于调整上下紧密相连的坐标轴显示时,其上一坐标轴最小值刻度和下一坐标轴最大值刻度显示重叠的问题


参数: axeslist – 上下相连的坐标轴列表 (ax1,ax2,…)
  • ax_draw_macd(axes, kdata[, n1=12, n2=26, n3=9])绘制MACD
参数:
  • axes – 指定的坐标轴
  • kdata (KData) – KData
  • n1 (int) – 指标 MACD 的参数1
  • n2 (int) – 指标 MACD 的参数2
  • n3 (int) – 指标 MACD 的参数3
  • ax_draw_macd2(axes, ref, kdata, n1=12, n2=26, n3=9)


绘制MACD。 当BAR值变化与参考序列ref变化不一致时,显示为灰色, 当BAR和参考序列ref同时上涨,显示红色 当BAR和参考序列ref同时下跌,显示绿色


参数:
  • axes – 指定的坐标轴
  • ref – 参考序列,EMA
  • kdata (KData) – KData
  • n1 (int) – 指标 MACD 的参数1
  • n2 (int) – 指标 MACD 的参数2
  • n3 (int) – 指标 MACD 的参数3

3.绘制K线,指标,信号等
以下函数已设为相应类型对象的 plot 函数,如 KData kdata 可直接调用 kdata.plot(),等同于没有第一个参数的 kplot()
kplot(kdata[, new=Trueaxes=Nonecolorup='r'colordown='g'width=0.6alpha=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=Trueaxes=Nonecolorup='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=Trueaxes=Nonelegend_on=Falsetext_on=Falsetext_color='k'zero_on=Falselabel=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=Trueaxes=Nonelegend_on=Falsetext_on=Falsetext_color='k'label=Nonewidth=0.4color='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 = Trueaxes = Nonestyle = 1kdata = None])

绘制买入/卖出信号

参数:
  • sg (SignalBase) – 信号指示器
  • new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
  • axes – 指定在那个轴对象中进行绘制
  • style – 1 | 2 信号箭头绘制样式
  • kdata (KData) – 指定的KData(即信号发生器的交易对象),如该值为None,则认为该信号发生器已经指定了交易对象,否则,使用该参数作为交易对象
cnplot(cn[new=Trueaxes=Nonekdata=None])

绘制系统有效条件

参数:
  • cn (ConditionBase) – 系统有效条件
  • new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
  • axes – 指定在那个轴对象中进行绘制
  • kdata (KData) – 指定的KData,如该值为None,则认为该系统有效条件已经指定了交易对象,否则,使用该参数作为交易对象
sysplot(sys[new=Trueaxes=Nonestyle=1])

绘制系统实际买入/卖出信号

参数:
  • sys (SystemBase) – 系统实例
  • new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
  • axes – 指定在那个轴对象中进行绘制
  • style – 1 | 2 信号箭头绘制样式
 

4.内建示例

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)

绘制亚历山大.艾尔德交易系统图形。参见

kf.draw(stock, query=Query(-130), n=10, filter_n=20, filter_p=0.1, sg_type = "CROSS", show_high_low=False, arrow_style=1)

绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)。参见

 

  kf.draw2(block, query=Query(-130), ama1=AMA(n=10, fast_n=2, slow_n=30), ama2=None, n=10, filter_n=20, filter_p=0.1, sg_type='CROSS', show_high_low=True, arrow_style=1)

绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)。


详细示例:

0.导入

%matplotlib inline
%time from hikyuu.interactive.interactive import *


1.使用 create_figure 函数快速创建查看证券K线信息的常见组合窗口

 

 

 

 

 

 

2.在第一个坐标轴中绘制K线EMA在第二个坐标轴中绘制艾尔德力度指标

 


 3.绘制MACD

 


 4.内建的三个教复杂的示例

  • l.draw 绘制亚历山大.艾尔德交易系统图形
  • kf.draw 绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)
  • kf.draw2 绘制佩里.J.考夫曼(Perry J.Kaufman) 自适应移动平均系统(AMA)
  • vl.draw 绘制普通K线图 + 成交量(成交金额)
  • vl.draw2 绘制普通K线图 + 成交量(成交金额)+ MACD

 


 

 


绘制买入/卖出信号

参数:
  • sg (SignalBase) – 信号指示器
  • new – 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
  • axes – 指定在那个轴对象中进行绘制
  • style – 1 | 2 信号箭头绘制样式
  • kdata (KData) – 指定的KData(即信号发生器的交易对象),如该值为None,则认为该信号发生器已经指定了交易对象,否则,使用该参数作为交易对象
posted @ 2021-02-09 00:11  KnowledgePorter  阅读(126)  评论(0)    收藏  举报