def sysplot(sys, new=True, axes=None, style=1): """绘制系统实际买入/卖出信号 :param SystemBase sys: 系统实例 :param new: 仅在未指定axes的情况下生效,当为True时, 创建新的窗口对象并在其中进行绘制 :param axes: 指定在那个轴对象中进行绘制 :param style: 1 | 2 信号箭头绘制样式 """ kdata = sys.getTO() refdates = kdata.getDatetimeList() if kdata.getQuery().kType == KQuery.DAY: x_list = [d.date() for d in refdates] else: x_list = [d.datetime() for d in refdates] date_index = dict([(d,i) for i,d in enumerate(refdates)]) if axes is None: if new: axes = create_figure() kplot(kdata, axes=axes) else: axes = gca() es = EffectScatter() highest = round(max(HIGH(kdata)),2) lowest = round(min(LOW(kdata)), 2) height = highest - lowest tds = sys.tm.getTradeList() buy_dates = [] sell_dates = [] for t in tds: if t.business == BUSINESS.BUY: buy_dates.append(t.datetime) elif t.business == BUSINESS.SELL: sell_dates.append(t.datetime) else: pass dates = buy_dates buy_y_list = ['-' for i in range(len(refdates))] for d in dates: if d not in date_index: continue pos = date_index[d] krecord = kdata[pos] buy_y_list[pos] = round(krecord.lowPrice - height*0.02, 2) es.add("", x_list, buy_y_list, symbol_size=12, effect_scale=2.5, effect_period=0,symbol="triangle", is_label_show=True, label_formatter='B', label_pos = 'bottom', label_text_color = '#CD0000', label_color=['#CD0000', '#008B00']) dates = sell_dates sell_y_list = ['-' for i in range(len(refdates))] for d in dates: if d not in date_index: continue pos = date_index[d] krecord = kdata[pos] sell_y_list[pos] = round(krecord.highPrice + height*0.015, 2) es.add("", x_list, sell_y_list, symbol_size=20, effect_scale=2.5, effect_period=0,symbol="pin", is_label_show=True, label_formatter='S', label_pos = 'top', label_text_color = '#008B00', label_color=['#CD0000', '#008B00', '#008B00']) axes.add(es) gcf().set_xaxis(x_list) gcf().add_axis(axes) return gcf()
def cnplot(cn, new=True, axes=None, kdata=None): """绘制系统有效条件 :param ConditionBase cn: 系统有效条件 :param new: 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制 :param axes: 指定在那个轴对象中进行绘制 :param KData kdata: 指定的KData,如该值为None,则认为该系统有效条件已经 指定了交易对象,否则,使用该参数作为交易对象 """ if kdata is None: kdata = cn.getTO() else: cn.setTO(kdata) refdates = kdata.getDatetimeList() if kdata.getQuery().kType == KQuery.DAY: x_list = [d.date() for d in refdates] else: x_list = [d.datetime() for d in refdates] axes2 = None if axes is None: if new: axes, axes2 = create_figure(2) kplot(kdata, axes=axes) else: axes = gca() max_value = max(HIGH(kdata)) line = Line() y1 = [max_value if cn.isValid(d) else 0 for d in refdates] y2 = [0 if cn.isValid(d) else max_value for d in refdates] line.add("", x_list, y1, is_step=True, is_fill=True, yaxis_max = max_value, is_symbol_show=False, line_opacity=0, label_color='#CD0000', area_color='#CD0000', area_opacity=0.2) line.add("", x_list, y2, is_step=True, is_fill=True, yaxis_max = max_value, is_symbol_show=False, line_opacity=0, label_color='#0000FF', area_color='#0000FF', area_opacity=0.2) gcf().set_xaxis(x_list) if axes2 is not None: axes2.add(line) gcf().add_axis(axes2) else: axes.add(line) gcf().add_axis(axes) return gcf()
def 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)""" kdata = stock.getKData(query) ax1, ax2 = create_figure(2) kdata.plot(axes=ax1) cama = AMA(CLOSE(kdata), n=n) cama.name = "CAMA" cama.plot(axes=ax1, color='b', legend_on=True) hama = AMA(HIGH(kdata), n=n) hama.name = "HAMA" hstd = STDEV(hama, n) lama = AMA(LOW(kdata), n=n) lama.name = "LAMA" lstd = STDEV(lama, n) fy1 = list(lama - lstd)[lstd.discard:] fy2 = list(hama + hstd)[hstd.discard:] ax1.fill_between(range(lstd.discard, len(kdata)), fy1, fy2, alpha=0.2, color='y') if show_high_low: hama.plot(axes=ax1, color='r', legend_on=True) lama.plot(axes=ax1, color='g', legend_on=True) if sg_type == 'CROSS': fast_op = AMA(n=n) slow_op = EMA(n=2 * n)(fast_op) sg = SG_Cross(fast_op, slow_op) sg.plot(axes=ax1, kdata=kdata) ind = slow_op(KDATA(kdata)) ind.name = "EMA(CAMA)" ind.plot(axes=ax1, color='m', legend_on=True) elif sg_type == 'SINGLE': sg = SG_Single(cama, filter_n=filter_n, filter_p=filter_p) sg.plot(axes=ax1, kdata=kdata) else: print("sg_type only in ('CORSS', 'SINGLE')") cer = PRICELIST(cama, 1) label = "ER(%s)" % cer[-1] cer.plot(axes=ax2, color='b', marker='o', label=label, legend_on=False, text_on=True) c = CLOSE(kdata) CVAL(c, 0.8).plot(axes=ax2, color='r', linestyle='--') CVAL(c, -0.6).plot(axes=ax2, color='r', linestyle='--') CVAL(c, -0.8).plot(axes=ax2, color='r', linestyle='--') CVAL(c, 0).plot(axes=ax2, color='k', linestyle='-') #ax2.hlines(0.8,0,len(kdata),color='r',linestyle='--') #ax2.hlines(-0.6,0,len(kdata),color='r',linestyle='--') #ax2.hlines(-0.8,0,len(kdata),color='r',linestyle='--') #ax2.hlines(0,0,len(kdata)) ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.getDatetimeList(), query.kType) adjust_axes_show([ax1, ax2]) return show_gcf()
def sgplot(sg, new=True, axes=None, style=1, kdata=None): """绘制买入/卖出信号 :param SignalBase sg: 信号指示器 :param new: 仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制 :param axes: 指定在那个轴对象中进行绘制 :param style: 1 | 2 信号箭头绘制样式 :param KData kdata: 指定的KData(即信号发生器的交易对象), 如该值为None,则认为该信号发生器已经指定了交易对象, 否则,使用该参数作为交易对象 """ if kdata is None: kdata = sg.getTO() else: sg.setTO(kdata) refdates = kdata.getDatetimeList() if kdata.getQuery().kType == KQuery.DAY: x_list = [d.date() for d in refdates] else: x_list = [d.datetime() for d in refdates] date_index = dict([(d,i) for i,d in enumerate(refdates)]) if axes is None: if new: axes = create_figure() kplot(kdata, axes=axes) else: axes = gca() es = EffectScatter() highest = round(max(HIGH(kdata)),2) lowest = round(min(LOW(kdata)), 2) height = highest - lowest dates = sg.getBuySignal() buy_y_list = ['-' for i in range(len(refdates))] for d in dates: if d not in date_index: continue pos = date_index[d] krecord = kdata[pos] buy_y_list[pos] = round(krecord.lowPrice - height*0.02, 2) es.add("", x_list, buy_y_list, symbol_size=12, effect_scale=2.5, effect_period=0,symbol="triangle", is_label_show=True, label_formatter='B', label_pos = 'bottom', label_text_color = '#CD0000', label_color=['#CD0000', '#008B00']) dates = sg.getSellSignal() sell_y_list = ['-' for i in range(len(refdates))] for d in dates: if d not in date_index: continue pos = date_index[d] krecord = kdata[pos] sell_y_list[pos] = round(krecord.highPrice + height*0.015, 2) es.add("", x_list, sell_y_list, symbol_size=20, effect_scale=2.5, effect_period=0,symbol="pin", is_label_show=True, label_formatter='S', label_pos = 'top', label_text_color = '#008B00', label_color=['#CD0000', '#008B00', '#008B00']) axes.add(es) gcf().set_xaxis(x_list) gcf().add_axis(axes) return gcf()
def 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)""" sm = StockManager.instance() if block.name == 'SZ': kdata = sm['sz000001'].getKData(query) elif block.name == 'GEM': kdata = sm['sz399006'].getKData(query) else: kdata = sm['sh000001'].getKData(query) ax1, ax2, ax3 = create_figure(3) kdata.plot(axes=ax1) cama = AMA(CLOSE(kdata), n=n) cama.name = "CAMA" cama.plot(axes=ax1, color='b', legend_on=True) hama = AMA(HIGH(kdata), n=n) hama.name = "HAMA" hstd = STDEV(hama, n) lama = AMA(LOW(kdata), n=n) lama.name = "LAMA" lstd = STDEV(lama, n) fy1 = list(lama - lstd)[lstd.discard:] fy2 = list(hama + hstd)[hstd.discard:] ax1.fill_between(range(lstd.discard, len(kdata)), fy1, fy2, alpha=0.2, color='y') if show_high_low: hama.plot(axes=ax1, color='r', legend_on=True) lama.plot(axes=ax1, color='g', legend_on=True) if sg_type == 'CROSS': fast_op = AMA(n=n) slow_op = EMA(n=2 * n)(fast_op) sg = SG_Cross(fast_op, slow_op) sg.plot(axes=ax1, kdata=kdata) ind = slow_op(KDATA(kdata)) ind.name = "EMA(CAMA)" ind.plot(axes=ax1, color='m', legend_on=True) elif sg_type == 'SINGLE': sg = SG_Single(cama, filter_n=filter_n, filter_p=filter_p) sg.plot(axes=ax1, kdata=kdata) else: print("sg_type only in ('CORSS', 'SINGLE')") a = POS(block, query, SG_Flex(AMA(n=3), 6)) a.name = "POS(3)" a.plot(axes=ax2, color='b', marker='.', legend_on=True) a = POS(block, query, SG_Flex(AMA(n=30), 60)) a.name = "POS(30)" a.plot(axes=ax2, color='g', marker='.', legend_on=True) c = CLOSE(kdata) CVAL(c, 0.8).plot(axes=ax2, color='r', linestyle='--') CVAL(c, 0.2).plot(axes=ax2, color='r', linestyle='--') if ama1.name == "AMA": cer = PRICELIST(cama, 1) label = "ER(%s)" % cer[-1] cer.plot(axes=ax3, color='b', marker='.', label=label, legend_on=False, text_on=True) CVAL(c, 0.8).plot(axes=ax3, color='r', linestyle='--') CVAL(c, -0.6).plot(axes=ax3, color='r', linestyle='--') CVAL(c, -0.8).plot(axes=ax3, color='r', linestyle='--') CVAL(c, 0).plot(axes=ax3, color='k', linestyle='-') else: ax_draw_macd(ax2, kdata) #ax2.set_ylim(-1, 1) ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.getDatetimeList(), query.kType) adjust_axes_show([ax1, ax2]) return show_gcf()