Ejemplo n.º 1
0
def longsklearn(code='999999', ptype='f', dtype='d', start=None, end=None):
    # code='999999'
    # dtype = 'w'
    # start = '2014-09-01'
    # start = None
    # end='2015-12-23'
    # end = None
    df = tdd.get_tdx_append_now_df(code, ptype, start,
                                   end).sort_index(ascending=True)
    # if not dtype == 'd':
    # df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True)
    dw = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True)
    # print df[:1]
    h = df.loc[:, ['open', 'close', 'high', 'low']]
    highp = h['high'].values
    lowp = h['low'].values
    openp = h['open'].values
    closep = h['close'].values
    lr = LinearRegression()
    x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T
    lr.fit(x, closep)
    LinearRegression(copy_X=True,
                     fit_intercept=True,
                     n_jobs=1,
                     normalize=False)
    xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T
    yt = lr.predict(xt)
    # plt.plot(xt,yt,'-g',linewidth=5)
    # plt.plot(closep)
    bV = []
    bP = []

    uV = []
    uP = []
    for i in range(1, len(highp) - 1):
        # if highp[i] <= highp[i - 1] and highp[i] < highp[i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]:
        if lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]:
            bV.append(lowp[i])
            bP.append(i)

    for i in range(1, len(highp) - 1):
        # if highp[i] >= highp[i - 1] and highp[i] > highp[i + 1] and lowp[i] >= lowp[i - 1] and lowp[i] > lowp[i + 1]:
        if highp[i] >= highp[i - 1] and highp[i] > highp[i + 1]:
            uV.append(highp[i])
            uP.append(i)
    print(highp)
    print("uV:%s" % uV[:1])
    print("uP:%s" % uP[:1])
    print("bV:%s" % bV[:1])
    print("bP:%s" % bP[:1])

    sV, sP = LIS(uV)
    dV, dP = LIS(bV)
    print("sV:%s" % sV[:1])
    print("sP:%s" % sP[:1])
    print("dV:%s" % dV[:1])
    print("dP:%s" % dP[:1])
    sidx = []
    didx = []
    for i in range(len(sP)):
        # idx.append(bP[p[i]])
        sidx.append(uP[sP[i]])
    for i in range(len(dP)):
        # idx.append(bP[p[i]])
        didx.append(bP[dP[i]])

    print("sidx:%s" % sidx[:1])
    print("didx:%s" % didx[:1])

    # plt.plot(closep)
    # plt.plot(idx,d,'ko')
    lr = LinearRegression()
    X = np.atleast_2d(np.array(sidx)).T
    Y = np.array(sV)
    lr.fit(X, Y)
    estV = lr.predict(xt)

    fig = plt.figure(figsize=(16, 10), dpi=72)
    # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
    plt.subplots_adjust(left=0.05,
                        bottom=0.08,
                        right=0.95,
                        top=0.95,
                        wspace=0.15,
                        hspace=0.25)
    # set (gca,'Position',[0,0,512,512])
    # fig.set_size_inches(18.5, 10.5)
    # fig=plt.fig(figsize=(14,8))
    ax = fig.add_subplot(111)
    plt.grid(True)
    # print h.index[:5], h['close']
    ax = h['close'].plot()
    # ax.plot(pd.datetime(h.index),h['close'], linewidth=1)
    # ax.plot(uP, uV, linewidth=1)
    # ax.plot(uP, uV, 'ko')
    # ax.plot(bP, bV, linewidth=1)
    # ax.plot(bP, bV, 'bo')
    #    # ax.plot(sP, sV, linewidth=1)
    #    # ax.plot(sP, sV, 'yo')
    # ax.plot(sidx, sV, linewidth=1)
    # ax.plot(sidx, sV, 'ro')
    # ax.plot(didx, dV, linewidth=1)
    # ax.plot(didx, dV, 'co')
    df['mean'] = list(
        map(lambda h, l: (h + l) / 2, df.high.values, df.low.values))
    print(df['mean'][:1])
    # d=df.mean
    dw = dw.set_index('date')
    # print dw[:2]
    # ax.plot(df.index,df['mean'],'g',linewidth=1)
    ax.plot(df.index, pd.rolling_mean(df['mean'], 60), 'g', linewidth=1)
    ax.plot(dw.index, pd.rolling_mean(dw.close, 5), 'r', linewidth=1)
    ax.plot(dw.index, pd.rolling_min(dw.close, 5), 'bo')
    ax.plot(dw.index, pd.rolling_max(dw.close, 5), 'yo')
    ax.plot(dw.index, pd.expanding_max(dw.close, 5), 'ro')
    ax.plot(dw.index, pd.expanding_min(dw.close, 5), 'go')
    # print pd.rolling_min(df.close,20)[:1],pd.rolling_min(df.close,20)[-1:]
    # print pd.rolling_min(df.close,20)
    # print pd.rolling_max(df.close,20)[:1],pd.rolling_max(df.close,20)[-1:]
    # print pd.rolling_max(df.close,20)

    # ax.plot(idx, d, 'ko')
    # ax.plot(xt, estV, '-r', linewidth=5)
    # ax.plot(xt, yt, '-g', linewidth=5)

    # ax2 = fig.add_subplot(122)
    # print len(closep),len(idx),len(d),len(xt),len(estV),len(yt)
    # f=lambda x:x[-int(len(x)/10):]
    # ax2.plot(f(closep))
    # ax2.plot(f(idx),f(d),'ko')
    # ax2.plot(f(xt),f(estV),'-r',linewidth=5)
    # ax2.plot(f(xt),f(yt),'-g',linewidth=5)
    # # plt.show()
    scale = 1.1
    zp = zoompan.ZoomPan()
    figZoom = zp.zoom_factory(ax, base_scale=scale)
    figPan = zp.pan_factory(ax)
    show()
