예제 #1
0
def kplot(kdata, new=True, axes=None, 
          colorup='r', colordown='g', width=0.6, alpha=1.0):
    """绘制K线图
    
    :param KData kdata: K线数据
    :param bool new:    是否在新窗口中显示,只在没有指定axes时生效
    :param axes:        指定的坐标轴
    :param colorup:     the color of the rectangle where close >= open
    :param colordown:   the color of the rectangle where close < open
    :param width:       fraction of a day for the rectangle width
    :param alpha:       the rectangle alpha level, 透明度(0.0~1.0) 1.0为不透明
    """
    if not kdata:
        print("kdata is None")
        return
    
    if not axes:
        axes = create_figure() if new else gca()
        
    OFFSET = width/2.0
    rfcolor = matplotlib.rcParams['axes.facecolor']
    for i in range(len(kdata)):
        record = kdata[i]
        open, high, low, close = record.openPrice, record.highPrice, record.lowPrice, record.closePrice
        if close>=open:
            color = colorup
            lower = open
            height = close-open
            rect = Rectangle(xy=(i-OFFSET, lower), width=width, height=height, facecolor=rfcolor, edgecolor=color)
        else:
            color = colordown
            lower = close
            height = open-close
            rect = Rectangle(xy=(i-OFFSET, lower), width=width, height=height, facecolor=color, edgecolor=color)

        vline1 = Line2D(xdata=(i, i), ydata=(low, lower), color=color, linewidth=0.5, antialiased=True)
        vline2 = Line2D(xdata=(i, i), ydata=(lower+height, high), color=color, linewidth=0.5, antialiased=True)
        rect.set_alpha(alpha)

        axes.add_line(vline1)
        axes.add_line(vline2)
        axes.add_patch(rect)
        
    title = get_draw_title(kdata)
    axes.set_title(title)  
    last_record = kdata[-1]
    color = 'r' if last_record.closePrice>kdata[-2].closePrice else 'g'
    text = u'%s 开:%.2f 高:%.2f 低:%.2f 收:%.2f 涨幅:%.2f%%' % (
        last_record.datetime.number/10000, 
        last_record.openPrice, last_record.highPrice,
        last_record.lowPrice,  last_record.closePrice,
        100*(last_record.closePrice-kdata[-2].closePrice)/kdata[-2].closePrice)
    axes.text(0.99,0.97, text, horizontalalignment='right', verticalalignment='top', 
              transform=axes.transAxes, color=color)
        
    axes.autoscale_view()
    axes.set_xlim(-1, len(kdata)+1)
    ax_set_locator_formatter(axes, kdata.getDatetimeList(), kdata.getQuery().kType)
예제 #2
0
def mkplot(kdata, new=True, axes=None, colorup='r', colordown='g', ticksize=3):
    """绘制美式K线图
    
    :param KData kdata: K线数据
    :param bool new:    是否在新窗口中显示,只在没有指定axes时生效
    :param axes:        指定的坐标轴
    :param colorup:     the color of the lines where close >= open
    :param colordown:   the color of the lines where close < open
    :param ticksize:    open/close tick marker in points
    """
    if not kdata:
        print("kdata is None")
        return

    if not axes:
        axes = create_figure() if new else gca()
        
    for t in range(len(kdata)):
        record = kdata[t]
        open, high, low, close = record.openPrice, record.highPrice, record.lowPrice, record.closePrice 
        color = colorup if close>=open else colordown

        vline = Line2D(xdata=(t, t), ydata=(low, high), color=color, antialiased=False)
        oline = Line2D(xdata=(t, t), ydata=(open, open), color=color, antialiased=False,
                       marker=TICKLEFT, markersize=ticksize)
        cline = Line2D(xdata=(t, t), ydata=(close, close), color=color, antialiased=False,
                       markersize=ticksize, marker=TICKRIGHT)

        axes.add_line(vline)
        axes.add_line(oline)
        axes.add_line(cline)
        
    title = get_draw_title(kdata)
    axes.set_title(title)  
    last_record = kdata[-1]
    color = 'r' if last_record.closePrice>kdata[-2].closePrice else 'g'
    text = u'%s 开:%.2f 高:%.2f 低:%.2f 收:%.2f' % (last_record.datetime.number/10000, 
                                                last_record.openPrice, 
                                                last_record.highPrice,
                                                last_record.lowPrice, 
                                                last_record.closePrice)
    axes.text(0.99,0.97, text, horizontalalignment='right', verticalalignment='top', 
              transform=axes.transAxes, color=color)
        
    axes.autoscale_view()
    #axes.set_xlim(0, len(kdata))
    ax_set_locator_formatter(axes, kdata.getDatetimeList(), kdata.getQuery().kType)
예제 #3
0
def mkplot(kdata, new=True, axes=None, colorup='r', colordown='g', ticksize=3):
    """绘制美式K线图
    
    :param KData kdata: K线数据
    :param bool new:    是否在新窗口中显示,只在没有指定axes时生效
    :param axes:        指定的坐标轴
    :param colorup:     the color of the lines where close >= open
    :param colordown:   the color of the lines where close < open
    :param ticksize:    open/close tick marker in points
    """
    if not kdata:
        print("kdata is None")
        return

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

    for t in range(len(kdata)):
        record = kdata[t]
        open, high, low, close = record.open, record.high, record.low, record.close
        color = colorup if close >= open else colordown

        vline = Line2D(xdata=(t, t), ydata=(low, high), color=color, antialiased=False)
        oline = Line2D(
            xdata=(t, t),
            ydata=(open, open),
            color=color,
            antialiased=False,
            marker=TICKLEFT,
            markersize=ticksize
        )
        cline = Line2D(
            xdata=(t, t),
            ydata=(close, close),
            color=color,
            antialiased=False,
            markersize=ticksize,
            marker=TICKRIGHT
        )

        axes.add_line(vline)
        axes.add_line(oline)
        axes.add_line(cline)

    title = get_draw_title(kdata)
    axes.set_title(title)
    last_record = kdata[-1]
    color = 'r' if last_record.close > kdata[-2].close else 'g'
    text = u'%s 开:%.2f 高:%.2f 低:%.2f 收:%.2f' % (
        last_record.date.number / 10000, last_record.open, last_record.high, last_record.low,
        last_record.close
    )
    axes.text(
        0.99,
        0.97,
        text,
        horizontalalignment='right',
        verticalalignment='top',
        transform=axes.transAxes,
        color=color
    )

    axes.autoscale_view()
    axes.set_xlim(-1, len(kdata) + 1)
    ax_set_locator_formatter(axes, kdata.get_date_list(), kdata.get_query().ktype)
