Verification of Golden Cross and Death Cross 4: With watching if long term moving averages rise up or fall down


Verification of Golden Cross and Death Cross 3:  With watching if long term moving averages rise up or fall down

I compared the close prices on a golden cross and a successive death cross. I extracted a golden cross only when long term averages rise up and a death cross only when long term averages fall down.




Source Code

For more information, take a look at my previous article: [Python] Verification of Golden cross and Death cross 3 because below codes misses about stocks.db and trend_test.pickle. I added a condition cond1_long when taking crosses from historical data. cond1_long is for checking a long term average goes up or down dureing the day at a cross and a previous day.

def golden_vs_death(short_term=50, long_term=200):
    ccodes = pickle.load(open('test_trend.pickle', 'rb')).columns
    all = 0
    nice = 0
    for ccode in ccodes:
        df = get_df(ccode)
        if len(df)<=long_term:
            return None
        mavg_short = pd.rolling_mean(df['close'], short_term)
        mavg_long = pd.rolling_mean(df['close'], long_term)
        df['mavg_short'] = pd.Series(mavg_short, index=df.index)
        df['mavg_long'] = pd.Series(mavg_long, index=df.index)
        df['fluctuation'] =df['mavg_short']-df['mavg_long']
        df=df[long_term:]
        signal = {}
        # signal stocks date and golden cross (BUY) or death cross (SELL)
        for i,j in enumerate(df['fluctuation']):
            cond1_long = df['mavg_long'].iloc[i]-df['mavg_long'].iloc[i-1]>0
            cond2_long = df['mavg_long'].iloc[i-1]-df['mavg_long'].iloc[i-2]>0
            if i==0:
                continue             
            # also watch long term moving averages go up or down    
            elif df['fluctuation'].iloc[i]>0 and df['fluctuation'].iloc[i-1]<0 data-blogger-escaped-and="" data-blogger-escaped-cond1_long:="" 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 and not cond1_long:
                signal[df.index[i]]='SELL'
            else:
                continue
        if len(signal)>1:

            kys=sorted(signal)
            for i,j in enumerate(kys):
                prev_day = kys[i-1]
                the_day = j
                if i==0:
                    pass
                elif signal[prev_day]=='SELL' and signal[the_day]=='BUY':
                    all+=1
                    if df['close'].loc[prev_day] > df['close'].loc[the_day]:
                        print(ccode, prev_day.date(), df['close'].loc[prev_day], 'SELL', '->', j.date(), df['close'].loc[the_day],'BUY RIGHT')
                        nice+=1
                    else:
                        print(ccode, prev_day.date(),df['close'].loc[prev_day], 'SELL', '->', j.date(), df['close'].loc[the_day],'BUY')
                elif signal[prev_day]=='BUY' and signal[the_day]=='SELL':
                    all+=1
                    if df['close'].loc[prev_day] < df['close'].loc[the_day]:
                        print(ccode, prev_day.date(), df['close'].loc[prev_day], 'BUY', '->', j.date(), df['close'].loc[the_day],'SELL RIGHT')
                        nice+=1
                    else:
                        print(ccode, prev_day.date(), df['close'].loc[prev_day], 'BUY', '->',j.date(), df['close'].loc[the_day],'SELL')
                else:
                    pass
    print('{0}/{1} = {2}%'.format(nice, all, round(100*nice/all,1)))


I found later that I made a pickle file which has Golden crosses and Death crosses with 50 days and 200 days moving averages.

Results

a short term is 5 days and a long term is 24 days

1301 2014-02-19 267 SELL -> 2014-02-21 271 BUY
1301 2014-02-21 271 BUY -> 2014-03-17 265 SELL
1301 2014-05-12 256 SELL -> 2014-06-30 260 BUY
1301 2014-06-30 260 BUY -> 2014-07-01 259 SELL
1301 2014-07-01 259 SELL -> 2014-09-11 254 BUY RIGHT
1301 2014-09-11 254 BUY -> 2014-10-09 254 SELL
1301 2014-10-09 254 SELL -> 2014-10-30 257 BUY
1379 2014-07-29 1996.0 BUY -> 2014-07-31 1962.0 SELL
1379 2014-07-31 1962.0 SELL -> 2014-08-20 1999.0 BUY
1379 2014-08-20 1999.0 BUY -> 2014-08-28 1967.0 SELL
1379 2014-10-02 1951.0 SELL -> 2014-10-27 2012.0 BUY
1383 2014-04-17 1495.0 SELL -> 2014-07-07 1539.0 BUY
1383 2014-07-07 1539.0 BUY -> 2014-09-25 1521.0 SELL
...
8309 2014-06-04 441.0 BUY -> 2014-07-11 447.0 SELL RIGHT
8309 2014-09-01 430.5 SELL -> 2014-11-04 477.0 BUY
8309 2014-11-04 477.0 BUY -> 2014-12-12 466.9 SELL
8316 2014-05-19 3909.0 SELL -> 2014-05-29 4102.0 BUY
8316 2014-07-07 4296.0 BUY -> 2014-07-09 4229.0 SELL
8316 2014-08-07 4100.0 SELL -> 2014-11-04 4543.0 BUY
8316 2014-11-04 4543.0 BUY -> 2014-12-11 4317.0 SELL
8350 2014-03-25 211.0 BUY -> 2014-04-14 201.0 SELL
8350 2014-04-14 201.0 SELL -> 2014-04-21 213.0 BUY
8350 2014-04-21 213.0 BUY -> 2014-05-13 207.0 SELL
8350 2014-10-03 209.0 SELL -> 2014-10-31 213.0 BUY
344/2632 = 13.1%