Ejemplo n.º 2
0
def show_chan_mpl(code,
                  start_date,
                  end_date,
                  stock_days,
                  resample,
                  show_mpl=True,
                  least_init=3,
                  chanK_flag=False,
                  windows=20):
    def get_least_khl_num(resample, idx=0, init_num=3):
        # init = 3
        if init_num - idx > 0:
            initw = init_num - idx
        else:
            initw = 0
        return init_num if resample == 'd' else initw if resample == 'w' else init_num-idx-1 if init_num-idx-1 >0 else 0\
                if resample == 'm' else 5

    stock_code = code  # 股票代码
    # stock_code = '002176' # 股票代码
    # start_date = '2017-09-05'
    # start_date = None
    # end_date = '2017-10-12 15:00:00'  # 最后生成k线日期
    # end_date = None
    # stock_days = 60  # 看几天/分钟前的k线
    # resample = 'd'
    # resample = 'w'
    x_jizhun = 3  # window 周期 x轴展示的时间距离  5:日,40:30分钟, 48: 5分钟
    least_khl_num = get_least_khl_num(resample, init_num=least_init)
    # stock_frequency = '5m' # 1d日线, 30m 30分钟, 5m 5分钟,1m 1分钟
    stock_frequency = resample  # 1d日线, 30m 30分钟, 5m 5分钟,1m 1分钟 w:week
    # chanK_flag = chanK  # True 看缠论K线, False 看k线
    # chanK_flag = True  # True 看缠论K线, False 看k线
    show_mpl = show_mpl

    def con2Cxianduan(stock,
                      k_data,
                      chanK,
                      frsBiType,
                      biIdx,
                      end_date,
                      cur_ji=1,
                      recursion=False,
                      dl=None,
                      chanK_flag=False,
                      least_init=3):
        max_k_num = 4
        if cur_ji >= 6 or len(biIdx) == 0 or recursion:
            return biIdx
        idx = biIdx[len(biIdx) - 1]
        k_data_dts = list(k_data.index)
        st_data = chanK['enddate'][idx]
        if st_data not in k_data_dts:
            return biIdx
        # 重构次级别线段的点到本级别的chanK中

        def refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji):
            new_biIdx = []
            biIdxB = biIdx[len(biIdx) - 1] if len(biIdx) > 0 else 0
            for xdIdxcn in xdIdxc:
                for chanKidx in range(len(chanK.index))[biIdxB:]:
                    if judge_day_bao(chanK, chanKidx, chanKc, xdIdxcn, cur_ji):
                        new_biIdx.append(chanKidx)
                        break
            return new_biIdx

        # 判断次级别日期是否被包含

        def judge_day_bao(chanK, chanKidx, chanKc, xdIdxcn, cur_ji):
            _end_date = chanK['enddate'][chanKidx] + datetime.timedelta(
                hours=15) if cur_ji == 1 else chanK['enddate'][chanKidx]
            _start_date = chanK.index[chanKidx] if chanKidx == 0\
                else chanK['enddate'][chanKidx - 1] + datetime.timedelta(minutes=1)
            return _start_date <= chanKc.index[xdIdxcn] <= _end_date

        # cur_ji = 1 #当前级别
        # 符合k线根数大于4根 1日级别, 2 30分钟, 3 5分钟, 4 一分钟
        if not recursion:
            resample = 'd' if cur_ji + 1 == 2 else '5m' if cur_ji + 1 == 3 else \
                'd' if cur_ji + 1 == 5 else 'w' if cur_ji + 1 == 6 else 'd'
        least_khl_num = get_least_khl_num(resample, 1, init_num=least_init)
        print "次级:%s st_data:%s k_data_dts:%s least_khl_num:%s" % (
            len(k_data_dts) - k_data_dts.index(st_data), str(st_data)[:10],
            len(k_data_dts), least_khl_num)
        if cur_ji + 1 != 2 and len(k_data_dts) - k_data_dts.index(
                st_data) >= least_khl_num + 1:
            frequency = '30m' if cur_ji + 1 == 2 else '5m' if cur_ji + 1 == 3 else '1m'
            # else:
            # frequency = 'd' if cur_ji+1==2 else '5m' if cur_ji+1==3 else \
            #                 'd' if cur_ji+1==5 else 'w' if cur_ji+1==6 else 'd'

            start_lastday = str(chanK.index[biIdx[-1]])[0:10]
            print "次级别为:%s cur_ji:%s %s" % (resample, cur_ji, start_lastday)
            # print [chanK.index[x] for x in biIdx]
            k_data_c, cname = get_quotes_tdx(stock,
                                             start=start_lastday,
                                             end=end_date,
                                             dl=dl,
                                             resample=resample)
            print k_data_c.index[0], k_data_c.index[-1]
            chanKc = chan.parse2ChanK(
                k_data_c, k_data_c.values) if chanK_flag else k_data_c
            fenTypesc, fenIdxc = chan.parse2ChanFen(chanKc, recursion=True)
            if len(fenTypesc) == 0:
                return biIdx
            biIdxc, frsBiTypec = chan.parse2ChanBi(
                fenTypesc, fenIdxc, chanKc, least_khl_num=least_khl_num - 1)
            if len(biIdxc) == 0:
                return biIdx
            print "biIdxc:", [round(k_data_c.high[x], 2) for x in biIdxc
                              ], [str(k_data_c.index[x])[:10] for x in biIdxc]
            xdIdxc, xdTypec = chan.parse2Xianduan(
                biIdxc, chanKc, least_windows=1 if least_khl_num > 0 else 0)
            biIdxc = con2Cxianduan(stock,
                                   k_data_c,
                                   chanKc,
                                   frsBiTypec,
                                   biIdxc,
                                   end_date,
                                   cur_ji + 1,
                                   recursion=True)
            print "xdIdxc:%s xdTypec:%s biIdxc:%s" % (xdIdxc, xdTypec, biIdxc)
            if len(xdIdxc) == 0:
                return biIdx
            # 连接线段位为上级别的bi
            lastBiType = frsBiType if len(biIdx) % 2 == 0 else -frsBiType
            if len(biIdx) == 0:
                return refactorXd(biIdx, xdIdxc, chanK, chanKc, cur_ji)
            lastbi = biIdx.pop()
            firstbic = xdIdxc.pop(0)
            # 同向连接
            if lastBiType == xdTypec:
                biIdx = biIdx + refactorXd(biIdx, xdIdxc, chanK, chanKc,
                                           cur_ji)
            # 逆向连接
            else:
                #             print '开始逆向连接'
                _mid = [lastbi] if (lastBiType == -1 and chanK['low'][lastbi] <= chanKc['low'][firstbic])\
                    or (lastBiType == 1 and chanK['high'][lastbi] >= chanKc['high'][firstbic]) else\
                    [chanKidx for chanKidx in range(len(chanK.index))[biIdx[len(biIdx) - 1]:]
                     if judge_day_bao(chanK, chanKidx, chanKc, firstbic, cur_ji)]
                biIdx = biIdx + [_mid[0]] + refactorXd(biIdx, xdIdxc, chanK,
                                                       chanKc, cur_ji)
            # print "次级:",len(biIdx),biIdx,[str(k_data_c.index[x])[:10] for x in biIdx]
        return biIdx

    def get_quotes_tdx(code,
                       start=None,
                       end=None,
                       dl=120,
                       resample='d',
                       show_name=True):

        quotes = tdd.get_tdx_append_now_df_api(
            code=stock_code, start=start, end=end,
            dl=dl).sort_index(ascending=True)
        if not resample == 'd' and resample in tdd.resample_dtype:
            quotes = tdd.get_tdx_stock_period_to_type(quotes,
                                                      period_day=resample)
        quotes.index = quotes.index.astype('datetime64')
        if show_name:
            if 'name' in quotes.columns:
                cname = quotes.name[0]
                # cname_g =cname
            else:
                dm = tdd.get_sina_data_df(code)
                if 'name' in dm.columns:
                    cname = dm.name[0]
                else:
                    cname = '-'
        else:
            cname = '-'
        if quotes is not None and len(quotes) > 0:
            quotes = quotes.loc[:, [
                'open', 'close', 'high', 'low', 'vol', 'amount'
            ]]
        else:
            # log.error("quotes is None check:%s"%(code))
            raise Exception("Code:%s error, df is None%s" % (code))
        return quotes, cname

    quotes, cname = get_quotes_tdx(stock_code,
                                   start_date,
                                   end_date,
                                   dl=stock_days,
                                   resample=resample,
                                   show_name=show_mpl)
    # quotes.rename(columns={'amount': 'money'}, inplace=True)
    # quotes.rename(columns={'vol': 'vol'}, inplace=True)
    # print quotes[-2:]
    # print quotes[:1]
    # 缠论k线
    #         open  close   high    low    volume      money
    # 2017-05-03  15.69  15.66  15.73  15.53  10557743  165075887
    # 2017-05-04  15.66  15.63  15.70  15.52   8343270  130330396
    # 2017-05-05  15.56  15.65  15.68  15.41  18384031  285966842
    # 2017-05-08  15.62  15.75  15.76  15.54  12598891  197310688
    quotes = chan.parse2ChanK(quotes, quotes.values) if chanK_flag else quotes
    # print quotes[:1].index
    # print quotes[-1:].index

    quotes[quotes['vol'] == 0] = np.nan
    quotes = quotes.dropna()
    Close = quotes['close']
    Open = quotes['open']
    High = quotes['high']
    Low = quotes['low']
    T0 = quotes.index.values
    # T0 =  mdates.date2num(T0)
    length = len(Close)

    initial_trend = "down"
    cur_ji = 1 if stock_frequency == 'd' else \
        2 if stock_frequency == '30m' else \
        3 if stock_frequency == '5m' else \
        4 if stock_frequency == 'w' else \
        5 if stock_frequency == 'm' else 6

    log.debug('======笔形成最后一段未完成段判断是否是次级别的走势形成笔=======:%s %s' %
              (stock_frequency, cur_ji))

    x_date_list = quotes.index.values.tolist()
    # for x_date in x_date_list:
    #     d = datetime.datetime.fromtimestamp(x_date/1000000000)
    #     print d.strftime("%Y-%m-%d %H:%M:%S.%f")
    # print x_date_list
    k_data = quotes
    k_values = k_data.values
    # 缠论k线
    chanK = quotes if chanK_flag else chan.parse2ChanK(
        k_data, k_values, chan_kdf=chanK_flag)

    fenTypes, fenIdx = chan.parse2ChanFen(chanK)
    # log.debug("code:%s fenTypes:%s fenIdx:%s k_data:%s" % (stock_code,fenTypes, fenIdx, len(k_data)))
    biIdx, frsBiType = chan.parse2ChanBi(fenTypes,
                                         fenIdx,
                                         chanK,
                                         least_khl_num=least_khl_num)
    # log.debug("biIdx1:%s chanK:%s" % (biIdx, len(chanK)))
    print("biIdx1:%s %s chanK:%s" %
          (biIdx, str(chanK.index.values[biIdx[-1]])[:10], len(chanK)))

    biIdx = con2Cxianduan(stock_code,
                          k_data,
                          chanK,
                          frsBiType,
                          biIdx,
                          end_date,
                          cur_ji,
                          least_init=least_init)
    # log.debug("biIdx2:%s chanK:%s" % (biIdx, len(biIdx)))
    chanKIdx = [(chanK.index[x]) for x in biIdx]

    if len(biIdx) == 0 and len(chanKIdx) == 0:
        print "BiIdx is None and chanKidx is None:%s" % (code)
        return None

    log.debug("con2Cxianduan:%s chanK:%s %s" %
              (biIdx, len(chanK), chanKIdx[-1] if len(chanKIdx) > 0 else None))

    # print quotes['close'].apply(lambda x:round(x,2))

    # print '股票代码', get_security_info(stock_code).display_name
    # print '股票代码', (stock_code), resample, least_khl_num
    #  3.得到分笔结果,计算坐标显示

    def plot_fenbi_seq(biIdx, frsBiType, plt=None, color=None):
        x_fenbi_seq = []
        y_fenbi_seq = []
        for i in range(len(biIdx)):
            if biIdx[i] is not None:
                fenType = -frsBiType if i % 2 == 0 else frsBiType
                #         dt = chanK['enddate'][biIdx[i]]
                # 缠论k线
                dt = chanK.index[biIdx[i]] if chanK_flag else chanK['enddate'][
                    biIdx[i]]
                # print i,k_data['high'][dt], k_data['low'][dt]
                time_long = long(
                    time.mktime(
                        (dt + datetime.timedelta(hours=8)).timetuple()) *
                    1000000000)
                # print x_date_list.index(time_long) if time_long in x_date_list else 0
                if fenType == 1:
                    if plt is not None:
                        if color is None:
                            plt.text(x_date_list.index(time_long),
                                     k_data['high'][dt],
                                     str(k_data['high'][dt]),
                                     ha='left',
                                     fontsize=12)
                        else:
                            col_v = color[0] if fenType > 0 else color[1]
                            plt.text(x_date_list.index(time_long),
                                     k_data['high'][dt],
                                     str(k_data['high'][dt]),
                                     ha='left',
                                     fontsize=12,
                                     bbox=dict(facecolor=col_v, alpha=0.5))

                    x_fenbi_seq.append(x_date_list.index(time_long))
                    y_fenbi_seq.append(k_data['high'][dt])
                if fenType == -1:
                    if plt is not None:
                        if color is None:
                            plt.text(x_date_list.index(time_long),
                                     k_data['low'][dt],
                                     str(k_data['low'][dt]),
                                     va='bottom',
                                     fontsize=12)
                        else:
                            col_v = color[0] if fenType > 0 else color[1]
                            plt.text(x_date_list.index(time_long),
                                     k_data['low'][dt],
                                     str(k_data['low'][dt]),
                                     va='bottom',
                                     fontsize=12,
                                     bbox=dict(facecolor=col_v, alpha=0.5))

                    x_fenbi_seq.append(x_date_list.index(time_long))
                    y_fenbi_seq.append(k_data['low'][dt])
    #             bottom_time = None
    #             for k_line_dto in m_line_dto.member_list[::-1]:
    #                 if k_line_dto.low == m_line_dto.low:
    #                     # get_price返回的日期,默认时间是08:00:00
    #                     bottom_time = k_line_dto.begin_time.strftime('%Y-%m-%d') +' 08:00:00'
    #                     break
    #             x_fenbi_seq.append(x_date_list.index(long(time.mktime(datetime.strptime(bottom_time, "%Y-%m-%d %H:%M:%S").timetuple())*1000000000)))
    #             y_fenbi_seq.append(m_line_dto.low)
        return x_fenbi_seq, y_fenbi_seq

    # print  T0[-len(T0):].astype(dt.date)
    T1 = T0[-len(T0):].astype(datetime.date) / 1000000000
    Ti = []
    if len(T0) / x_jizhun > 12:
        x_jizhun = len(T0) / 12
    for i in range(len(T0) / x_jizhun):
        # print "len(T0)/x_jizhun:",len(T0)/x_jizhun
        a = i * x_jizhun
        d = datetime.date.fromtimestamp(T1[a])
        # print d
        T2 = d.strftime('$%Y-%m-%d$')
        Ti.append(T2)
        # print tab
    d1 = datetime.date.fromtimestamp(T1[len(T0) - 1])
    d2 = (d1 + datetime.timedelta(days=1)).strftime('$%Y-%m-%d$')
    Ti.append(d2)

    ll = Low.min() * 0.97
    hh = High.max() * 1.03

    # ht = HoverTool(tooltips=[
    #             ("date", "@date"),
    #             ("open", "@open"),
    #             ("close", "@close"),
    #             ("high", "@high"),
    #             ("low", "@low"),
    #             ("volume", "@volume"),
    #             ("money", "@money"),])
    # TOOLS = [ht, WheelZoomTool(dimensions=['width']),\
    #          ResizeTool(), ResetTool(),\
    #          PanTool(dimensions=['width']), PreviewSaveTool()]
    if show_mpl:
        fig = plt.figure(figsize=(10, 6))
        ax1 = plt.subplot2grid((10, 1), (0, 0), rowspan=8, colspan=1)
        # ax1 = fig.add_subplot(2,1,1)
        #fig = plt.figure()
        #ax1 = plt.axes([0,0,3,2])

        X = np.array(range(0, length))
        pad_nan = X + nan

        # 计算上 下影线
        max_clop = Close.copy()
        max_clop[Close < Open] = Open[Close < Open]
        min_clop = Close.copy()
        min_clop[Close > Open] = Open[Close > Open]

        # 上影线
        line_up = np.array([High, max_clop, pad_nan])
        line_up = np.ravel(line_up, 'F')
        # 下影线
        line_down = np.array([Low, min_clop, pad_nan])
        line_down = np.ravel(line_down, 'F')

        # 计算上下影线对应的X坐标
        pad_nan = nan + X
        pad_X = np.array([X, X, X])
        pad_X = np.ravel(pad_X, 'F')

        # 画出实体部分,先画收盘价在上的部分
        up_cl = Close.copy()
        up_cl[Close <= Open] = nan
        up_op = Open.copy()
        up_op[Close <= Open] = nan

        down_cl = Close.copy()
        down_cl[Open <= Close] = nan
        down_op = Open.copy()
        down_op[Open <= Close] = nan

        even = Close.copy()
        even[Close != Open] = nan

        # 画出收红的实体部分
        pad_box_up = np.array([up_op, up_op, up_cl, up_cl, pad_nan])
        pad_box_up = np.ravel(pad_box_up, 'F')
        pad_box_down = np.array([down_cl, down_cl, down_op, down_op, pad_nan])
        pad_box_down = np.ravel(pad_box_down, 'F')
        pad_box_even = np.array([even, even, even, even, pad_nan])
        pad_box_even = np.ravel(pad_box_even, 'F')

        # X的nan可以不用与y一一对应
        X_left = X - 0.25
        X_right = X + 0.25
        box_X = np.array([X_left, X_right, X_right, X_left, pad_nan])
        # print box_X
        box_X = np.ravel(box_X, 'F')
        # print box_X
        # Close_handle=plt.plot(pad_X,line_up,color='k')

        vertices_up = np.array([box_X, pad_box_up]).T
        vertices_down = np.array([box_X, pad_box_down]).T
        vertices_even = np.array([box_X, pad_box_even]).T

        handle_box_up = mat.patches.Polygon(vertices_up, color='r', zorder=1)
        handle_box_down = mat.patches.Polygon(vertices_down,
                                              color='g',
                                              zorder=1)
        handle_box_even = mat.patches.Polygon(vertices_even,
                                              color='k',
                                              zorder=1)

        ax1.add_patch(handle_box_up)
        ax1.add_patch(handle_box_down)
        ax1.add_patch(handle_box_even)

        handle_line_up = mat.lines.Line2D(pad_X,
                                          line_up,
                                          color='k',
                                          linestyle='solid',
                                          zorder=0)
        handle_line_down = mat.lines.Line2D(pad_X,
                                            line_down,
                                            color='k',
                                            linestyle='solid',
                                            zorder=0)

        ax1.add_line(handle_line_up)
        ax1.add_line(handle_line_down)

        v = [0, length, Open.min() - 0.5, Open.max() + 0.5]
        plt.axis(v)

        ax1.set_xticks(np.linspace(-2, len(Close) + 2, len(Ti)))

        ax1.set_ylim(ll, hh)

        ax1.set_xticklabels(Ti)

        plt.grid(True)
        plt.setp(plt.gca().get_xticklabels(),
                 rotation=30,
                 horizontalalignment='right')
    '''
    以上代码拷贝自https://www.joinquant.com/post/1756
    感谢alpha-smart-dog

    K线图绘制完毕
    '''

    # print "biIdx:%s chankIdx:%s"%(biIdx,str(chanKIdx[-1])[:10])
    if show_mpl:
        x_fenbi_seq, y_fenbi_seq = plot_fenbi_seq(biIdx, frsBiType, plt)
        # plot_fenbi_seq(fenIdx,fenTypes[0], plt,color=['red','green'])
        plot_fenbi_seq(fenIdx, frsBiType, plt, color=['red', 'green'])
    else:
        x_fenbi_seq, y_fenbi_seq = plot_fenbi_seq(biIdx, frsBiType, plt=None)
        plot_fenbi_seq(fenIdx, frsBiType, plt=None, color=['red', 'green'])
    #  在原图基础上添加分笔蓝线
    inx_value = chanK.high.values
    inx_va = [round(inx_value[x], 2) for x in biIdx]
    log.debug("inx_va:%s count:%s" % (inx_va, len(quotes.high)))
    log.debug("yfenbi:%s count:%s" % ([round(y, 2)
                                       for y in y_fenbi_seq], len(chanK)))
    j_BiType = [
        -frsBiType if i % 2 == 0 else frsBiType for i in range(len(biIdx))
    ]
    BiType_s = j_BiType[-1] if len(j_BiType) > 0 else -2
    # bi_price = [str(chanK.low[idx]) if i % 2 == 0 else str(chanK.high[idx])  for i,idx in enumerate(biIdx)]
    # print ("笔     :%s %s"%(biIdx,bi_price))
    # fen_dt = [str(chanK.index[fenIdx[i]])[:10] if chanK_flag else str(chanK['enddate'][fenIdx[i]])[:10]for i in range(len(fenIdx))]
    fen_dt = [(chanK.index[fenIdx[i]]) if chanK_flag else
              (chanK['enddate'][fenIdx[i]]) for i in range(len(fenIdx))]
    if len(fenTypes) > 0:
        if fenTypes[0] == -1:
            # fen_price = [str(k_data.low[idx]) if i % 2 == 0 else str(k_data.high[idx])  for i,idx in enumerate(fen_dt)]
            low_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 == 0]
            high_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 <> 0]
        else:
            # fen_price = [str(k_data.high[idx]) if i % 2 == 0 else str(k_data.low[idx])  for i,idx in enumerate(fen_dt)]
            high_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 == 0]
            low_fen = [idx for i, idx in enumerate(fen_dt) if i % 2 <> 0]
        # fen_duration =[fenIdx[i] - fenIdx[i -1 ] if i >0 else 0 for i,idx in enumerate(fenIdx)]
    else:
        # fen_price = fenTypes
        # fen_duration = fenTypes
        low_fen = []
        high_fen = []
    # fen_dt = [str(k_data.index[idx])[:10] for i,idx in enumerate(fenIdx)]
    # print low_fen,high_fen
    def dataframe_mode_round(df):
        roundlist = [1, 0]
        df_mode = []
        # df.high.cummin().value_counts()
        for i in roundlist:
            df_mode = df.apply(lambda x: round(x, i)).mode()
            if len(df_mode) > 0:
                break
        return df_mode

    kdl = k_data.loc[low_fen].low
    kdl_mode = dataframe_mode_round(kdl)
    kdh = k_data.loc[high_fen].high
    kdh_mode = dataframe_mode_round(kdh)

    print("kdl:%s" % (kdl.values))
    print("kdh:%s" % (kdh.values))
    print("kdl_mode:%s kdh_mode%s chanKidx:%s" %
          (kdl_mode.values, kdh_mode.values, str(chanKIdx[-1])[:10]))

    lastdf = k_data[k_data.index >= chanKIdx[-1]]
    if BiType_s == -1:
        keydf = lastdf[((lastdf.close >= kdl_mode.max()) &
                        (lastdf.low >= kdl_mode.max()))]
    elif BiType_s == 1:
        keydf = lastdf[((lastdf.close >= kdh_mode.max()) &
                        (lastdf.high >= kdh_mode.min()))]
    else:
        keydf = lastdf[((lastdf.close >= kdh_mode.max()) &
                        (lastdf.high >= kdh_mode.min())) |
                       ((lastdf.close <= kdl_mode.min()) &
                        (lastdf.low <= kdl_mode.min()))]
    print("BiType_s:%s keydf:%s key:%s" %
          (BiType_s, None if len(keydf) == 0 else str(
              keydf.index.values[0])[:10], len(keydf)))

    # return BiType_s,None if len(keydf) == 0 else str(keydf.index.values[0])[:10],len(keydf)
    # import ipdb;ipdb.set_trace()

    log.debug("Fentype:%s " % (fenTypes))
    log.debug("fenIdx:%s " % (fenIdx))
    # print ("fen_duration:%s "%(fen_duration))
    # print ("fen_price:%s "%(fen_price))
    # print ("fendt:%s "%(fen_dt))

    print("BiType :%s frsBiType:%s" % (j_BiType, frsBiType))

    if len(j_BiType) > 0:
        if j_BiType[0] == -1:
            tb_price = [
                str(quotes.low[idx]) if i % 2 == 0 else str(quotes.high[idx])
                for i, idx in enumerate(x_fenbi_seq)
            ]
        else:
            tb_price = [
                str(quotes.high[idx]) if i % 2 == 0 else str(quotes.low[idx])
                for i, idx in enumerate(x_fenbi_seq)
            ]
        tb_duration = [
            x_fenbi_seq[i] - x_fenbi_seq[i - 1] if i > 0 else 0
            for i, idx in enumerate(x_fenbi_seq)
        ]

    else:
        tb_price = j_BiType
        tb_duration = j_BiType
    print "图笔 :", x_fenbi_seq, tb_price
    print "图笔dura :", tb_duration

    # 线段画到笔上
    xdIdxs, xfenTypes = chan.parse2ChanXD(frsBiType, biIdx, chanK)
    print '线段', xdIdxs, xfenTypes
    x_xd_seq = []
    y_xd_seq = []
    for i in range(len(xdIdxs)):
        if xdIdxs[i] is not None:
            fenType = xfenTypes[i]
            #         dt = chanK['enddate'][biIdx[i]]
            # 缠论k线
            dt = chanK.index[xdIdxs[i]] if chanK_flag else chanK['enddate'][
                xdIdxs[i]]
            #         print k_data['high'][dt], k_data['low'][dt]
            time_long = long(
                time.mktime((dt + datetime.timedelta(hours=8)).timetuple()) *
                1000000000)
            #         print x_date_list.index(time_long) if time_long in x_date_list else 0
            if fenType == 1:
                x_xd_seq.append(x_date_list.index(time_long))
                y_xd_seq.append(k_data['high'][dt])
            if fenType == -1:
                x_xd_seq.append(x_date_list.index(time_long))
                y_xd_seq.append(k_data['low'][dt])
    #             bottom_time = None
    #             for k_line_dto in m_line_dto.member_list[::-1]:
    #                 if k_line_dto.low == m_line_dto.low:
    #                     # get_price返回的日期,默认时间是08:00:00
    #                     bottom_time = k_line_dto.begin_time.strftime('%Y-%m-%d') +' 08:00:00'
    #                     break
    #             x_fenbi_seq.append(x_date_list.index(long(time.mktime(datetime.strptime(bottom_time, "%Y-%m-%d %H:%M:%S").timetuple())*1000000000)))
    #             y_fenbi_seq.append(m_line_dto.low)

    #  在原图基础上添加分笔蓝线
    print("线段   :%s" % (x_xd_seq))
    print("笔值  :%s" % ([str(x) for x in (y_xd_seq)]))
    # Y_hat = X * b + a

    if show_mpl:
        plt.plot(x_fenbi_seq, y_fenbi_seq)
        plt.legend([stock_code, cname], loc=0)
        plt.title(stock_code + " | " + cname + " | " +
                  str(quotes.index[-1])[:10],
                  fontsize=14)

        plt.plot(x_xd_seq, y_xd_seq)
        if len(quotes) > windows:
            roll_mean = pd.rolling_mean(quotes.close, window=windows)
            plt.plot(roll_mean, 'r')
        zp = zoompan.ZoomPan()
        figZoom = zp.zoom_factory(ax1, base_scale=1.1)
        figPan = zp.pan_factory(ax1)
        '''#subplot2 bar
        ax2 = plt.subplot2grid((10, 1), (8, 0), rowspan=2, colspan=1)
        # ax2.plot(quotes.vol)
        # ax2.set_xticks(np.linspace(-2, len(quotes) + 2, len(Ti)))
        ll = min(quotes.vol.values.tolist()) * 0.97
        hh = max(quotes.vol.values.tolist()) * 1.03
        ax2.set_ylim(ll, hh)
        # ax2.set_xticklabels(Ti)
        # plt.hist(quotes.vol, histtype='bar', rwidth=0.8)
        plt.bar(x_date_list,quotes.vol, label="Volume", color='b')
        '''

        #画Volume no tight_layout()
        '''
        pad = 0.25
        yl = ax1.get_ylim()
        ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
        ax2 = ax1.twinx()
        ax2.set_position(mat.transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
        volume = np.asarray(quotes.amount)
        pos = quotes['open']-quotes['close']<0
        neg = quotes['open']-quotes['close']>=0
        idx = quotes.reset_index().index
        ax2.bar(idx[pos],volume[pos],color='red',width=1,align='center')
        ax2.bar(idx[neg],volume[neg],color='green',width=1,align='center')
        yticks = ax2.get_yticks()
        ax2.set_yticks(yticks[::3])        
        '''

        # same sharex
        plt.subplots_adjust(left=0.05,
                            bottom=0.08,
                            right=0.95,
                            top=0.95,
                            wspace=0.15,
                            hspace=0.00)
        plt.setp(ax1.get_xticklabels(), visible=False)
        yl = ax1.get_ylim()
        # ax2 = plt.subplot(212, sharex=ax1)
        ax2 = plt.subplot2grid((10, 1), (8, 0),
                               rowspan=2,
                               colspan=1,
                               sharex=ax1)
        # ax2.set_position(mat.transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
        volume = np.asarray(quotes.amount)
        pos = quotes['open'] - quotes['close'] < 0
        neg = quotes['open'] - quotes['close'] >= 0
        idx = quotes.reset_index().index
        ax2.bar(idx[pos], volume[pos], color='red', width=1, align='center')
        ax2.bar(idx[neg], volume[neg], color='green', width=1, align='center')
        yticks = ax2.get_yticks()
        ax2.set_yticks(yticks[::3])
        # plt.tight_layout()
        # plt.subplots_adjust(hspace=0.00, bottom=0.08)
        plt.xticks(rotation=15, horizontalalignment='center')
        # plt.bar(x_date_list,quotes.vol, label="Volume", color='b')

        # quotes['vol'].plot(kind='bar', ax=ax2, color='g', alpha=0.1)
        # ax2.set_ylim([0, ax2.get_ylim()[1] * 2])
        # plt.gcf().subplots_adjust(bottom=0.15)
        # fig.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25)
        #scale the x-axis tight
        # ax2.set_xlim(min(x_date_list),max(x_date_list))
        # the y-ticks for the bar were too dense, keep only every third one
        # plt.grid(True)
        # plt.xticks(rotation=30, horizontalalignment='center')
        # plt.setp( axs[1].xaxis.get_majorticklabels(), rotation=70 )
        # plt.legend()
        # plt.tight_layout()
        # plt.draw()
        # plt.show()
        plt.show(block=False)
