def _draw_ema_pipe(axes, kdata, ema, n=22, w=0.10): emas = [i for i in ema] p = _find_ema_coefficient( list( CLOSE(kdata)), emas, number=66, percent=0.95) if w == 'auto' else w emas_high = [i + i * p for i in emas] emas_low = [i - i * p for i in emas] emas_len = len(emas) PRICELIST(emas).plot(axes=axes, color='b', linestyle='-', label='%s(%s) %.2f' % (ema.name, n, emas[-1])) PRICELIST(emas_high).plot(axes=axes, color='b', linestyle=':', label='U%s(%.2f%%) %.2f' % (ema.name, p * 100, emas_high[-1])) PRICELIST(emas_low).plot(axes=axes, color='b', linestyle=':', label='L%s(%.2f%%) %.2f' % (ema.name, p * 100, emas_low[-1])) #axes.plot(emas, '-b', label='%s(%s) %.2f'%(ema.name, n, emas[-1])) #axes.plot(emas_high, ':b', label='U%s(%.2f%%) %.2f'%(ema.name, p*100, emas_high[-1])) #axes.plot(emas_low, ':b', label='L%s(%.2f%%) %.2f'%(ema.name, p*100, emas_low[-1])) fy1 = [i for i in emas_low] fy2 = [i for i in emas_high] axes.fill_between(range(emas_len), fy1, fy2, alpha=0.2, color='y')
def ax_draw_macd(axes, kdata, n1=12, n2=26, n3=9): """绘制MACD :param axes: 指定的坐标轴 :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult(2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f'%(n1,n2,n3,fmacd[-1],smacd[-1],bmacd[-1]) #axes.text(0.01,0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x_list = [i for i in range(total)] y1_list = [round(x) if x > 0 else '-' for x in bmacd] y2_list = [round(x) if x <= 0 else '-' for x in bmacd] bar = Bar(subtitle=text, title_pos='10%', title_top='8%') bar.add('1', x_list, y1_list, is_stack=True, is_legend_show=False) bar.add('2', x_list, y2_list, is_stack=True, is_legend_show=False) axes.add(bar) iplot(fmacd, line_type='dotted') iplot(smacd) return axes
def 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""" kdata = stock.get_kdata(query) close = CLOSE(kdata) ma1 = MA(close, ma1_n) ma2 = MA(close, ma2_n) ma3 = MA(close, ma3_n) ma4 = MA(close, ma4_n) ma5 = MA(close, ma5_n) ax1, ax2, ax3 = create_figure(3) kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True) ma2.plot(axes=ax1, legend_on=True) ma3.plot(axes=ax1, legend_on=True) ma4.plot(axes=ax1, legend_on=True) ma5.plot(axes=ax1, legend_on=True) vol = VOL(kdata) total = len(kdata) engine = get_current_draw_engine() if engine == 'matplotlib': rg = range(total) x = [i - 0.2 for i in rg] x1 = [x[i] for i in rg if kdata[i].close > kdata[i].open] y1 = [vol[i] for i in rg if kdata[i].close > kdata[i].open] x2 = [x[i] for i in rg if kdata[i].close < kdata[i].open] y2 = [vol[i] for i in rg if kdata[i].close < kdata[i].open] ax2.bar(x1, y1, width=0.4, color='r', edgecolor='r') ax2.bar(x2, y2, width=0.4, color='g', edgecolor='g') elif engine == 'echarts': vol.bar(axes=ax2, color='r', legend_on=True) else: pass vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True) vma2.plot(axes=ax2, legend_on=True) ax_draw_macd(ax3, kdata) ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.get_datetime_list(), kdata.get_query().ktype) adjust_axes_show([ax1, ax2, ax3]) return show_gcf()
def TurtleSG(self): n = self.getParam("n") k = self.getTO() c = CLOSE(k) h = REF(HHV(c, n), 1) #前n日高点 L = REF(LLV(c, n), 1) #前n日低点 for i in range(h.discard, len(k)): if (c[i] >= h[i]): self._addBuySignal(k[i].datetime) elif (c[i] <= L[i]): self._addSellSignal(k[i].datetime)
def draw(stock, query=Query(-130), ma1_n=5, ma2_n=10, ma3_n=20, ma4_n=60, ma5_n=100, vma1_n=5, vma2_n=10): """绘制普通K线图 + 成交量(成交金额)""" kdata = stock.getKData(query) close = CLOSE(kdata,) ma1 = MA(close, ma1_n) ma2 = MA(close, ma2_n) ma3 = MA(close, ma3_n) ma4 = MA(close, ma4_n) ma5 = MA(close, ma5_n) ax1, ax2 = create_figure(2) kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True) ma2.plot(axes=ax1, legend_on=True) ma3.plot(axes=ax1, legend_on=True) ma4.plot(axes=ax1, legend_on=True) ma5.plot(axes=ax1, legend_on=True) sg = SG_Cross(MA(n=ma1_n), MA(n=ma2_n)) sg.setTO(kdata) sg.plot(axes=ax1, kdata=kdata) vol = VOL(kdata) total = len(kdata) engine = get_current_draw_engine() if engine == 'matplotlib': rg = range(total) x = [i-0.2 for i in rg] x1 = [x[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] y1 = [vol[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] x2 = [x[i] for i in rg if kdata[i].closePrice <= kdata[i].openPrice] y2 = [vol[i] for i in rg if kdata[i].closePrice <= kdata[i].openPrice] ax2.bar(x1, y1, width=0.4, color='r', edgecolor='r') ax2.bar(x2, y2, width=0.4, color='g', edgecolor='g') elif engine == 'echarts': vol.bar(axes=ax2, color='r') else: pass vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True) vma2.plot(axes=ax2, legend_on=True) if query.kType == Query.WEEK and stock.market == 'SH' and stock.code=='000001': CVAL(0.16e+009, total, color='b',linestyle='--') #ax2.hlines(0.16e+009,0,len(kdata),color='b',linestyle='--') ax_set_locator_formatter(ax1, kdata.getDatetimeList(), kdata.getQuery().kType) adjust_axes_show([ax1, ax2]) return show_gcf()
def ax_draw_macd2(axes, ref, kdata, n1=12, n2=26, n3=9): """绘制MACD。 当BAR值变化与参考序列ref变化不一致时,显示为灰色, 当BAR和参考序列ref同时上涨,显示红色 当BAR和参考序列ref同时下跌,显示绿色 :param axes: 指定的坐标轴 :param ref: 参考序列,EMA :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult( 2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f' % ( n1, n2, n3, fmacd[-1], smacd[-1], bmacd[-1]) axes.text(0.01, 0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x = [i - 0.2 for i in range(0, total)] y = bmacd x1, x2, x3 = [x[0]], [], [] y1, y2, y3 = [y[0]], [], [] for i in range(1, total): if ref[i] - ref[i - 1] > 0 and y[i] - y[i - 1] > 0: x2.append(x[i]) y2.append(y[i]) elif ref[i] - ref[i - 1] < 0 and y[i] - y[i - 1] < 0: x3.append(x[i]) y3.append(y[i]) else: x1.append(x[i]) y1.append(y[i]) axes.bar(x1, y1, width=0.4, color='#BFBFBF', edgecolor='#BFBFBF') axes.bar(x2, y2, width=0.4, color='r', edgecolor='r') axes.bar(x3, y3, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
def 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): kdata = stock.getKData(query) close = CLOSE(kdata, ) ma1 = MA(close, ma1_n, ma_type) ma2 = MA(close, ma2_n, ma_type) ma3 = MA(close, ma3_n, ma_type) ma4 = MA(close, ma4_n, ma_type) ma5 = MA(close, ma5_n, ma_type) ax1, ax2 = create_two_axes_figure() kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True) ma2.plot(axes=ax1, legend_on=True) ma3.plot(axes=ax1, legend_on=True) ma4.plot(axes=ax1, legend_on=True) ma5.plot(axes=ax1, legend_on=True) sg = SG_Cross(OP(MA(n=ma1_n, type=ma_type)), OP(MA(n=ma2_n, type=ma_type))) sg.setTO(kdata) ax_draw_signal(ax1, kdata, sg.getBuySignal(), 'BUY', 1) ax_draw_signal(ax1, kdata, sg.getSellSignal(), 'SELL', 1) vol = VOL(kdata) total = len(kdata) rg = range(total) x = [i - 0.2 for i in rg] x1 = [x[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] y1 = [vol[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] x2 = [x[i] for i in rg if kdata[i].closePrice < kdata[i].openPrice] y2 = [vol[i] for i in rg if kdata[i].closePrice < kdata[i].openPrice] ax2.bar(x1, y1, width=0.4, color='r', edgecolor='r') ax2.bar(x2, y2, width=0.4, color='g', edgecolor='g') vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True) vma2.plot(axes=ax2, legend_on=True) if query.kType == Query.WEEK and stock.market == 'SH' and stock.code == '000001': ax2.hlines(0.16e+009, 0, len(kdata), color='b', linestyle='--') ax_set_locator_formatter(ax1, kdata.getDatetimeList(), kdata.getQuery().kType) adjust_axes_show([ax1, ax2])
def draw(stock, query=Query(-130), ma1_n=5, ma2_n=10, ma3_n=20, ma4_n=60, ma5_n=100, vma1_n=5, vma2_n=10): """绘制普通K线图 + 成交量(成交金额)""" kdata = stock.get_kdata(query) close = CLOSE(kdata, ) ma1 = MA(close, ma1_n) ma2 = MA(close, ma2_n) ma3 = MA(close, ma3_n) ma4 = MA(close, ma4_n) ma5 = MA(close, ma5_n) ax1, ax2 = create_figure(2) kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True, kref=kdata) ma2.plot(axes=ax1, legend_on=True, kref=kdata) ma3.plot(axes=ax1, legend_on=True, kref=kdata) ma4.plot(axes=ax1, legend_on=True, kref=kdata) ma5.plot(axes=ax1, legend_on=True, kref=kdata) sg = SG_Cross(MA(n=ma1_n), MA(n=ma2_n)) sg.to = kdata sg.plot(axes=ax1, kdata=kdata) vol = VOL(kdata) total = len(kdata) x1 = IF(kdata.close > kdata.open, vol, 0) x2 = IF(kdata.close <= kdata.open, vol, 0) x1.bar(axes=ax2, width=0.4, color='r', edgecolor='r', kref=kdata) x2.bar(axes=ax2, width=0.4, color='g', edgecolor='g', kref=kdata) vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True, kref=kdata) vma2.plot(axes=ax2, legend_on=True, kref=kdata) if query.ktype == Query.WEEK and stock.market == 'SH' and stock.code == '000001': CVAL(0.16e+009, total, color='b', linestyle='--', kref=kdata) #ax2.hlines(0.16e+009,0,len(kdata),color='b',linestyle='--') ax_set_locator_formatter(ax1, kdata.get_datetime_list(), kdata.get_query().ktype) adjust_axes_show([ax1, ax2]) return show_gcf()
def 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""" kdata = stock.getKData(query) close = CLOSE(kdata) ma1 = MA(close, ma1_n) ma2 = MA(close, ma2_n) ma3 = MA(close, ma3_n) ma4 = MA(close, ma4_n) ma5 = MA(close, ma5_n) ax1, ax2, ax3 = create_figure(3) kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True) ma2.plot(axes=ax1, legend_on=True) ma3.plot(axes=ax1, legend_on=True) ma4.plot(axes=ax1, legend_on=True) ma5.plot(axes=ax1, legend_on=True) vol = VOL(kdata) total = len(kdata) rg = range(total) x = [i - 0.2 for i in rg] x1 = [x[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] y1 = [vol[i] for i in rg if kdata[i].closePrice > kdata[i].openPrice] x2 = [x[i] for i in rg if kdata[i].closePrice < kdata[i].openPrice] y2 = [vol[i] for i in rg if kdata[i].closePrice < kdata[i].openPrice] ax2.bar(x1, y1, width=0.4, color='r', edgecolor='r') ax2.bar(x2, y2, width=0.4, color='g', edgecolor='g') vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True) vma2.plot(axes=ax2, legend_on=True) ax_draw_macd(ax3, kdata) ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.getDatetimeList(), kdata.getQuery().kType) adjust_axes_show([ax1, ax2, ax3]) return show_gcf()
def draw(stock, query=QueryByIndex(-130), ma_n=22, ma_w='auto', vigor_n=13): """绘制亚历山大.艾尔德交易系统图形""" kdata = stock.getKData(query) close = CLOSE(kdata) ema = EMA(close, ma_n) sf = SAFTYLOSS(close, 10, 3, 2.0) vigor = VIGOR(kdata, vigor_n) ax1, ax2, ax3 = create_figure(3) kdata.plot(axes=ax1) _draw_ema_pipe(ax1, kdata, ema, n=ma_n, w=ma_w) sf.plot(axes=ax1, color='y', legend_on=True) #ax1.legend(loc='upper left') ax_draw_macd2(ax2, ema, kdata) vigor.plot(axes=ax3, marker='.', color='r', zero_on=True, legend_on=False, text_on=True) u = [i for i in vigor if i > 0 and i != constant.null_price] l = [i for i in vigor if i < 0] umean = mean(u) umax = max(u) lmean = mean(l) lmin = min(l) up = int(umax / umean) lp = int(lmin / lmean) for i in range(up): CVAL(umean * (i + 1), len(kdata)).plot(axes=ax3, color='r', linestyle='--') #ax3.hlines(umean * (i + 1),0,len(kdata),color='r',linestyle='--') for i in range(lp): CVAL(lmean * (i + 1), len(kdata)).plot(axes=ax3, color='g', linestyle='--') #ax3.hlines(lmean * (i + 1),0,len(kdata),color='g',linestyle='--') ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.getDatetimeList(), kdata.getQuery().kType) adjust_axes_show([ax1, ax2, ax3]) return show_gcf()
def 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""" kdata = stock.get_kdata(query) close = CLOSE(kdata) ma1 = MA(close, ma1_n) ma2 = MA(close, ma2_n) ma3 = MA(close, ma3_n) ma4 = MA(close, ma4_n) ma5 = MA(close, ma5_n) ax1, ax2, ax3 = create_figure(3) kdata.plot(axes=ax1) ma1.plot(axes=ax1, legend_on=True, kref=kdata) ma2.plot(axes=ax1, legend_on=True, kref=kdata) ma3.plot(axes=ax1, legend_on=True, kref=kdata) ma4.plot(axes=ax1, legend_on=True, kref=kdata) ma5.plot(axes=ax1, legend_on=True, kref=kdata) vol = VOL(kdata) x1 = IF(kdata.close > kdata.open, vol, 0) x2 = IF(kdata.close <= kdata.open, vol, 0) x1.bar(axes=ax2, width=0.4, color='r', edgecolor='r', kref=kdata) x2.bar(axes=ax2, width=0.4, color='g', edgecolor='g', kref=kdata) vol.bar(axes=ax2, color='r', legend_on=True) vma1 = MA(vol, vma1_n) vma2 = MA(vol, vma2_n) vma1.plot(axes=ax2, legend_on=True) vma2.plot(axes=ax2, legend_on=True) ax_draw_macd(ax3, kdata) ax1.set_xlim((0, len(kdata))) ax_set_locator_formatter(ax1, kdata.get_datetime_list(), kdata.get_query().ktype) adjust_axes_show([ax1, ax2, ax3]) return show_gcf()
def ax_draw_macd2(axes, ref, kdata, n1=12, n2=26, n3=9): """绘制MACD :param axes: 指定的坐标轴 :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult( 2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f' % ( n1, n2, n3, fmacd[-1], smacd[-1], bmacd[-1]) #axes.text(0.01,0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x = [i for i in range(total)] y = bmacd y1, y2, y3 = [y[0]], [y[0]], [y[0]] for i in range(1, total): if ref[i] - ref[i - 1] > 0 and y[i] - y[i - 1] > 0: y2.append(y[i]) y1.append('-') y3.append('-') elif ref[i] - ref[i - 1] < 0 and y[i] - y[i - 1] < 0: y3.append(y[i]) y1.append('-') y2.append('-') else: y1.append(y[i]) y2.append('-') y3.append('-') style = gcf().get_style(axes) bar = Bar(subtitle=text, title_pos='10%', title_top='8%') bar.add('1', x, y1, is_stack=True, is_legend_show=False, **style) bar.add('2', x, y2, is_stack=True, is_legend_show=False, **style) bar.add('3', x, y3, is_stack=True, is_legend_show=False, **style) axes.add(bar) fmacd.plot(axes=axes, line_type='dotted') smacd.plot(axes=axes) gcf().add_axis(axes) return gcf()
def ax_draw_macd(axes, kdata, n1=12, n2=26, n3=9): """绘制MACD :param axes: 指定的坐标轴 :param KData kdata: KData :param int n1: 指标 MACD 的参数1 :param int n2: 指标 MACD 的参数2 :param int n3: 指标 MACD 的参数3 """ macd = MACD(CLOSE(kdata), n1, n2, n3) bmacd, fmacd, smacd = macd.getResult(0), macd.getResult(1), macd.getResult( 2) text = 'MACD(%s,%s,%s) DIF:%.2f, DEA:%.2f, BAR:%.2f' % ( n1, n2, n3, fmacd[-1], smacd[-1], bmacd[-1]) axes.text(0.01, 0.97, text, horizontalalignment='left', verticalalignment='top', transform=axes.transAxes) total = len(kdata) x = [i - 0.2 for i in range(total)] x1 = [x[i] for i, d in enumerate(bmacd) if d > 0] y1 = [i for i in bmacd if i > 0] x2 = [x[i] for i, d in enumerate(bmacd) if d <= 0] y2 = [i for i in bmacd if i <= 0] axes.bar(x1, y1, width=0.4, color='r', edgecolor='r') axes.bar(x2, y2, width=0.4, color='g', edgecolor='g') axt = axes.twinx() axt.grid(False) axt.set_yticks([]) fmacd.plot(axes=axt, linestyle='--', legend_on=False, text_on=False) smacd.plot(axes=axt, legend_on=False, text_on=False) for label in axt.get_xticklabels(): label.set_visible(False)
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 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()