Esempio n. 1
0
def DrawClosesAndVolumes(pl, closes, volumes, zz=None, avg=None, trade_index=None,\
                         title=None, closes_dp=None, closes_bankuai=None):
    """画closes,非df模式,
    closes_dp: 大盘
    closes_bankuai: 板块
    """
    legend = []
    pl.figure
    pl.subplot(211)
    if title != None:
        pl.title(title, fontproperties=getFont())
    pl.plot(closes)
    legend.append('close')
    if zz is not None:
        DrawZZ(pl, zz, c='r')
    if avg is not None:
        pl.plot(avg)
    if not agl.IsNone(closes_dp):
        pl.plot(closes_dp)
        legend.append('dapan')
    if not agl.IsNone(closes_bankuai):
        pl.plot(closes_bankuai)
        legend.append('bankuai')
    if trade_index != None:
        pl, index, ts = pl, trade_index, closes
        _DrawVLine(pl, index, ts)	
    pl.legend(legend, loc='upper left')
    pl.subplot(212)
    pl.plot(volumes)
    pl.show()
    pl.close()
Esempio n. 2
0
def drawKline(pl,
              df_code,
              df_dp=None,
              df_bk=None,
              title=None,
              df_syl=None,
              legend2=None):
    """画个股k线图
    pl: 自定义pl
    df_code: 个股pd.DataFrame, ['holcv']
    df_dp: 大盘
    df_bk: 板块
    df_syl: 历史市盈率
    legend2: 标识, 不支持中文
    """
    if 0: pl = plt
    pl.figure
    pl.subplot(211)
    if title != None:
        pl.title(title, fontproperties=getFont())
    #画个股数据
    df = copy.deepcopy(df_code)
    df['c'] = df['c'] / max(df['c'])
    df['v'] = df['v'] / max(df['v'])
    df['c'].plot()
    df2 = df['v']
    legend = ['close']
    if not isinstance(df_dp, type(None)) and len(df_dp) > 0:
        df = copy.deepcopy(df_dp)
        if isinstance(df, pd.Series):
            df.plot()
        else:
            df['c'] = df['c'] / max(df['c'])
            #df['v'] = df['v'] / max(df['v'])
            df['c'].plot()
        legend.append('dapan')
        #df['v'].plot()
    if not agl.IsNone(df_bk):
        df_bk.plot()
        legend.append('bankuai')
    pl.legend(legend, loc='upper left')
    pl.subplot(212)
    df2.plot()
    legend = ['volumns']
    if not isinstance(df_syl, type(None)) and len(df_syl) > 0:
        df = copy.deepcopy(df_syl)
        #df['市盈率'] = df['市盈率'] / max(df['市盈率'])
        #df['每股收益'] = df['每股收益'] / max(df['每股收益'])
        ##df['市盈率'].plot()
        #df['每股收益'].plot()
        #legend.append('mgsy')
        df.plot()
        legend.append('jll')
    if legend2 is not None:
        legend = legend2
    pl.legend(legend, loc='upper left')
    pl.show()
    pl.close()
Esempio n. 3
0
    def drawKline(df, df_trades=None):
        """画k线图, 异步模式
        需要注意的是如果df比较大的话,那么速度是相当的慢, 一般5日线10天的数据较好
        df : 日线或5分钟线, cols('ohlcv')
        df_trades : 交易点, 需要包含字段cols('trade_bSell', ['trade_price']), 见enum, []意思为不是必须
        """

        def df_to_matplotformat(df):
            """
            return: list[turpl(t,o,h,l,c)]
            """
            data_list = []
            for dates,row in df.iterrows():
                # 将时间转换为数字
                #date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
                t = date2num(dates)
                open,high,low,close = row[:4]
                datas = (t,open,high,low,close)
                data_list.append(datas)	
            return data_list
        quotes = df_to_matplotformat(df)
        plt.cla()
        # 创建一个子图 
        #fig, ax = plt.subplots(facecolor=(0.5, 0.5, 0.5))
        #fig.subplots_adjust(bottom=0.2)
        ax = plt.gca()
        if 0: ax = matplotlib.axes.Axes(fig, rect)
        plt.subplots_adjust(bottom=0.2)
        ## 设置X轴刻度为日期时间
        #ax.xaxis_date()
        ## X轴刻度文字倾斜45度
        #plt.xticks(rotation=45)
        #plt.title("code")
        #plt.xlabel("time")
        #plt.ylabel("price")
        #mpf.candlestick_ohlc(ax,quotes,width=.001,colorup='r',colordown='green')
        #调整下面日期显示的密度
        freq = len(quotes)/20
        weekday_candlestick(quotes, ax, fmt='%b %d %H:%M', freq=freq, width=0.01, colorup='r',colordown='green')
        #plt.grid(True)    

        #画交易点
        if not agl.IsNone(df_trades):
            if 0:df_trades = pd.DataFrame()
            for index, row in df_trades.iterrows():
                if AsynDrawKline.enum.trade_price in row.keys():
                    price = row[AsynDrawKline.enum.trade_price]
                else:
                    price = row['c']
                #交易点
                trade_position = []
                a = np.zeros(len(quotes))
                a[:] = price
                index = len(df[:index])
                a[:index] =np.nan
                bSell = int(row[AsynDrawKline.enum.trade_bSell])
                clr = agl.where(bSell, 'g', 'r')
                plt.plot(a, color=clr, linewidth=0.25)
                plt.text(len(quotes),price, str(price), color=clr)   

        #左右两边都有坐标
        #ax.yaxis.set_ticks_position('both')
        #plt.tick_params(axis='y', which='both', labelleft='on', labelright='on')
        plt.draw()
        plt.pause(0.1)