株価のトレンド変化について検証 -*- ゴールデンクロスとデスクロスについて -+-.

株価のトレンド変化

株価は、短期トレンド、長期トレンドなどがある。短期トレンドが長期トレンドを上回れば、その後は上がるだろうと (golden cross)、また、短期トレンドが長期トレンドを下回れば、その後は下がるだろうと予想できる (death cross)。短期、長期は10日や15日、一年以下など、100日や200日、一年以上など様々。

嫌な例。下がると思って売ったのに高くなって買う。



これも予想を外した例。ゴールデンクロスの後、デスクロスでより下がっている。

増減を短期間で繰り返している例。


クロスとは関係なく、最近ゲームで復活したと言われていたmixi。


全体の何%がうまくいっているのか過去のデータから検証した。
stocks.db中の、例えばstocks2121には次がSQL形式で格納されている。
ccode=2121
start_date = datetime.datetime(2014,1,1)
end_date = datetime.date.today()
jsm.Quotes().get_historical_prices(ccode,jsm.DAILY,start_date=start_date,end_date=end_date)

200日平均、50日平均を比較して、クロスが二個あるものを抽出し、クロス時点の終値を比較した。


import pandas as pd
import sqlite3
import datetime
import matplotlib
import matplotlib.pyplot as plt
import pickle

conn = sqlite3.connect('stocks.db')
c = conn.cursor()

def get_name(ccode):
    brand = pd.read_sql('SELECT * FROM brand_data ORDER BY ccode', conn, index_col='ccode')
    return brand.loc[ccode]['name']    

def check_trends_test():
    ccodes = [i[0] for i in c.execute('SELECT ccode FROM brand_data ORDER BY ccode').fetchall()]
    index = get_df(1332).index
    new_df=pd.DataFrame()
    for ccode in ccodes:
        df = get_df(ccode)
        if len(df)<201: data-blogger-escaped-200="" data-blogger-escaped-50="" data-blogger-escaped-close="" data-blogger-escaped-continue="" data-blogger-escaped-i-j="" data-blogger-escaped-if="" data-blogger-escaped-mvavg200="pd.rolling_mean(df[" data-blogger-escaped-mvavg50="pd.rolling_mean(df[" data-blogger-escaped-trend="[1">0 else -1 for i, j in zip(mvavg50, mvavg200)]
        new_trend = [0 for i in trend]
        for i in range(len(trend)):
            if trend[i]>0 and trend[i-1]<0: data-blogger-escaped-and="" data-blogger-escaped-close="" data-blogger-escaped-df.iloc="" data-blogger-escaped-elif="" data-blogger-escaped-format="" data-blogger-escaped-i-1="" data-blogger-escaped-i="" data-blogger-escaped-new_trend="" data-blogger-escaped-trend="">0:
                new_trend[i] = 'SELL {0}'.format(df.iloc[i]['close'])
            else:
                pass 
        try:
            sum(new_trend[200:])
        except TypeError:
            new_df[ccode] = pd.Series(new_trend, index=df.index)
    pickle.dump(new_df[200:], open('test_trend.pickle', 'wb'))    
    print(new_df.shape)
    print(new_df.iloc[200:])

def golden_vs_death():
    ccodes = pickle.load(open('test_trend.pickle', 'rb')).columns
    all = 0
    nice = 0
    for ccode in ccodes:
        df = get_df(ccode)
        if len(df)<=200:
            return None
        mvmt50 = pd.rolling_mean(df['close'], 50)
        mvmt200 = pd.rolling_mean(df['close'], 200)
        df=df[-50:]
        df['fluctuation'] = pd.Series([i-j for i, j in zip(mvmt50[-50:], mvmt200[-50:])], index=df.index[-50:])
        cnt_cross = 0
        signal = {}
        for i,j in enumerate(df['fluctuation']):
            if i==0:
                pass
            elif df['fluctuation'].iloc[i]>0 and df['fluctuation'].iloc[i-1]<0: data-blogger-escaped-and="" data-blogger-escaped-df.index="" data-blogger-escaped-df="" data-blogger-escaped-elif="" data-blogger-escaped-fluctuation="" data-blogger-escaped-i-1="" data-blogger-escaped-i="" data-blogger-escaped-iloc="" data-blogger-escaped-signal="">0:
                signal[df.index[i]]='SELL'
            else:
                pass
        if len(signal)>1:

            kys=sorted(signal)
            for i,j in enumerate(kys):
                #print(ccode, signal[j], signal[kys[i-1]])
                if i==0:
                    pass
                elif signal[kys[i-1]]=='SELL' and signal[j]=='BUY':
                    all+=1
                    if df['close'].loc[kys[i-1]]>df['close'].loc[j]:
                        print(ccode, kys[i-1].date(), df['close'].loc[kys[i-1]], 'SELL', '->', j.date(), df['close'].loc[j],'BUY RIGHT')
                        nice+=1
                    else:
                        print(ccode, kys[i-1].date(),df['close'].loc[kys[i-1]], 'SELL', '->', j.date(), df['close'].loc[j],'BUY')
                elif signal[kys[i-1]]=='BUY' and signal[j]=='SELL':
                    all+=1
                    if df['close'].loc[kys[i-1]]', j.date(), df['close'].loc[j],'SELL RIGHT')
                        nice+=1
                    else:
                        print(ccode, kys[i-1].date(), df['close'].loc[kys[i-1]], 'BUY', '->',j.date(), df['close'].loc[j],'SELL')
                else:
                    pass
    print(nice, 'out of', all)

