示例#1
0
def ax_draw_signal(axes, kdata, dates, direct="BUY", style=1):
    """
    """
    refdates = kdata.getDatetimeList()
    date_index = dict([(d, i) for i, d in enumerate(refdates)])
    ylim = axes.get_ylim()
    height = ylim[1] - ylim[0]

    if style == 1:
        arrow = dict(arrowstyle="->")
    else:
        if direct == "BUY":
            arrow = dict(facecolor='red', frac=0.5)
        else:
            arrow = dict(facecolor='blue', frac=0.5)

    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        if direct == "BUY":
            axes.annotate('B', (pos, krecord.lowPrice - height * 0.01),
                          (pos, krecord.lowPrice - height * 0.1),
                          arrowprops=arrow,
                          horizontalalignment='center',
                          verticalalignment='bottom',
                          color='red')
        else:
            axes.annotate('S', (pos, krecord.highPrice + height * 0.01),
                          (pos, krecord.highPrice + height * 0.1),
                          arrowprops=arrow,
                          horizontalalignment='center',
                          verticalalignment='top',
                          color='blue')
示例#2
0
def ax_draw_sys_signal(axes, kdata, sys, style=2):
    """
    """
    refdates = kdata.getDatetimeList()
    date_index = dict([(d, i) for i, d in enumerate(refdates)])
    ylim = axes.get_ylim()
    height = ylim[1] - ylim[0]

    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)

    buy_request = sys.getBuyTradeRequest()
    sell_request = sys.getSellTradeRequest()
    text_request = ''
    if buy_request.valid:
        text_request = u'建议买入  止损: %.2f' % (buy_request.stoploss)
        color = 'r'
    if sell_request.valid:
        text_request = u'建议卖出  来源: %s' % (getSystemPartName(sell_request.part))
        color = 'b'

    if buy_request.valid or sell_request.valid:
        axes.text(0.99,
                  0.03,
                  text_request,
                  horizontalalignment='right',
                  verticalalignment='bottom',
                  transform=axes.transAxes,
                  color=color)

    dates = sys.getTradeRecordList()
    for d in dates:
        if not date_index.has_key(d.datetime):
            continue
        pos = date_index[d.datetime]
        krecord = kdata[pos]
        if d.business == BUSINESS.BUY:
            axes.annotate('SG', (pos, krecord.lowPrice - height * 0.01),
                          (pos, krecord.lowPrice - height * 0.1),
                          arrowprops=arrow_buy,
                          horizontalalignment='center',
                          verticalalignment='bottom',
                          color='red')
        elif d.business == BUSINESS.SELL:
            text = getSystemPartName(d.part)
            axes.annotate(text, (pos, krecord.highPrice + height * 0.01),
                          (pos, krecord.highPrice + height * 0.1),
                          arrowprops=arrow_sell,
                          horizontalalignment='center',
                          verticalalignment='top',
                          color='blue')
        else:
            None