a short term is 5 days and a long term is 100 days

1301 2014-07-31 258 SELL -> 2014-09-29 263 BUY
1379 2014-07-02 2016.0 BUY -> 2014-08-27 1968.0 SELL
1379 2014-10-06 1962.0 SELL -> 2014-10-28 2035.0 BUY
1379 2014-10-28 2035.0 BUY -> 2014-12-12 1946.0 SELL
1379 2014-12-12 1946.0 SELL -> 2014-12-29 2001.0 BUY
...
8309 2014-08-05 427.5 SELL -> 2014-09-05 445.2 BUY
8316 2014-08-06 4068.0 SELL -> 2014-11-04 4543.0 BUY
8350 2014-10-02 210.0 SELL -> 2014-11-06 213.0 BUY
4/580 = 0.7%

a short term is 5 days and a long term is 200 days


1514 2014-11-17 118.0 SELL -> 2014-11-27 126.0 BUY
1514 2014-11-27 126.0 BUY -> 2014-12-16 116.0 SELL
1662 2014-11-20 3955.0 BUY -> 2014-12-01 3625.0 SELL
1937 2014-11-04 490.0 BUY -> 2014-11-14 465.0 SELL
1937 2014-11-14 465.0 SELL -> 2014-12-25 479.0 BUY
1964 2014-11-25 218.0 SELL -> 2014-12-05 249.0 BUY
...
8153 2014-11-07 2105.0 SELL -> 2014-12-10 2164.0 BUY
8226 2014-11-12 205.0 SELL -> 2014-12-01 226.0 BUY
8267 2014-12-18 1161.0 SELL -> 2015-01-20 1214.5 BUY
8282 2014-11-10 2850.0 SELL -> 2014-11-14 3055.0 BUY
1/125 = 0.8%

a short term is 10 days and a long term is 100 days

1301 2014-10-10 251 SELL -> 2014-11-07 261 BUY
1379 2014-07-08 2000.0 BUY -> 2014-09-03 1979.0 SELL
1379 2014-09-03 1979.0 SELL -> 2014-10-31 2038.0 BUY
1379 2014-10-31 2038.0 BUY -> 2014-12-16 1912.0 SELL
1379 2014-12-16 1912.0 SELL -> 2015-01-06 1988.0 BUY
1383 2014-10-02 1500.0 SELL -> 2014-11-20 1551.0 BUY
1383 2014-11-20 1551.0 BUY -> 2014-12-18 1452.0 SELL
...
8309 2014-08-06 422.2 SELL -> 2014-09-10 452.3 BUY
8309 2014-11-11 473.4 BUY -> 2015-01-13 423.7 SELL
8316 2014-10-16 3933.5 SELL -> 2014-11-07 4427.0 BUY
8350 2014-09-26 212.0 SELL -> 2014-09-30 213.0 BUY
2/356 = 0.6%

a short term is 10 days and a long term is 200 days

1514 2014-11-27 126.0 BUY -> 2014-12-18 117.0 SELL
1937 2014-11-06 482.0 BUY -> 2014-11-19 470.0 SELL
1937 2014-11-19 470.0 SELL -> 2014-12-30 488.0 BUY
1964 2014-11-27 213.0 SELL -> 2014-12-05 249.0 BUY
2152 2014-11-27 814.0 BUY -> 2014-12-10 799.0 SELL
...
7999 2014-12-01 500.0 BUY -> 2014-12-16 466.0 SELL
8031 2014-10-31 1665.5 BUY -> 2014-12-15 1527.5 SELL
8226 2014-11-14 204.0 SELL -> 2014-12-03 216.0 BUY
0/75 = 0.0%


a short term is 25 days and a long term is 100 days

1383 2014-07-14 1514.0 SELL -> 2014-12-02 1571.0 BUY
1383 2014-12-02 1571.0 BUY -> 2014-12-30 1439.0 SELL
1405 2014-07-07 858.0 BUY -> 2014-11-18 841.0 SELL
1414 2014-07-24 4425.0 SELL -> 2014-12-02 4560.0 BUY
...
8316 2014-07-25 4095.5 SELL -> 2014-09-03 4321.5 BUY
8316 2014-09-08 4328.5 BUY -> 2014-10-28 4020.5 SELL
8316 2014-10-28 4020.5 SELL -> 2014-11-20 4469.0 BUY
8316 2014-11-20 4469.0 BUY -> 2015-01-14 4007.5 SELL
8350 2014-08-26 209.0 SELL -> 2014-11-21 230.0 BUY
2/266 = 0.8%