得られたのは次の結果。
証券コード、株価の変化、シグナルのBUYまたはSELL、的中していたらRIGHTのフラグを立ててある。
長期200日, 短期50日で10/89 = 11%の的中率だった。これで取引していたら損してしまうんじゃないか。

1383 2014-12-08 1577.0 BUY -> 2014-12-26 1441.0 SELL
1514 2014-11-10 122.0 BUY -> 2014-12-05 124.0 SELL RIGHT
1515 2014-11-27 433.0 SELL -> 2015-01-14 418.0 BUY RIGHT
1844 2014-11-28 34.0 SELL -> 2015-01-14 36.0 BUY
1921 2014-12-15 438.0 SELL -> 2014-12-25 446.0 BUY
1933 2014-12-22 342.0 SELL -> 2015-01-05 374.0 BUY
2160 2014-12-12 311.0 SELL -> 2014-12-29 326.0 BUY
2173 2015-01-14 555.0 SELL -> 2015-01-22 580.0 BUY
2222 2014-12-08 2165.0 SELL -> 2015-01-07 2354.0 BUY
2309 2014-12-11 1706.0 SELL -> 2015-01-20 1999.0 BUY
2340 2014-12-16 432.0 SELL -> 2015-01-15 686.0 BUY
2389 2014-12-30 735.0 BUY -> 2015-01-21 698.0 SELL
2445 2014-11-14 732.5 SELL -> 2014-12-02 891.5 BUY
2480 2014-11-10 805.0 BUY -> 2014-12-10 746.0 SELL
2594 2014-12-22 1647.0 SELL -> 2014-12-24 1657.0 BUY
2676 2014-11-28 1034.0 SELL -> 2015-01-16 1026.0 BUY RIGHT
2782 2014-11-14 4480.0 BUY -> 2015-01-14 3510.0 SELL
2902 2014-11-25 740.0 SELL -> 2015-01-14 771.0 BUY
3021 2014-12-10 586.0 BUY -> 2015-01-16 486.0 SELL
3322 2014-12-04 1180.0 BUY -> 2015-01-06 1149.0 SELL
3366 2014-11-12 447.0 BUY -> 2014-12-05 448.0 SELL RIGHT
3384 2014-12-19 293.0 SELL -> 2015-01-13 301.0 BUY
3441 2014-11-21 394.0 SELL -> 2014-12-22 428.0 BUY
3741 2014-12-12 4145.0 SELL -> 2014-12-29 4020.0 BUY RIGHT
3769 2014-12-04 2115.0 SELL -> 2015-01-06 2260.0 BUY
3800 2014-12-12 1199.0 SELL -> 2014-12-29 1349.0 BUY
3807 2014-12-19 356.0 SELL -> 2015-01-08 406.0 BUY
4031 2014-11-12 262.0 SELL -> 2015-01-20 277.0 BUY
4093 2014-12-02 169.0 SELL -> 2015-01-06 187.0 BUY
4112 2015-01-20 192.0 SELL -> 2015-01-21 207.0 BUY
4117 2014-11-17 153.0 SELL -> 2015-01-23 159.0 BUY
4282 2014-12-19 1285.0 SELL -> 2015-01-09 1524.0 BUY
4653 2014-12-17 891.0 SELL -> 2015-01-06 915.0 BUY
4653 2015-01-06 915.0 BUY -> 2015-01-16 893.0 SELL
4668 2014-12-02 1235.0 SELL -> 2014-12-30 1225.0 BUY RIGHT
4783 2014-12-02 303.0 SELL -> 2015-01-14 306.0 BUY
4914 2014-11-10 538.0 BUY -> 2014-11-12 512.0 SELL
4914 2014-11-12 512.0 SELL -> 2014-12-16 510.0 BUY RIGHT
4928 2014-12-16 2039.0 SELL -> 2014-12-24 2126.0 BUY
5017 2014-12-25 359.0 SELL -> 2014-12-30 366.0 BUY
5331 2014-11-20 259.0 SELL -> 2015-01-15 278.0 BUY
5355 2014-12-11 170.0 SELL -> 2014-12-24 171.0 BUY
5363 2014-11-20 201.0 SELL -> 2014-12-30 220.0 BUY
5408 2014-11-21 80.0 SELL -> 2014-12-30 81.0 BUY
5727 2014-11-21 683.0 SELL -> 2014-12-26 803.0 BUY
5809 2014-11-10 544.0 BUY -> 2014-11-26 508.0 SELL
5928 2014-12-22 378.0 SELL -> 2015-01-21 386.0 BUY
5987 2014-11-28 146.0 SELL -> 2014-12-30 147.0 BUY
6249 2014-11-25 1503.0 SELL -> 2014-12-22 1535.0 BUY
6298 2014-11-17 570.0 SELL -> 2015-01-23 660.0 BUY
6405 2014-11-19 972.0 BUY -> 2014-11-20 963.0 SELL
6405 2014-11-20 963.0 SELL -> 2014-11-21 965.0 BUY
6406 2014-11-26 1139.0 SELL -> 2014-12-26 1264.0 BUY
6474 2014-12-11 676.0 SELL -> 2015-01-06 723.0 BUY
6488 2014-12-05 670.0 SELL -> 2015-01-14 672.0 BUY
6488 2015-01-14 672.0 BUY -> 2015-01-15 666.0 SELL
6488 2015-01-15 666.0 SELL -> 2015-01-20 673.0 BUY
6624 2014-12-18 995.0 SELL -> 2015-01-13 1174.0 BUY
6627 2014-12-29 1057.0 BUY -> 2015-01-22 995.0 SELL
6651 2014-11-10 2011.0 SELL -> 2014-12-26 2380.0 BUY
6742 2014-11-28 365.0 SELL -> 2014-12-08 380.0 BUY
6819 2014-12-05 60.0 SELL -> 2015-01-09 65.0 BUY
6901 2014-12-10 224.0 SELL -> 2015-01-07 238.0 BUY
6924 2015-01-13 237.0 BUY -> 2015-01-21 229.0 SELL
7213 2014-12-22 850.0 SELL -> 2014-12-26 864.0 BUY
7213 2014-12-26 864.0 BUY -> 2015-01-08 833.0 SELL
7267 2014-11-20 3676.5 BUY -> 2014-12-09 3716.5 SELL RIGHT
7267 2014-12-09 3716.5 SELL -> 2014-12-30 3526.0 BUY RIGHT
7274 2015-01-07 1098.0 BUY -> 2015-01-22 1062.0 SELL
7533 2014-11-18 900.0 SELL -> 2015-01-15 980.0 BUY
7596 2014-12-08 1565.0 SELL -> 2015-01-09 1596.0 BUY
7621 2014-12-09 2200.0 SELL -> 2014-12-19 2219.0 BUY
7631 2014-12-18 3170.0 SELL -> 2015-01-05 3340.0 BUY
7714 2014-11-10 275.0 BUY -> 2014-11-27 266.0 SELL
7715 2014-11-12 681.0 SELL -> 2014-11-14 693.0 BUY
7715 2014-11-14 693.0 BUY -> 2014-11-17 672.0 SELL
7715 2014-11-17 672.0 SELL -> 2015-01-16 681.0 BUY
7886 2014-11-11 104.0 SELL -> 2015-01-15 108.0 BUY
7921 2014-12-09 807.0 SELL -> 2015-01-05 840.0 BUY
7981 2014-12-17 865.0 SELL -> 2015-01-05 874.0 BUY
7981 2015-01-05 874.0 BUY -> 2015-01-14 866.0 SELL
7987 2014-11-18 205.0 SELL -> 2015-01-05 209.0 BUY
7997 2014-12-19 133.0 SELL -> 2015-01-06 134.0 BUY
8007 2014-11-19 240.0 BUY -> 2014-11-26 241.0 SELL RIGHT
8007 2014-11-26 241.0 SELL -> 2014-12-26 248.0 BUY
8103 2014-12-11 459.0 SELL -> 2014-12-24 465.0 BUY
8136 2015-01-13 2996.0 BUY -> 2015-01-19 2859.0 SELL
8153 2014-12-02 2118.0 SELL -> 2015-01-09 2252.0 BUY
8160 2014-12-04 1918.0 SELL -> 2015-01-15 1998.0 BUY
10 out of 89

具体的に上記結果に出てきた会社のグラフを見ると

下降傾向はつかめていても、実際の終わり値を見ると安く売って高く売ることになっていたり、下降予想のトレンドが上昇したり、
突然急上昇したり、
これも突然の急上昇や、
変化が微妙すぎたり
予想通りのトレンドを見せないことも多い。

次は、移動平均の短期、長期期間を変更、または下記文献を参考に試す




BIBLIOGRAPGY

コメント

人気の投稿