Ejemplo n.º 3
0
def longsklearn(code='999999'):
    # code='999999'
    df = tdd.get_tdx_append_now_df(code, 'f').sort_index(ascending=True)
    # print df[:1]
    h = df.loc[:, ['open', 'close', 'high', 'low']]
    highp = h['high'].values
    lowp = h['low'].values
    openp = h['open'].values
    closep = h['close'].values
    lr = LinearRegression()
    x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T
    lr.fit(x, closep)
    LinearRegression(copy_X=True,
                     fit_intercept=True,
                     n_jobs=1,
                     normalize=False)
    xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T
    yt = lr.predict(xt)
    # plt.plot(xt,yt,'-g',linewidth=5)
    # plt.plot(closep)
    bV = []
    bP = []
    for i in range(1, len(highp) - 1):
        if highp[i] <= highp[i - 1] and highp[i] < highp[
                i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]:
            bV.append(lowp[i])
            bP.append(i)

    d, p = LIS(bV)

    idx = []
    for i in range(len(p)):
        idx.append(bP[p[i]])
    # plt.plot(closep)
    # plt.plot(idx,d,'ko')
    lr = LinearRegression()
    X = np.atleast_2d(np.array(idx)).T
    Y = np.array(d)
    lr.fit(X, Y)
    estV = lr.predict(xt)

    fig = plt.figure(figsize=(16, 10), dpi=72)
    # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
    plt.subplots_adjust(left=0.05,
                        bottom=0.08,
                        right=0.95,
                        top=0.95,
                        wspace=0.15,
                        hspace=0.25)
    # set (gca,'Position',[0,0,512,512])
    # fig.set_size_inches(18.5, 10.5)
    # fig=plt.fig(figsize=(14,8))
    ax = fig.add_subplot(111)
    plt.grid(True)
    ax.plot(closep, linewidth=2)
    ax.plot(idx, d, 'ko')
    ax.plot(xt, estV, '-r', linewidth=5)
    ax.plot(xt, yt, '-g', linewidth=5)

    # ax2 = fig.add_subplot(122)
    # print len(closep),len(idx),len(d),len(xt),len(estV),len(yt)
    # f=lambda x:x[-int(len(x)/10):]
    # ax2.plot(f(closep))
    # ax2.plot(f(idx),f(d),'ko')
    # ax2.plot(f(xt),f(estV),'-r',linewidth=5)
    # ax2.plot(f(xt),f(yt),'-g',linewidth=5)
    # # plt.show()
    scale = 1.1
    zp = zoompan.ZoomPan()
    figZoom = zp.zoom_factory(ax, base_scale=scale)
    figPan = zp.pan_factory(ax)
    show()