a short term is 25 days and a long term is 200 days

1379 2014-11-21 2005.0 BUY -> 2014-12-15 1944.0 SELL
1383 2014-11-27 1572.0 BUY -> 2014-12-30 1439.0 SELL
1873 2014-11-04 475.0 SELL -> 2015-01-07 511.0 BUY
1937 2014-11-13 463.0 SELL -> 2015-01-09 485.0 BUY
2340 2014-11-25 439.0 SELL -> 2015-01-09 638.0 BUY
...
7997 2014-11-12 132.0 SELL -> 2014-12-04 136.0 BUY
8002 2014-12-01 745.7 BUY -> 2015-01-06 695.3 SELL
8160 2014-10-29 1864.0 SELL -> 2014-12-08 1918.0 BUY
8217 2014-10-28 870.0 SELL -> 2014-11-25 963.0 BUY
8309 2014-11-26 490.6 BUY -> 2015-01-21 420.5 SELL
1/51 = 2.0%

a short term is 50 days and a long term is 100 days

1405 2014-07-29 874.0 BUY -> 2014-12-11 837.0 SELL
1414 2014-08-12 4250.0 SELL -> 2014-12-22 4690.0 BUY
1515 2014-07-07 460.0 BUY -> 2014-10-27 398.0 SELL
1782 2014-10-09 298.0 SELL -> 2015-01-13 324.0 BUY
...
8153 2014-06-27 2185.0 BUY -> 2014-10-23 2110.0 SELL 8153 2014-10-23 2110.0 SELL -> 2015-01-23 2339.0 BUY8166 2014-10-06 209.0 SELL -> 2015-01-13 218.0 BUY 8281 2014-08-21 1718.0 SELL -> 2014-12-08 1982.0 BUY 5/135 = 3.7%

a short term is 50 days and a long term is 200 days

1383 2014-12-08 1577.0 BUY -> 2014-12-26 1441.0 SELL
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
2340 2014-12-16 432.0 SELL -> 2015-01-15 686.0 BUY
2445 2014-11-14 732.5 SELL -> 2014-12-02 891.5 BUY
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
2918 2014-10-30 1896.0 SELL -> 2015-01-05 2032.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
4540 2014-11-05 2515.0 SELL -> 2014-11-27 2691.0 BUY
5451 2014-11-06 407.0 SELL -> 2015-01-07 446.0 BUY
5714 2014-10-28 848.0 SELL -> 2014-12-03 975.0 BUY
6406 2014-11-26 1139.0 SELL -> 2014-12-26 1264.0 BUY
6488 2014-12-05 670.0 SELL -> 2015-01-14 672.0 BUY
6779 2014-10-31 869.0 SELL -> 2015-01-05 983.0 BUY
6819 2014-12-05 60.0 SELL -> 2015-01-09 65.0 BUY
7601 2014-10-28 530.0 SELL -> 2015-01-05 554.0 BUY
7715 2014-11-17 672.0 SELL -> 2015-01-16 681.0 BUY
7911 2014-10-28 707.0 SELL -> 2015-01-05 788.0 BUY
7987 2014-11-18 205.0 SELL -> 2015-01-05 209.0 BUY
7991 2014-10-29 197.0 SELL -> 2015-01-20 223.0 BUY
8153 2014-12-02 2118.0 SELL -> 2015-01-09 2252.0 BUY
8154 2014-10-28 1197.0 SELL -> 2014-11-10 1370.0 BUY
1/24 = 4.2%

Results

It is still too bad. Terrible. Useless.

Discussion

ccode = 1515 with 5 days and 24 days moving averages.
Above graph shows ccode=1515 with 50 days and 200 days moving averages, which signals only one RIGHT in above Results. It does not seem to something we can predict.
ccode = 4540 with 50 days and 200 days moving averages.
ccode = 2918 with 50 days and 200 days moving averages. They seem pretty much far from actual close prices trends.
ccode = 2918 with 10 days and 100 days moving averages. There seem to be two death crosses.
ccode = 2918 with 5 days and 25 days moving averages. Somehow I feel it as useful to reflect trends but current my code does not catch a signal well.
ccode = 8309 with 5 days and 25 days. It seems to have a nice golden and a death cross.
ccode = 1301 with 5 days and 25 days moving averages.
I have to think more and investigate articles. One more thing I want to test is the golden cross before that a short and a long moving averages are alike. That stocks are still, but at some time something ignite it and make it popular.

BiIBLIOGRAPHY

コメント

人気の投稿