def _create_technical_window(self): self.frame = widgets.TechnicalWidget(self._fig, price_data, height=0.85) axes = self.frame.init_layout(50, 4, 1) ax_candles, ax_volume = axes[0], axes[1] # at most 5 ticks, pruning the upper and lower so they don't overlap # with other ticks ax_volume.yaxis.set_major_locator(widgets.MyLocator(5, prune='both')) # 添加k线和交易信号。 subwidget1 = widgets.FrameWidget(axes[0], "subwidget1", 100, 50) candles = Candles(price_data, None, 'candles') subwidget1.add_plotter(candles, False) self.candle_widget = self.frame.add_widget(0, subwidget1, True) ## 添加指标 #self.frame.add_technical(0, MA(price_data.close, 20, 'MA20', 'y', 2)) #self.frame.add_technical(0, MA(price_data.close, 30, 'MA30', 'b', 2)) #self.frame.add_technical(1, Volume(price_data.open, price_data.close, price_data.vol)) self.frame.draw_widgets()
def plot_strategy(price_data, indicators={}, deals=[], curve=[], marks=[]): """ 显示回测结果。 """ print "plotting.." fig = plt.figure() frame = widgets.TechnicalWidget(fig, price_data) axes = frame.init_layout( 50, # 窗口显示k线数量。 4, 1 # 两个1:1大小的窗口 ) # 绘制第一个窗口 # 添加k线 subwidget1 = widgets.FrameWidget(axes[0], "subwidget1", 100, 50) candles = Candles(price_data, None, 'candles') subwidget1.add_plotter(candles, False) #subwidget1.plot(price_data) # 交易信号。 if deals: signal = mplots.TradingSignalPos(price_data, deals, lw=2) subwidget1.add_plotter(signal, False) if len(curve) > 0: curve = Line(curve) subwidget1.add_plotter(curve, True) # 添加指标 for name, indic in indicators.iteritems(): subwidget1.add_plotter(indic, False) # 绘制第2个窗口 subwidget2 = widgets.FrameWidget(axes[1], "subwidget2", 100, 50) volume_plotter = Volume(price_data.open, price_data.close, price_data.volume) subwidget2.add_plotter(volume_plotter, False) subwidgets = [subwidget1, subwidget2] ### 绘制标志 if marks: if marks[0]: # plot lines for name, values in marks[0].iteritems(): v = values[0] ith_ax = v[0] twinx = v[1] line_pieces = [[v[2]], [v[3]], v[4], v[5], v[6]] line = [] for v in values[1:]: ## @TODO 如果是带“点”的,以点的特征聚类,会减少绘图对象的数目 x, y, style, lw, ms = v[2], v[3], v[4], v[5], v[6] if style != line_pieces[2] or lw != line_pieces[ 3] or ms != line_pieces[4]: line.append(line_pieces) line_pieces = [[x], [y], style, lw, ms] else: line_pieces[0].append(x) line_pieces[1].append(y) line.append(line_pieces) for v in line: ## @TODO 这里的sytle明确指出有点奇怪,不一致。 x, y, style, lw, marksize = v[0], v[1], v[2], v[3], v[4] curve = LineWithX(x, y, style=style, lw=lw, ms=marksize) subwidgets[ith_ax].add_plotter(curve, twinx) if marks[1]: # plot texts for name, values in marks[1].iteritems(): for v in values: ith_ax, x, y, text = v[0], v[1], v[2], v[3] color, size, rotation = v[4], v[5], v[6] ## @TODO move to text plotter frame.plot_text(name, ith_ax, x, y, text, color, size, rotation) frame.add_widget(0, subwidget1, True) frame.add_widget(1, subwidget2, True) frame.draw_widgets() plt.show()
# -*- coding: utf-8 -*- #import os, sys #sys.path.append(os.path.join('..', '..')) import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt from quantdigger.widgets.mplotwidgets import widgets from quantdigger.technicals.common import MA, Volume import pandas as pd price_data = pd.read_csv('./data/IF000.csv', index_col=0, parse_dates=True) print len(price_data) fig = plt.figure() frame = widgets.TechnicalWidget(fig, price_data, height=0.85) frame.init_layout(50, 4, 1) ax_candles, ax_volume = frame.get_subwidgets() # 添加k线和交易信号。 kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50) candle_widget = frame.add_widget(0, kwindow, True) # 添加指标 ma = frame.add_technical(0, MA(price_data.close, 20, 'MA20', 'y', 2)) frame.add_technical(0, MA(price_data.close, 30, 'MA30', 'b', 2)) frame.add_technical(1, Volume(price_data.open, price_data.close, price_data.vol)) frame.draw_widgets() # at most 5 ticks, pruning the upper and lower so they don't overlap # with other ticks
if tr['islong']: c = 'r' if tr['exit_price'] > tr['entry_price'] else 'b' else: c = 'r' if tr['exit_price'] < tr['entry_price'] else 'b' r, g, b = colorConverter.to_rgb(c) colors.append((r, g, b, 1)) return price_data, entry_x, entry_y, exit_x, exit_y, colors #price_data, entry_x, entry_y, exit_x, exit_y, colors = get_stock_signal_data() price_data = pd.read_csv('./work/IF111.csv', index_col=0, parse_dates=True) #import matplotlib.font_manager as font_manager print len(price_data) fig = plt.figure() frame = widgets.TechnicalWidget(fig, price_data) frame.init_layout(50, 4, 1) ax_candles, ax_volume = frame.get_subwidgets() # 添加k线和交易信号。 kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50) candle_widget = frame.add_widget(0, kwindow, True) #signal = mplots.TradingSignal(None, zip(zip(entry_x,entry_y),zip(exit_x,exit_y)), c=colors, lw=2) #frame.add_technical(0, signal) # 添加指标 ma = frame.add_technical(0, MA(price_data.close, 20, 'MA20', 'y', 2)) frame.add_technical(0, MA(price_data.close, 30, 'MA30', 'b', 2)) #frame.add_technical(1, RSI(None, price_data.close, 14, name='RSI', fillcolor='b')) frame.add_technical(1, Volume(price_data.open, price_data.close, price_data.vol))
def plot_strategy(price_data, indicators={}, deals=[], curve=[], marks=[]): """ 显示回测结果。 """ print "plotting.." fig = plt.figure() frame = widgets.TechnicalWidget(fig, price_data) frame.init_layout( 50, # 窗口显示k线数量。 4, 1 # 两个1:1大小的窗口 ) # 添加k线 kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50) frame.add_widget(0, kwindow, True) # 交易信号。 if deals: signal = mplots.TradingSignalPos(price_data, deals, lw=2) frame.add_technical(0, signal) if len(curve) > 0: curve = Line(curve) frame.add_technical(0, curve, True) frame.add_technical( 1, Volume(price_data.open, price_data.close, price_data.volume)) ## 添加指标 for name, indic in indicators.iteritems(): frame.add_technical(0, indic) ## 绘制标志 if marks: if marks[0]: # plot lines for name, values in marks[0].iteritems(): v = values[0] ith_ax = v[0] twinx = v[1] line_pieces = [[v[2]], [v[3]], v[4], v[5], v[6]] line = [] for v in values[1:]: ## @TODO 如果是带“点”的,以点的特征聚类,会减少indicator对象的数目 x, y, style, lw, ms = v[2], v[3], v[4], v[5], v[6] if style != line_pieces[2] or lw != line_pieces[ 3] or ms != line_pieces[4]: line.append(line_pieces) line_pieces = [[x], [y], style, lw, ms] else: line_pieces[0].append(x) line_pieces[1].append(y) line.append(line_pieces) for v in line: ## @TODO 这里的sytle明确指出有点奇怪,不一致。 x, y, style, lw, marksize = v[0], v[1], v[2], v[3], v[4] curve = LineWithX(x, y, style=style, lw=lw, ms=marksize) frame.add_technical(ith_ax, curve, twinx) if marks[1]: # plot texts for name, values in marks[1].iteritems(): for v in values: ith_ax, x, y, text = v[0], v[1], v[2], v[3] color, size, rotation = v[4], v[5], v[6] frame.plot_text(name, ith_ax, x, y, text, color, size, rotation) frame.draw_widgets() plt.show()