Ejemplo n.º 4
0
sys.exit(0)

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.01, 5.0, 0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = np.sin(4 * np.pi * t)

ax1 = plt.subplot(311)
plt.plot(t, s1)
plt.setp(ax1.get_xticklabels(), fontsize=6)

# share x only
ax2 = plt.subplot(312, sharex=ax1)
plt.plot(t, s2)
# make these tick labels invisible
plt.setp(ax2.get_xticklabels(), visible=False)

# share x and y
ax3 = plt.subplot(313, sharex=ax1, sharey=ax1)
plt.plot(t, s3)
plt.xlim(0.01, 5.0)
import sys
sys.path.append('../../')
from JohnsonUtil import zoompan
zp = zoompan.ZoomPan()
figZoom = zp.zoom_factory(ax1, base_scale=1.1)
figPan = zp.pan_factory(ax1)
plt.show()
Ejemplo n.º 5
0
def get_linear_model_histogram(code,
                               ptype='low',
                               dtype='d',
                               start=None,
                               end=None,
                               vtype='f',
                               filter='n',
                               df=None):
    # 399001','cyb':'zs399006','zxb':'zs399005
    # code = '999999'
    # code = '601608'
    # code = '000002'
    # asset = get_kdate_data(code)['close'].sort_index(ascending=True)
    # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True)
    # ptype='close'
    # if ptype == 'close' or ptype==''
    # ptype=

    if start is not None and filter == 'y':
        if code not in ['999999', '399006', '399001']:
            index_d, dl = tdd.get_duration_Index_date(dt=start)
            log.debug("index_d:%s dl:%s" % (str(index_d), dl))
        else:
            index_d = cct.day8_to_day10(start)
            log.debug("index_d:%s" % (index_d))
        start = tdd.get_duration_price_date(code, ptype='low', dt=index_d)
        log.debug("start:%s" % (start))
    if df is None:
        # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True)
        df = tdd.get_tdx_append_now_df_api(code, start,
                                           end).sort_index(ascending=True)
    if not dtype == 'd':
        df = tdd.get_tdx_stock_period_to_type(df,
                                              dtype).sort_index(ascending=True)
    asset = df[ptype]
    log.info("df:%s" % asset[:1])
    asset = asset.dropna()
    dates = asset.index

    if not code.startswith('999') and not code.startswith('399'):
        # print "code:",code
        if code[:1] in ['5', '6', '9']:
            code2 = '999999'
        elif code[:2] in ['30']:
            # print "cyb"
            code2 = '399006'
        else:
            code2 = '399001'
        df1 = tdd.get_tdx_append_now_df_api(code2, start,
                                            end).sort_index(ascending=True)
        # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True)
        if not dtype == 'd':
            df1 = tdd.get_tdx_stock_period_to_type(
                df1, dtype).sort_index(ascending=True)
            # if len(asset) < len(df1):
            # asset1 = df1.loc[asset.index, ptype]
            # else:
            # asset1 = df1.loc[asset.index, ptype]
        # startv = asset1[:1]
        # asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        # print asset[:1].index[0] , df1[:1].index[0]
        if asset[:1].index[0] > df1[:1].index[0]:
            asset1 = df1.loc[asset.index, ptype]
            startv = asset1[:1]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        else:
            df = df[df.index >= df1.index[0]]
            asset = df[ptype]
            asset = asset.dropna()
            dates = asset.index
            asset1 = df1.loc[df.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))

    else:
        if code.startswith('399001'):
            code2 = '999999'
        elif code.startswith('399006'):
            code2 = '399005'
        else:
            code2 = '399001'
        df1 = tdd.get_tdx_append_now_df_api(code2, start,
                                            end).sort_index(ascending=True)
        # print df1[:1]
        # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True)
        if not dtype == 'd':
            df1 = tdd.get_tdx_stock_period_to_type(
                df1, dtype).sort_index(ascending=True)
        if len(asset) < len(df1):
            asset1 = df1.loc[asset.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        else:

            df = df[df.index >= df1.index[0]]
            asset = df[ptype]
            asset = asset.dropna()
            dates = asset.index
            asset1 = df1.loc[df.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
    # print len(df),len(asset),len(df1),len(asset1)

    if end is not None:
        # print asset[-1:]
        asset = asset[:-1]
        dates = asset.index
        asset1 = asset1[:-1]
        asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))

    # 画出价格随时间变化的图像
    # _, ax = plt.subplots()
    # fig = plt.figure()
    fig = plt.figure(figsize=(16, 5))
    # fig = plt.figure(figsize=(16, 10), dpi=72)
    # fig.autofmt_xdate() #(no fact)

    # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
    plt.subplots_adjust(left=0.05,
                        bottom=0.08,
                        right=0.95,
                        top=0.95,
                        wspace=0.15,
                        hspace=0.25)
    # set (gca,'Position',[0,0,512,512])
    # fig.set_size_inches(18.5, 10.5)
    # fig=plt.fig(figsize=(14,8))
    ax1 = fig.add_subplot(121)
    # asset=asset.apply(lambda x:round( x/asset[:1],2))
    ax1.plot(asset)
    # ax1.plot(asset1,'-r', linewidth=2)
    ticks = ax1.get_xticks()
    # start, end = ax1.get_xlim()
    # print start, end, len(asset)
    # print ticks, ticks[:-1]
    # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1))
    ax1.set_xticklabels(
        [dates[int(i)] for i in (np.append(ticks[:-1],
                                           len(asset) - 1))],
        rotation=15)  # Label x-axis with dates
    # 拟合
    X = np.arange(len(asset))
    x = sm.add_constant(X)
    model = regression.linear_model.OLS(asset, x).fit()
    a = model.params[0]
    b = model.params[1]
    # log.info("a:%s b:%s" % (a, b))
    log.info("X:%s a:%s b:%s" % (len(asset), a, b))
    Y_hat = X * b + a

    # 真实值-拟合值,差值最大最小作为价值波动区间
    # 向下平移
    i = (asset.values.T - Y_hat).argmin()
    c_low = X[i] * b + a - asset.values[i]
    Y_hatlow = X * b + a - c_low

    # 向上平移
    i = (asset.values.T - Y_hat).argmax()
    c_high = X[i] * b + a - asset.values[i]
    Y_hathigh = X * b + a - c_high
    plt.plot(X, Y_hat, 'k', alpha=0.9)
    plt.plot(X, Y_hatlow, 'r', alpha=0.9)
    plt.plot(X, Y_hathigh, 'r', alpha=0.9)
    # plt.xlabel('Date', fontsize=12)
    plt.ylabel('Price', fontsize=12)
    plt.title(code + " | " + str(dates[-1])[:11], fontsize=14)
    plt.legend([asset.iat[-1]], fontsize=12, loc=4)
    plt.grid(True)

    # plt.legend([code]);
    # plt.legend([code, 'Value center line', 'Value interval line']);
    # fig=plt.fig()
    # fig.figsize = [14,8]
    scale = 1.1
    zp = zoompan.ZoomPan()
    figZoom = zp.zoom_factory(ax1, base_scale=scale)
    figPan = zp.pan_factory(ax1)

    # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。
    ax3 = fig.add_subplot(122)
    # distance = (asset.values.T - Y_hat)
    distance = (asset.values.T - Y_hat)[0]
    # if code.startswith('999') or code.startswith('399'):
    if len(asset) > len(df1):
        ax3.plot(asset)
        plt.plot(distance)
        ticks = ax3.get_xticks()
        ax3.set_xticklabels(
            [dates[int(i)] for i in (np.append(ticks[:-1],
                                               len(asset) - 1))],
            rotation=15)
        n = 5
        d = (-c_high + c_low) / n
        c = c_high
        while c <= c_low:
            Y = X * b + a - c
            plt.plot(X, Y - Y_hat, 'r', alpha=0.9)
            c = c + d
        ax3.plot(asset)
        ## plt.xlabel('Date', fontsize=12)
        plt.ylabel('Price-center price', fontsize=14)
        plt.grid(True)
    else:
        as3 = asset.apply(lambda x: round(x / asset[:1], 2))
        ax3.plot(as3)
        ax3.plot(asset1, '-r', linewidth=2)

        # assvol = df.loc[asset.index]['vol']
        # assvol = assvol.apply(lambda x: round(x / assvol[:1], 2))
        # ax3.plot(assvol, '-g', linewidth=2)

        plt.grid(True)
        zp3 = zoompan.ZoomPan()
        figZoom = zp3.zoom_factory(ax3, base_scale=scale)
        figPan = zp3.pan_factory(ax3)
    # plt.title(code, fontsize=14)
    if 'name' in df.columns:
        plt.legend([df.name[-1], df1.name[-1]], loc=0)
    else:
        dm = tdd.get_sina_data_df(code)
        if 'name' in dm.columns:
            cname = dm.name[0]
        else:
            cname = '-'
        # plt.legend([code, code2], loc=0)
        plt.legend([cname, code2], loc=0)
    plt.show(block=False)