示例#3
0
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,则认为该信号发生器已经指定了交易对象,
                       否则,使用该参数作为交易对象
    """
    kdata = sg.to if kdata is None else kdata
    refdates = kdata.get_datetime_list()
    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()

    ylim = axes.get_ylim()
    height = ylim[1] - ylim[0]

    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)

    dates = sg.get_buy_signal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('B', (pos, krecord.low - height * 0.01),
                      (pos, krecord.low - height * 0.1),
                      arrowprops=arrow_buy,
                      horizontalalignment='center',
                      verticalalignment='bottom',
                      color='red')

    dates = sg.get_sell_signal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('S', (pos, krecord.high + height * 0.01),
                      (pos, krecord.high + height * 0.1),
                      arrowprops=arrow_sell,
                      horizontalalignment='center',
                      verticalalignment='top',
                      color='blue')
示例#4
0
def ibar(indicator, new=True, axes=None, 
         legend_on=False, text_on=False, text_color='k', label=None,
         width=0.4, color='r', edgecolor='r', 
         zero_on=False, *args, **kwargs):
    """绘制indicator柱状图
    
    :param Indicator indicator: Indicator实例
    :param axes:       指定的坐标轴
    :param new:        是否在新窗口中显示,只在没有指定axes时生效
    :param legend_on:  是否打开图例
    :param text_on:    是否在左上角显示指标名称及其参数
    :param text_color: 指标名称解释文字的颜色,默认为黑色
    :param str label:  label显示文字信息,text_on 及 legend_on 为 True 时生效
    :param zero_on:    是否需要在y=0轴上绘制一条直线
    :param width:      Bar的宽度
    :param color:      Bar的颜色
    :param edgecolor:  Bar边缘颜色
    :param args:       pylab plot参数
    :param kwargs:     pylab plot参数
    """
    if not indicator:
        print("indicator is None")
        return
    
    if not axes:
        axes = create_figure() if new else gca()

    if not label:
        label = "%s %.2f" % (indicator.long_name, indicator[-1])
    
    py_indicatr = [ None if x == constant.null_price else x for x in indicator]
    x = [i-0.2 for i in range(len(indicator))]
    y = py_indicatr
    
    axes.bar(x, py_indicatr, width=width, color=color, edgecolor=edgecolor, 
             *args, **kwargs)
        
    if legend_on:
        leg = axes.legend(loc='upper left')
        leg.get_frame().set_alpha(0.5)
        
    if text_on:
        if not axes.texts:
            axes.text(0.01,0.97, label, horizontalalignment='left', verticalalignment='top', 
                      transform=axes.transAxes, color=text_color)
        else:
            temp_str = axes.texts[0].get_text() + '  ' + label
            axes.texts[0].set_text(temp_str)
        
    if zero_on:
        ylim = axes.get_ylim()
        if ylim[0]<0<ylim[1]:
            axes.hlines(0,0,len(indicator))

    axes.autoscale_view()
    axes.set_xlim(-1, len(indicator)+1)
示例#5
0
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.get_date_list()
    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()

    ylim = axes.get_ylim()
    height = ylim[1] - ylim[0]

    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)

    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

    for d in buy_dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate(
            'B', (pos, krecord.low - height * 0.01), (pos, krecord.low - height * 0.1),
            arrowprops=arrow_buy,
            horizontalalignment='center',
            verticalalignment='bottom',
            color='red'
        )

    for d in sell_dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate(
            'S', (pos, krecord.high + height * 0.01), (pos, krecord.high + height * 0.1),
            arrowprops=arrow_sell,
            horizontalalignment='center',
            verticalalignment='top',
            color='blue'
        )
示例#6
0
def ibar(
    indicator,
    new=True,
    axes=None,
    legend_on=False,
    text_on=False,
    text_color='k',
    label=None,
    width=0.4,
    color='r',
    edgecolor='r',
    zero_on=False,
    *args,
    **kwargs
):
    """绘制indicator柱状图
    
    :param Indicator indicator: Indicator实例
    :param axes:       指定的坐标轴
    :param new:        是否在新窗口中显示,只在没有指定axes时生效
    :param legend_on:  是否打开图例
    :param text_on:    是否在左上角显示指标名称及其参数
    :param text_color: 指标名称解释文字的颜色,默认为黑色
    :param str label:  label显示文字信息,text_on 及 legend_on 为 True 时生效
    :param zero_on:    是否需要在y=0轴上绘制一条直线
    :param width:      Bar的宽度
    :param color:      Bar的颜色
    :param edgecolor:  Bar边缘颜色
    :param args:       pylab plot参数
    :param kwargs:     pylab plot参数
    """
    if not indicator:
        print("indicator is None")
        return

    if not axes:
        axes = create_figure() if new else gca()

    if not label:
        label = "%s %.2f" % (indicator.long_name, indicator[-1])

    py_indicatr = [None if x == constant.null_price else x for x in indicator]
    x = [i - 0.2 for i in range(len(indicator))]
    y = py_indicatr

    axes.bar(x, py_indicatr, width=width, color=color, edgecolor=edgecolor, *args, **kwargs)

    if legend_on:
        leg = axes.legend(loc='upper left')
        leg.get_frame().set_alpha(0.5)

    if text_on:
        if not axes.texts:
            axes.text(
                0.01,
                0.97,
                label,
                horizontalalignment='left',
                verticalalignment='top',
                transform=axes.transAxes,
                color=text_color
            )
        else:
            temp_str = axes.texts[0].get_text() + '  ' + label
            axes.texts[0].set_text(temp_str)

    if zero_on:
        ylim = axes.get_ylim()
        if ylim[0] < 0 < ylim[1]:
            axes.hlines(0, 0, len(indicator))

    axes.autoscale_view()
    axes.set_xlim(-1, len(indicator) + 1)
    k = indicator.get_context()
    if len(k) > 0:
        ax_set_locator_formatter(axes, k.get_date_list(), k.get_query().ktype)
示例#7
0
def iplot(indicator,
          new=True,
          axes=None,
          legend_on=False,
          text_on=False,
          text_color='k',
          zero_on=False,
          label=None,
          *args,
          **kwargs):
    """绘制indicator曲线
    
    :param Indicator indicator: indicator实例
    :param axes:            指定的坐标轴
    :param new:             是否在新窗口中显示,只在没有指定axes时生效
    :param legend_on:       是否打开图例
    :param text_on:         是否在左上角显示指标名称及其参数
    :param text_color:      指标名称解释文字的颜色,默认为黑色
    :param zero_on:         是否需要在y=0轴上绘制一条直线
    :param str label:       label显示文字信息,text_on 及 legend_on 为 True 时生效
    :param args:            pylab plot参数
    :param kwargs:          pylab plot参数,如:marker(标记类型)、
                             markerfacecolor(标记颜色)、
                             markeredgecolor(标记的边缘颜色)
    """
    if not indicator:
        print("indicator is None")
        return

    if not axes:
        axes = create_figure() if new else gca()

    if not label:
        label = "%s %.2f" % (indicator.long_name, indicator[-1])

    py_indicatr = [None if x == constant.null_price else x for x in indicator]
    axes.plot(py_indicatr, '-', label=label, *args, **kwargs)

    if legend_on:
        leg = axes.legend(loc='upper left')
        leg.get_frame().set_alpha(0.5)

    if text_on:
        if not axes.texts:
            axes.text(0.01,
                      0.97,
                      label,
                      horizontalalignment='left',
                      verticalalignment='top',
                      transform=axes.transAxes,
                      color=text_color)
        else:
            temp_str = axes.texts[0].get_text() + '  ' + label
            axes.texts[0].set_text(temp_str)

    if zero_on:
        ylim = axes.get_ylim()
        if ylim[0] < 0 < ylim[1]:
            axes.hlines(0, 0, len(indicator))

    axes.autoscale_view()
    axes.set_xlim(-1, len(indicator) + 1)
示例#8
0
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()
    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()        
            
    ylim = axes.get_ylim()
    height = ylim[1]-ylim[0]
    
    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)  

    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
    
    for d in buy_dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('B', 
                      (pos, krecord.lowPrice - height*0.01), 
                      (pos, krecord.lowPrice - height*0.1), 
                      arrowprops = arrow_buy,
                      horizontalalignment = 'center', 
                      verticalalignment = 'bottom',
                      color='red')
              
    for d in sell_dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('S', 
                      (pos, krecord.highPrice + height*0.01), 
                      (pos, krecord.highPrice + height*0.1), 
                      arrowprops = arrow_sell,
                      horizontalalignment = 'center', 
                      verticalalignment = 'top',
                      color='blue')            
示例#9
0
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()
    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()        
            
    ylim = axes.get_ylim()
    height = ylim[1]-ylim[0]
    
    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)  

    dates = sg.getBuySignal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('B', 
                      (pos, krecord.lowPrice - height*0.01), 
                      (pos, krecord.lowPrice - height*0.1), 
                      arrowprops = arrow_buy,
                      horizontalalignment = 'center', 
                      verticalalignment = 'bottom',
                      color='red')
              
    dates = sg.getSellSignal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('S', 
                      (pos, krecord.highPrice + height*0.01), 
                      (pos, krecord.highPrice + height*0.1), 
                      arrowprops = arrow_sell,
                      horizontalalignment = 'center', 
                      verticalalignment = 'top',
                      color='blue')            
示例#10
0
def sgplot(sg, kdata=None, style=1, axes=None, new=True):
    """
    绘制买入/卖出信号
    参数:
        sg:   信号发生器
        kdata:指定的KData(即信号发生器的交易对象),如该值为None,则认为该信号
               发生器已经指定了交易对象,否则,使用该参数作为交易对象
        style: 1 | 2 信号箭头绘制样式
        axes: 指定在那个轴对象中进行绘制
        new:  仅在未指定axes的情况下生效,当为True时,创建新的窗口对象并在其中进行绘制
    """
    if kdata is None:
        kdata = sg.getTO()
    else:
        sg.setTO(kdata)

    refdates = kdata.getDatetimeList()
    date_index = dict([(d, i) for i, d in enumerate(refdates)])

    if axes is None:
        if new:
            axes = create_one_axes_figure()
            kplot(kdata, axes=axes)
        else:
            axes = gca()

    ylim = axes.get_ylim()
    height = ylim[1] - ylim[0]

    if style == 1:
        arrow_buy = dict(arrowstyle="->")
        arrow_sell = arrow_buy
    else:
        arrow_buy = dict(facecolor='red', frac=0.5)
        arrow_sell = dict(facecolor='blue', frac=0.5)

    dates = sg.getBuySignal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('B', (pos, krecord.lowPrice - height * 0.01),
                      (pos, krecord.lowPrice - height * 0.1),
                      arrowprops=arrow_buy,
                      horizontalalignment='center',
                      verticalalignment='bottom',
                      color='red')

    dates = sg.getSellSignal()
    for d in dates:
        if d not in date_index:
            continue
        pos = date_index[d]
        krecord = kdata[pos]
        axes.annotate('S', (pos, krecord.highPrice + height * 0.01),
                      (pos, krecord.highPrice + height * 0.1),
                      arrowprops=arrow_sell,
                      horizontalalignment='center',
                      verticalalignment='top',
                      color='blue')
示例#11
0
def ibar(indicator,
         axes=None,
         width=0.4,
         color='r',
         edgecolor='r',
         new=True,
         legend_on=False,
         text_on=False,
         text_color='k',
         label=None,
         zero_on=False,
         *args,
         **kwargs):
    """绘制indicator曲线
    indicator: Indicator实例
    axes: 指定的坐标轴
    new: 是否在新窗口中显示,只在没有指定axes时生效
    legend_on : 是否打开图例
    text_on: 是否在左上角显示指标名称及其参数
    text_color: 指标名称解释文字的颜色,默认为黑色
    zero_on: 是否需要在y=0轴上绘制一条直线
    *args, **kwargs : pylab bar参数
    """
    if not indicator:
        print("indicator is None")
        return

    if not axes:
        axes = create_one_axes_figure() if new else gca()

    if not label:
        label = "%s %.2f" % (indicator.long_name, indicator[-1])

    py_indicatr = [None if x == constant.null_price else x for x in indicator]
    x = [i - 0.2 for i in range(len(indicator))]
    y = py_indicatr

    axes.bar(x,
             py_indicatr,
             width=width,
             color=color,
             edgecolor=edgecolor,
             *args,
             **kwargs)

    if legend_on:
        leg = axes.legend(loc='upper left')
        leg.get_frame().set_alpha(0.5)

    if text_on:
        if not axes.texts:
            axes.text(0.01,
                      0.97,
                      label,
                      horizontalalignment='left',
                      verticalalignment='top',
                      transform=axes.transAxes,
                      color=text_color)
        else:
            temp_str = axes.texts[0].get_text() + '  ' + label
            axes.texts[0].set_text(temp_str)

    if zero_on:
        ylim = axes.get_ylim()
        if ylim[0] < 0 < ylim[1]:
            axes.hlines(0, 0, len(indicator))

    axes.autoscale_view()
    axes.set_xlim(-1, len(indicator) + 1)