Пример #1
0
def plot_strategy(price_data, indicators={}, deals=[], curve=[], marks=[]):
    """
        显示回测结果。
    """
    print "plotting.."
    fig = plt.figure()
    frame = widgets.MultiWidgets(
        fig, price_data,
        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_indicator(0, signal)
    if len(curve) > 0:
        curve = Line(curve)
        frame.add_indicator(0, curve, True)
    frame.add_indicator(1, Volume(price_data.open, price_data.close, price_data.volume))
    ## 添加指标
    for name, indic in indicators.iteritems():
        frame.add_indicator(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_indicator(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()
Пример #2
0
def plot_strategy(price_data, indicators={ }, deals=[], curve=[]):
    """ 
        显示回测结果。
    """
    print "plotting.."
    fig = plt.figure()
    frame = widgets.MultiWidgets(fig, price_data,
                                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_indicator(0, signal)
    if len(curve) > 0:
        curve = EquityCurve(curve)
        frame.add_indicator(0, curve, True)
    ## @bug indicators导致的双水平线!
    ## @todo 完mplot_demo上套。
    #frame.add_indicator(0, Volume(None, price_data.open, price_data.close, price_data.volume))

    ## 添加指标
    for name, indic in indicators.iteritems():
        frame.add_indicator(0, indic)
    frame.draw_widgets()
    plt.show()
Пример #3
0
def plot_result(price_data, indicators, signals, blotter):
    """ 
        显示回测结果。
    """
    try:
        curve = statics.create_equity_curve_dataframe(blotter.all_holdings)
        print statics.output_summary_stats(curve)

        fig = plt.figure()
        frame = widgets.MultiWidgets(
            fig,
            price_data,
            50  # 窗口显示k线数量。
        )

        # 添加k线
        kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50)
        frame.add_widget(0, kwindow, True)
        # 交易信号。
        signal = mplots.TradingSignalPos(None, price_data, signals, lw=2)
        frame.add_indicator(0, signal)

        # 添加指标
        k_axes, = frame
        for indic in indicators:
            indic.plot(k_axes)
        frame.draw_widgets()

        fig2 = plt.figure()
        ax = fig2.add_axes((0.1, 0.1, 0.9, 0.9))
        ax.plot(curve.equity)
        plt.show()

    except Exception, e:
        print(e)
Пример #4
0
def plot_strategy(price_data, indicators={}, deals=[], curve=[], marks=[]):
    """
        显示回测结果。
    """
    print "plotting.."
    fig = plt.figure()
    frame = widgets.MultiWidgets(
        fig,
        price_data,
        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_indicator(0, signal)
    if len(curve) > 0:
        curve = Line(curve)
        frame.add_indicator(0, curve, True)
    frame.add_indicator(
        1, Volume(price_data.open, price_data.close, price_data.volume))
    ## 添加指标
    for name, indic in indicators.iteritems():
        frame.add_indicator(0, indic)
    # 绘制标志
    if marks:
        if marks[0]:
            # plot lines
            for name, values in marks[0].iteritems():
                v = values[0]
                line_pieces = [[v[0]], [v[1]], v[2], v[3], v[4]]
                line = []
                for v in values[1:]:
                    ## @TODO 如果是带“点”的,以点的特征聚类,会减少indicator对象的数目
                    if v[2] != line_pieces[2] or v[3] != line_pieces[3] or v[
                            4] != line_pieces[4]:
                        line.append(line_pieces)
                        line_pieces = [[v[0]], [v[1]], v[2], v[3], v[4]]
                    else:
                        line_pieces[0].append(v[0])
                        line_pieces[1].append(v[1])
                line.append(line_pieces)
                for v in line:
                    ## @TODO 这里的sytle明确指出有点奇怪,不一致。
                    curve = LineWithX(v[0], v[1], style=v[2], lw=v[3], ms=v[4])
                    frame.add_indicator(0, curve, False)
        if marks[1]:
            # plot texts
            for name, values in marks[0].iteritems():
                print name
    frame.draw_widgets()
    plt.show()
Пример #5
0
def plot_result(price_data, indicators, signals, blotter):
    """ 
        显示回测结果。
    """
    print "summary.."
    dts = map(lambda x: datetime.fromtimestamp(x / 1000), price_data.index)
    price_data.index = dts
    print dts[-1]
    curve = finance.create_equity_curve_dataframe(blotter.all_holdings)
    print finance.output_summary_stats(curve)

    print "plotting.."
    fig = plt.figure()
    frame = widgets.MultiWidgets(
        fig,
        price_data,
        50  # 窗口显示k线数量。
        #4, 1     # 两个1:1大小的窗口
    )

    # 添加k线
    kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50)
    frame.add_widget(0, kwindow, True)
    ## 交易信号。
    signal = mplots.TradingSignalPos(None, price_data, signals, lw=2)
    frame.add_indicator(0, signal)
    ## @bug indicators导致的双水平线!
    ## @todo 完mplot_demo上套。
    #frame.add_indicator(0, Volume(None, price_data.open, price_data.close, price_data.volume))

    ## 添加指标
    for indic in indicators:
        frame.add_indicator(0, indic)

    frame.draw_widgets()

    # 画资金曲线
    #print curve.equity
    #fig2 = plt.figure()
    #ax = fig2.add_axes((0.1, 0.1, 0.8, 0.8))
    #ax.xaxis.set_major_formatter(TimeFormatter(curve.index, '%Y-%m-%d' ))
    #ax.get_yaxis().get_major_formatter().set_useOffset(False)
    ##ax.get_yaxis().get_major_formatter().set_scientific(False)
    #ax.set_xticks(xticks_to_display(len(curve)))
    #ax.plot(curve.equity)
    plt.show()
Пример #6
0
def plot_result(price_data, indicators, signals, blotter):
    """ 
        显示回测结果。
    """
    try:
        curve = finance.create_equity_curve_dataframe(blotter.all_holdings)
        print finance.output_summary_stats(curve)

        fig = plt.figure()
        frame = widgets.MultiWidgets(
            fig,
            price_data,
            50  # 窗口显示k线数量。
        )

        # 添加k线
        kwindow = widgets.CandleWindow("kwindow", price_data, 100, 50)
        frame.add_widget(0, kwindow, True)
        # 交易信号。
        signal = mplots.TradingSignalPos(None, price_data, signals, lw=2)
        frame.add_indicator(0, signal)

        # 添加指标
        k_axes, = frame
        for indic in indicators:
            indic.plot(k_axes)
        frame.draw_widgets()

        fig2 = plt.figure()
        ax = fig2.add_axes((0.1, 0.1, 0.8, 0.8))
        delta = curve.index[1] - curve.index[0]
        ax.xaxis.set_major_formatter(TimeFormatter(curve.index, '%Y-%m-%d'))
        ax.set_xticks(xticks_to_display(len(curve)))
        ax.plot(curve.equity)
        plt.show()

    except Exception, e:
        print(e)
Пример #7
0
        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('./data/IF000.csv', index_col=0, parse_dates=True)
#import matplotlib.font_manager as font_manager
print len(price_data)
fig = plt.figure()
frame = widgets.MultiWidgets(fig, price_data, 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_indicator(0, signal)

# 添加指标
ma = frame.add_indicator(0, MA(price_data.close, 20, 'MA20', 'y', 2))
frame.add_indicator(0, MA(price_data.close, 30, 'MA30', 'b', 2))
#frame.add_indicator(1, RSI(None, price_data.close, 14, name='RSI', fillcolor='b'))
frame.add_indicator(1, Volume(price_data.open, price_data.close, price_data.vol))
frame.draw_widgets()