Ejemplo n.º 6
0
def get_linear_model_histogramDouble(code,
                                     ptype='low',
                                     dtype='d',
                                     start=None,
                                     end=None,
                                     vtype='f',
                                     filter='n',
                                     df=None,
                                     dl=None):
    # 399001','cyb':'zs399006','zxb':'zs399005
    # code = '999999'
    # code = '601608'
    # code = '000002'
    # asset = get_kdate_data(code)['close'].sort_index(ascending=True)
    # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True)
    # ptype='close'
    # if ptype == 'close' or ptype==''
    # ptype=
    if start is not None and filter == 'y':
        if code not in ['999999', '399006', '399001']:
            index_d, dl = tdd.get_duration_Index_date(dt=start)
            log.debug("index_d:%s dl:%s" % (str(index_d), dl))
        else:
            index_d = cct.day8_to_day10(start)
            log.debug("index_d:%s" % (index_d))
        start = tdd.get_duration_price_date(code, ptype='low', dt=index_d)
        log.debug("start:%s" % (start))

    if start is None and df is None and dl is not None:
        start = cct.last_tddate(dl)
        # print start
        df = tdd.get_tdx_append_now_df_api(code, start=start,
                                           end=end).sort_index(ascending=True)

    if df is None:
        # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True)
        df = tdd.get_tdx_append_now_df_api(code, start,
                                           end).sort_index(ascending=True)
    if not dtype == 'd':
        df = tdd.get_tdx_stock_period_to_type(df,
                                              dtype).sort_index(ascending=True)

    if len(df) == 0:
        raise Exception("Code:%s error, df is None" % (code))
    asset = df[ptype].round(2)
    log.info("df:%s" % asset[:1])
    asset = asset.dropna()
    dates = asset.index

    if not code.startswith('999') and not code.startswith('399'):
        # print "code:",code
        if code[:1] in ['5', '6', '9']:
            code2 = '999999'
        elif code[:2] in ['30']:
            # print "cyb"
            code2 = '399006'
        else:
            code2 = '399001'
        df1 = tdd.get_tdx_append_now_df_api(code2, start,
                                            end).sort_index(ascending=True)
        # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True)
        if not dtype == 'd':
            df1 = tdd.get_tdx_stock_period_to_type(
                df1, dtype).sort_index(ascending=True)
            # if len(asset) < len(df1):
            # asset1 = df1.loc[asset.index, ptype]
            # else:
            # asset1 = df1.loc[asset.index, ptype]
        # startv = asset1[:1]
        # asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        # print asset[:1].index[0] , df1[:1].index[0]
        if asset[:1].index[0] > df1[:1].index[0]:
            asset1 = df1.loc[asset.index, ptype]
            startv = asset1[:1]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        else:
            df = df[df.index >= df1.index[0]]
            asset = df[ptype]
            asset = asset.dropna()
            dates = asset.index
            asset1 = df1.loc[df.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))

    else:
        if code.startswith('399001'):
            code2 = '399006'
        elif code.startswith('399006'):
            code2 = '399005'
        else:
            code2 = '399006'
        if code2.startswith('3990'):
            df1 = tdd.get_tdx_append_now_df_api(code2, start,
                                                end).sort_index(ascending=True)
            if len(df1) < int(len(df) / 4):
                code2 = '399001'
                df1 = tdd.get_tdx_append_now_df_api(
                    code2, start, end).sort_index(ascending=True)

        # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True)
        if not dtype == 'd':
            df1 = tdd.get_tdx_stock_period_to_type(
                df1, dtype).sort_index(ascending=True)
        if len(asset) < len(df1):
            asset1 = df1.loc[asset.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
        else:

            df = df[df.index >= df1.index[0]]
            asset = df[ptype]
            asset = asset.dropna()
            dates = asset.index
            asset1 = df1.loc[df.index, ptype]
            asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))
    # print len(df),len(asset),len(df1),len(asset1)

    if end is not None:
        # print asset[-1:]
        asset = asset[:-1]
        dates = asset.index
        asset1 = asset1[:-1]
        asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2))

    # 画出价格随时间变化的图像
    # _, ax = plt.subplots()
    # fig = plt.figure()
    # plt.ion()
    fig = plt.figure(figsize=(16, 10))

    # fig = plt.figure(figsize=(16, 10), dpi=72)
    # fig.autofmt_xdate() #(no fact)

    # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
    plt.subplots_adjust(left=0.05,
                        bottom=0.08,
                        right=0.95,
                        top=0.95,
                        wspace=0.15,
                        hspace=0.25)
    # set (gca,'Position',[0,0,512,512])
    # fig.set_size_inches(18.5, 10.5)
    # fig=plt.fig(figsize=(14,8))
    ax1 = fig.add_subplot(321)
    # asset=asset.apply(lambda x:round( x/asset[:1],2))
    ax1.plot(asset)
    # ax1.plot(asset1,'-r', linewidth=2)
    ticks = ax1.get_xticks()
    # start, end = ax1.get_xlim()
    # print start, end, len(asset)
    # print ticks, ticks[:-1]
    # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1))

    ax1.set_xticklabels(
        [dates[int(i)] for i in (np.append(ticks[:-1],
                                           len(asset) - 1))],
        rotation=15)  # Label x-axis with dates
    # 拟合
    X = np.arange(len(asset))
    x = sm.add_constant(X)
    model = regression.linear_model.OLS(asset, x).fit()
    a = model.params[0]
    b = model.params[1]
    # log.info("a:%s b:%s" % (a, b))
    log.info("X:%s a:%s b:%s" % (len(asset), a, b))
    Y_hat = X * b + a

    # 真实值-拟合值,差值最大最小作为价值波动区间
    # 向下平移
    i = (asset.values.T - Y_hat).argmin()
    c_low = X[i] * b + a - asset.values[i]
    Y_hatlow = X * b + a - c_low

    # 向上平移
    i = (asset.values.T - Y_hat).argmax()
    c_high = X[i] * b + a - asset.values[i]
    Y_hathigh = X * b + a - c_high
    plt.plot(X, Y_hat, 'k', alpha=0.9)
    plt.plot(X, Y_hatlow, 'r', alpha=0.9)
    plt.plot(X, Y_hathigh, 'r', alpha=0.9)
    # plt.xlabel('Date', fontsize=12)
    plt.ylabel('Price', fontsize=12)
    plt.title(code + " | " + str(dates[-1])[:11], fontsize=14)
    plt.legend([asset.iat[-1]], fontsize=12, loc=4)
    plt.grid(True)

    # #plot volume
    # pad = 0.25
    # yl = ax1.get_ylim()
    # ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])
    # axx = ax1.twinx()
    # axx.set_position(transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
    # volume = np.asarray(df.vol)
    # pos = df['open']-df['close']<0
    # neg = df['open']-df['close']>=0
    # idx = np.asarray([x for x in range(len(df))])
    # axx.bar(idx[pos],volume[pos],color='red',width=1,align='center')
    # axx.bar(idx[neg],volume[neg],color='green',width=1,align='center')

    # plt.legend([code]);
    # plt.legend([code, 'Value center line', 'Value interval line']);
    # fig=plt.fig()
    # fig.figsize = [14,8]
    scale = 1.1
    zp = zoompan.ZoomPan()
    figZoom = zp.zoom_factory(ax1, base_scale=scale)
    figPan = zp.pan_factory(ax1)

    # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。
    ax3 = fig.add_subplot(322)
    # distance = (asset.values.T - Y_hat)
    distance = (asset.values.T - Y_hat)[0]
    # if code.startswith('999') or code.startswith('399'):
    if len(asset) > len(df1):
        ax3.plot(asset)
        plt.plot(distance)
        ticks = ax3.get_xticks()
        ax3.set_xticklabels(
            [dates[int(i)] for i in (np.append(ticks[:-1],
                                               len(asset) - 1))],
            rotation=15)
        n = 5
        d = (-c_high + c_low) / n
        c = c_high
        while c <= c_low:
            Y = X * b + a - c
            plt.plot(X, Y - Y_hat, 'r', alpha=0.9)
            c = c + d
        ax3.plot(asset)
        ## plt.xlabel('Date', fontsize=12)
        plt.ylabel('Price-center price', fontsize=14)
        plt.grid(True)
    else:
        as3 = asset.apply(lambda x: round(x / asset[:1], 2))
        ax3.plot(as3)
        ticks = ax3.get_xticks()
        ax3.plot(asset1, '-r', linewidth=2)

        # show volume bar !!!
        # assvol = df.loc[asset.index]['vol']
        # assvol = assvol.apply(lambda x: round(x / assvol[:1], 2))
        # ax3.plot(assvol, '-g', linewidth=0.5)

        ax3.set_xticklabels(
            [dates[int(i)] for i in (np.append(ticks[:-1],
                                               len(asset) - 1))],
            rotation=15)
        plt.grid(True)
        zp3 = zoompan.ZoomPan()
        figZoom = zp3.zoom_factory(ax3, base_scale=scale)
        figPan = zp3.pan_factory(ax3)
    # plt.title(code, fontsize=14)

    if 'name' in df.columns:
        plt.legend([df.name.values[-1:][0], df1.name.values[-1:][0]], loc=0)
    else:
        if code not in ['999999', '399006', '399001']:
            indexIdx = False
        else:
            indexIdx = True
        dm = tdd.get_sina_data_df(code, index=indexIdx)
        if 'name' in dm.columns:
            cname = dm.name[0]
        else:
            cname = '-'
        # plt.legend([code, code2], loc=0)
        plt.legend([cname, code2], loc=0)

    ax2 = fig.add_subplot(323)
    # ax2.plot(asset)
    # ticks = ax2.get_xticks()
    ax2.set_xticklabels(
        [dates[int(i)] for i in (np.append(ticks[:-1],
                                           len(asset) - 1))],
        rotation=15)
    # plt.plot(X, Y_hat, 'k', alpha=0.9)
    n = 5
    d = (-c_high + c_low) / n
    c = c_high
    while c <= c_low:
        Y = X * b + a - c
        plt.plot(X, Y, 'r', alpha=0.9)
        c = c + d
    # asset=asset.apply(lambda x:round(x/asset[:1],2))
    ax2.plot(asset)
    # ax2.plot(asset1,'-r', linewidth=2)
    # plt.xlabel('Date', fontsize=12)
    plt.ylabel('Price', fontsize=12)
    plt.grid(True)

    # plt.title(code, fontsize=14)
    # plt.legend([code])

    if len(df) > 10:
        ax6 = fig.add_subplot(324)
        h = df.loc[:, ['open', 'close', 'high', 'low']]
        highp = h['high'].values
        lowp = h['low'].values
        openp = h['open'].values
        closep = h['close'].values
        # print len(closep)
        lr = LinearRegression()
        x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T
        lr.fit(x, closep)
        LinearRegression(copy_X=True,
                         fit_intercept=True,
                         n_jobs=1,
                         normalize=False)
        xt = np.atleast_2d(np.linspace(0,
                                       len(closep) + 200,
                                       len(closep) + 200)).T
        yt = lr.predict(xt)
        bV = []
        bP = []
        for i in range(1, len(highp) - 1):
            if highp[i] <= highp[i - 1] and highp[i] < highp[
                    i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i +
                                                                         1]:
                bV.append(lowp[i])
                bP.append(i)
            else:
                bV.append(lowp[i - 1])
                bP.append(i - 1)
        if len(bV) > 0:

            d, p = LIS(bV)

            idx = []
            for i in range(len(p)):
                idx.append(bP[p[i]])
            lr = LinearRegression()
            X = np.atleast_2d(np.array(idx)).T
            Y = np.array(d)
            lr.fit(X, Y)
            estV = lr.predict(xt)
            ax6.plot(closep, linewidth=2)
            ax6.plot(idx, d, 'ko')
            ax6.plot(xt, estV, '-r', linewidth=3)
            ax6.plot(xt, yt, '-g', linewidth=3)
            plt.grid(True)

            # plt.tight_layout()
            zp2 = zoompan.ZoomPan()
            figZoom = zp2.zoom_factory(ax6, base_scale=scale)
            figPan = zp2.pan_factory(ax6)

    # 统计出每个区域内各股价的频数,得到直方图,为了更精细的显示各个区域的频数,这里将整个边界区间分成100份。

    ax4 = fig.add_subplot(325)
    log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat),
                                   (asset.values.T - Y_hat)[0]))
    # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100)
    # now_distanse=int((asset.iat[-1]-Y_hat[-1])/Y_hat[-1]*100)
    # log.debug("dis:%s now:%s"%(distance[:2],now_distanse))
    # log.debug("now_distanse:%s"%now_distanse)
    distance = (asset.values.T - Y_hat)
    now_distanse = asset.iat[-1] - Y_hat[-1]
    # distance = (asset.values.T-Y_hat)[0]
    pd.Series(distance).plot(kind='hist', stacked=True, bins=100)
    # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9)
    plt.axvline(now_distanse, hold=None, label="1", color='red')
    # plt.axhline(now_distanse,hold=None,label="1",color='red')
    # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--")
    plt.xlabel(
        'Undervalue ------------------------------------------> Overvalue',
        fontsize=12)
    plt.ylabel('Frequency', fontsize=14)
    # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14)
    plt.legend([code, asset.iat[-1], str(dates[-1])[5:11]], fontsize=12)
    plt.grid(True)

    # plt.show()
    # import os
    # print(os.path.abspath(os.path.curdir))

    ax5 = fig.add_subplot(326)
    # fig.figsize=(5, 10)
    log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat),
                                   (asset.values.T - Y_hat)[0]))
    # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100)
    distance = (asset.values.T - Y_hat) / Y_hat * 100
    now_distanse = ((asset.iat[-1] - Y_hat[-1]) / Y_hat[-1] * 100)
    log.debug("dis:%s now:%s" % (distance[:2], now_distanse))
    log.debug("now_distanse:%s" % now_distanse)
    # n, bins = np.histogram(distance, 50)
    # print n, bins[:2]
    pd.Series(distance).plot(kind='hist', stacked=True, bins=100)
    # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9)
    plt.axvline(now_distanse, hold=None, label="1", color='red')
    # plt.axhline(now_distanse,hold=None,label="1",color='red')
    # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--")
    plt.xlabel(
        'Undervalue ------------------------------------------> Overvalue',
        fontsize=14)
    plt.ylabel('Frequency', fontsize=12)
    # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14)
    plt.legend([code, asset.iat[-1]], fontsize=12)
    plt.grid(True)

    # plt.ion()
    plt.draw()
    plt.pause(0.001)

    # plt.show(block=False)

    # plt.draw()
    # plt.pause(0.001)
    # plt.close()
    # print plt.get_backend()
    # plt.show(block=True)
    return df