예제 #4
0
def kplot(kdata, new=True, axes=None, colorup='r', colordown='g', width=0.6, alpha=1.0):
    """绘制K线图
    
    :param KData kdata: K线数据
    :param bool new:    是否在新窗口中显示,只在没有指定axes时生效
    :param axes:        指定的坐标轴
    :param colorup:     the color of the rectangle where close >= open
    :param colordown:   the color of the rectangle where close < open
    :param width:       fraction of a day for the rectangle width
    :param alpha:       the rectangle alpha level, 透明度(0.0~1.0) 1.0为不透明
    """
    if not kdata:
        print("kdata is None")
        return

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

    OFFSET = width / 2.0
    rfcolor = matplotlib.rcParams['axes.facecolor']
    for i in range(len(kdata)):
        record = kdata[i]
        open, high, low, close = record.open, record.high, record.low, record.close
        if close >= open:
            color = colorup
            lower = open
            height = close - open
            rect = Rectangle(
                xy=(i - OFFSET, lower),
                width=width,
                height=height,
                facecolor=rfcolor,
                edgecolor=color
            )
        else:
            color = colordown
            lower = close
            height = open - close
            rect = Rectangle(
                xy=(i - OFFSET, lower),
                width=width,
                height=height,
                facecolor=color,
                edgecolor=color
            )

        vline1 = Line2D(
            xdata=(i, i), ydata=(low, lower), color=color, linewidth=0.5, antialiased=True
        )
        vline2 = Line2D(
            xdata=(i, i),
            ydata=(lower + height, high),
            color=color,
            linewidth=0.5,
            antialiased=True
        )
        rect.set_alpha(alpha)

        axes.add_line(vline1)
        axes.add_line(vline2)
        axes.add_patch(rect)

    title = get_draw_title(kdata)
    axes.set_title(title)
    last_record = kdata[-1]
    color = 'r' if last_record.close > kdata[-2].close else 'g'
    text = u'%s 开:%.2f 高:%.2f 低:%.2f 收:%.2f 涨幅:%.2f%%' % (
        last_record.date.number / 10000, last_record.open, last_record.high, last_record.low,
        last_record.close, 100 * (last_record.close - kdata[-2].close) / kdata[-2].close
    )
    axes.text(
        0.99,
        0.97,
        text,
        horizontalalignment='right',
        verticalalignment='top',
        transform=axes.transAxes,
        color=color
    )

    axes.autoscale_view()
    axes.set_xlim(-1, len(kdata) + 1)
    ax_set_locator_formatter(axes, kdata.get_date_list(), kdata.get_query().ktype)
예제 #5
0
파일: drawplot.py 프로젝트: yunfeiz/hikyuu
def mkplot(kdata, ticksize=3, colorup='r', colordown='g', axes=None, new=True):
    """绘制美式K线图,x轴使用数据的自然索引做下标,并不使用quotes中的time
    kdata       : KData实例
    ticksize    : open/close tick marker in points
    colorup     : the color of the lines where close >= open
    colordown   : the color of the lines where close <  open
    axes        : 指定的坐标轴
    new       : 是否在新窗口中显示,只在没有指定axes时生效
    """
    if not kdata:
        print("kdata is None")
        return

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

    for t in range(len(kdata)):
        record = kdata[t]
        open, high, low, close = record.openPrice, record.highPrice, record.lowPrice, record.closePrice
        color = colorup if close >= open else colordown

        vline = Line2D(xdata=(t, t),
                       ydata=(low, high),
                       color=color,
                       antialiased=False)
        oline = Line2D(xdata=(t, t),
                       ydata=(open, open),
                       color=color,
                       antialiased=False,
                       marker=TICKLEFT,
                       markersize=ticksize)
        cline = Line2D(xdata=(t, t),
                       ydata=(close, close),
                       color=color,
                       antialiased=False,
                       markersize=ticksize,
                       marker=TICKRIGHT)

        axes.add_line(vline)
        axes.add_line(oline)
        axes.add_line(cline)

    title = get_draw_title(kdata)
    axes.set_title(title)
    last_record = kdata[-1]
    color = 'r' if last_record.closePrice > kdata[-2].closePrice else 'g'
    text = u'%s 开:%.2f 高:%.2f 低:%.2f 收:%.2f' % (
        last_record.datetime.number / 10000, last_record.openPrice,
        last_record.highPrice, last_record.lowPrice, last_record.closePrice)
    axes.text(0.99,
              0.97,
              text,
              horizontalalignment='right',
              verticalalignment='top',
              transform=axes.transAxes,
              color=color)

    axes.autoscale_view()
    #axes.set_xlim(0, len(kdata))
    ax_set_locator_formatter(axes, kdata.getDatetimeList(),
                             kdata.getQuery().kType)