Verification of Golden Cross and Dead Cross 2(ゴールデンクロスとデッドクロスの検証2)

Verification of Golden Cross and Dead Cross 2(ゴールデンクロスとデッドクロスの検証2)

*** I wrongly used only the last 50 days data. *** まちがえて直近50日間のデータのみ使って解析していました。


It is often said that buying at golden cross and selling at death cross is good. I tested if it is true or not. The data were closing stock prices in Japanese stock market during January 1, 2014 to January 23, 2015.

How often you buy low and sell high when you buy at golden cross and sell at death cross (単にゴールデンクロスで買い、デッドクロスで売ると、差額が正になるのは何割か)

stock cord, date, close, buy/sell -> date, close, buy/sell (, RIGHT)
if you buy low and sell high, flag is "RIGHT".

証券コード、日付、終値、売買 -> 日付、終値、売買 (, RIGHT)
で表した。売買というのは、ゴールデンクロスならBUY, デッドクロスなら売り。2クロスの終値を比較して、安く売って高く買っていた場合、RIGHTというフラグをつけた


5 days and 24 days moving averages (5日平均、24日平均による移動平均線)

1942 2015-01-22 685.0 SELL -> 2015-01-23 710.0 BUY
1948 2015-01-20 214.0 BUY -> 2015-01-22 214.0 SELL
1948 2015-01-22 214.0 SELL -> 2015-01-23 213.0 BUY RIGHT
2498 2015-01-20 692.0 SELL -> 2015-01-23 693.0 BUY
3001 2015-01-20 1261.0 BUY -> 2015-01-22 1239.0 SELL
3408 2015-01-20 170.0 SELL -> 2015-01-21 175.0 BUY
3738 2015-01-21 1229.0 SELL -> 2015-01-23 1254.0 BUY
4471 2015-01-21 851.0 SELL -> 2015-01-22 879.0 BUY
4645 2015-01-20 244.0 SELL -> 2015-01-23 244.0 BUY
6085 2015-01-20 3160.0 SELL -> 2015-01-22 3205.0 BUY
6258 2015-01-20 660.0 SELL -> 2015-01-23 673.0 BUY
6408 2015-01-21 326.0 BUY -> 2015-01-22 321.0 SELL
7768 2015-01-20 486.0 SELL -> 2015-01-23 489.0 BUY
7863 2015-01-20 325.0 BUY -> 2015-01-22 319.0 SELL
8154 2015-01-21 1433.0 BUY -> 2015-01-22 1416.0 SELL
1 out of 15



5 days and 100 days moving averages (5日平均、100日平均による移動平均線)

2449 2015-01-22 1103.0 SELL -> 2015-01-23 1125.0 BUY
3201 2015-01-21 755.0 BUY -> 2015-01-22 757.0 SELL RIGHT
3807 2015-01-20 392.0 BUY -> 2015-01-21 370.0 SELL
3807 2015-01-21 370.0 SELL -> 2015-01-22 367.0 BUY RIGHT
6929 2015-01-20 1648.0 SELL -> 2015-01-23 1678.0 BUY
2 out of 5

10 days and 100 days moving averages (10日平均、100日平均による移動平均線)

5214 2015-01-19 531.0 SELL -> 2015-01-22 539.0 BUY
5563 2015-01-19 281.0 SELL -> 2015-01-21 292.0 BUY
5902 2015-01-20 287.0 SELL -> 2015-01-22 289.0 BUY
6901 2015-01-13 236.0 BUY -> 2015-01-22 228.0 SELL
7313 2015-01-19 2580.0 SELL -> 2015-01-21 2917.0 BUY
7631 2015-01-16 3245.0 SELL -> 2015-01-23 3345.0 BUY
7709 2015-01-19 275.0 SELL -> 2015-01-21 281.0 BUY
7817 2015-01-13 3170.0 SELL -> 2015-01-23 3235.0 BUY
0 out of 8

50 days and 100 days moving averages (50日平均、100日平均による移動平均線)

1379 2014-12-04 1997.0 BUY -> 2014-12-09 1983.0 SELL
1379 2014-12-09 1983.0 SELL -> 2014-12-25 1989.0 BUY
1885 2014-12-15 194.0 SELL -> 2014-12-29 205.0 BUY
1933 2014-11-17 332.0 SELL -> 2015-01-19 366.0 BUY
2175 2014-12-01 1319.0 BUY -> 2014-12-11 1292.5 SELL
2594 2014-11-13 1625.0 SELL -> 2015-01-20 1676.0 BUY
2782 2014-12-17 3990.0 BUY -> 2015-01-16 3490.0 SELL
3027 2014-11-21 535.0 SELL -> 2014-12-16 540.0 BUY
3316 2014-11-04 1280.0 SELL -> 2015-01-19 1440.0 BUY
3366 2014-11-14 446.0 BUY -> 2014-12-11 432.0 SELL
4224 2014-11-17 142.0 SELL -> 2015-01-14 141.0 BUY RIGHT
4224 2015-01-14 141.0 BUY -> 2015-01-16 142.0 SELL RIGHT
4540 2014-11-11 2560.0 SELL -> 2014-12-19 2714.0 BUY
4625 2014-10-31 445.0 SELL -> 2015-01-20 486.0 BUY
4686 2014-11-12 800.0 BUY -> 2014-11-28 809.0 SELL RIGHT
4718 2014-11-13 787.0 SELL -> 2014-12-29 797.0 BUY
4745 2014-12-15 363.0 BUY -> 2014-12-16 355.0 SELL
4745 2014-12-16 355.0 SELL -> 2014-12-17 355.0 BUY
4745 2014-12-17 355.0 BUY -> 2015-01-14 351.0 SELL
4760 2014-11-26 179.0 SELL -> 2014-12-08 184.0 BUY
4760 2014-12-08 184.0 BUY -> 2014-12-09 177.0 SELL
4914 2014-12-01 556.0 SELL -> 2015-01-06 543.0 BUY RIGHT
6059 2014-11-18 626.0 BUY -> 2014-12-10 607.0 SELL
6104 2014-12-09 495.0 SELL -> 2015-01-07 459.0 BUY RIGHT
6249 2014-11-12 1505.0 SELL -> 2015-01-14 1562.0 BUY
6259 2014-12-25 413.0 SELL -> 2014-12-30 424.0 BUY
6286 2014-11-07 541.0 SELL -> 2015-01-14 573.0 BUY
6409 2014-11-12 1261.0 BUY -> 2014-12-03 1197.0 SELL
6487 2014-11-10 201.0 BUY -> 2014-11-17 183.0 SELL
6487 2014-11-17 183.0 SELL -> 2014-12-05 211.0 BUY
6586 2014-11-27 6070.0 BUY -> 2014-12-11 5710.0 SELL
6586 2014-12-11 5710.0 SELL -> 2014-12-26 5490.0 BUY RIGHT
6586 2014-12-26 5490.0 BUY -> 2015-01-15 5260.0 SELL
6627 2015-01-05 1070.0 BUY -> 2015-01-06 1063.0 SELL
6662 2014-12-04 292.0 SELL -> 2015-01-19 282.0 BUY RIGHT
7267 2014-11-13 3626.0 BUY -> 2014-12-04 3725.0 SELL RIGHT
7267 2014-12-04 3725.0 SELL -> 2015-01-07 3425.5 BUY RIGHT
7273 2014-12-10 168.0 SELL -> 2014-12-15 214.0 BUY
7274 2015-01-07 1098.0 BUY -> 2015-01-21 1069.0 SELL
7413 2014-11-17 127.0 SELL -> 2015-01-23 283.0 BUY
7504 2014-11-17 924.0 BUY -> 2014-12-02 900.0 SELL
7709 2014-12-01 285.0 BUY -> 2014-12-19 266.0 SELL
7709 2014-12-19 266.0 SELL -> 2014-12-22 270.0 BUY
7721 2014-11-20 273.0 SELL -> 2015-01-07 298.0 BUY
7932 2014-11-20 766.0 BUY -> 2014-12-04 791.0 SELL RIGHT
7932 2014-12-04 791.0 SELL -> 2015-01-05 765.0 BUY RIGHT
8007 2014-11-25 242.0 SELL -> 2014-12-30 252.0 BUY
8038 2014-12-01 199.0 SELL -> 2014-12-29 200.0 BUY
8136 2014-12-19 3020.0 SELL -> 2015-01-05 3050.0 BUY
8136 2015-01-05 3050.0 BUY -> 2015-01-19 2859.0 SELL
8165 2014-11-14 875.0 BUY -> 2014-12-15 858.0 SELL
11 out of 51

5 days and 200 days moving averages (5日平均、200日平均による移動平均線)

7476 2015-01-22 3115.0 SELL -> 2015-01-23 3130.0 BUY 0 out of 1

25 days and 200 days moving averages (25日平均、200日平均による移動平均線)

2169 2014-12-17 988.0 SELL -> 2014-12-19 1005.0 BUY
2169 2014-12-19 1005.0 BUY -> 2015-01-14 951.0 SELL
3088 2014-12-19 3350.0 SELL -> 2014-12-26 3420.0 BUY
3264 2015-01-14 139.0 SELL -> 2015-01-22 152.0 BUY
3408 2014-12-18 167.0 BUY -> 2015-01-19 168.0 SELL RIGHT
3683 2015-01-08 1419.0 BUY -> 2015-01-09 1415.0 SELL
3683 2015-01-09 1415.0 SELL -> 2015-01-20 1424.0 BUY
3683 2015-01-20 1424.0 BUY -> 2015-01-22 1429.0 SELL RIGHT
3683 2015-01-22 1429.0 SELL -> 2015-01-23 1429.0 BUY
3951 2014-12-22 2325.0 BUY -> 2014-12-25 2270.0 SELL
3951 2014-12-25 2270.0 SELL -> 2014-12-29 2300.0 BUY
3951 2014-12-29 2300.0 BUY -> 2015-01-06 2279.0 SELL
4653 2014-12-24 887.0 SELL -> 2015-01-06 915.0 BUY
4653 2015-01-06 915.0 BUY -> 2015-01-07 907.0 SELL
4653 2015-01-07 907.0 SELL -> 2015-01-08 918.0 BUY
4653 2015-01-08 918.0 BUY -> 2015-01-09 910.0 SELL
5603 2014-12-17 211.0 SELL -> 2014-12-30 212.0 BUY
5603 2014-12-30 212.0 BUY -> 2015-01-05 211.0 SELL
6016 2014-12-29 104.0 BUY -> 2015-01-20 104.0 SELL
6016 2015-01-20 104.0 SELL -> 2015-01-23 107.0 BUY
6309 2014-12-17 1672.0 SELL -> 2014-12-25 1717.0 BUY
6309 2014-12-25 1717.0 BUY -> 2015-01-13 1689.0 SELL
6488 2014-12-26 668.0 BUY -> 2015-01-09 671.0 SELL RIGHT
6488 2015-01-09 671.0 SELL -> 2015-01-21 673.0 BUY
6488 2015-01-21 673.0 BUY -> 2015-01-23 665.0 SELL
6819 2014-12-16 62.0 BUY -> 2014-12-18 62.0 SELL
6819 2014-12-18 62.0 SELL -> 2015-01-05 68.0 BUY
7214 2015-01-06 1316.0 BUY -> 2015-01-19 1286.0 SELL
7314 2014-12-16 1090.0 SELL -> 2014-12-18 1107.0 BUY
7445 2014-12-19 707.0 SELL -> 2014-12-30 742.0 BUY
8136 2014-12-24 3050.0 BUY -> 2015-01-13 2996.0 SELL
3 out of 31

50 days and 200 days moving averages (50日平均、200日平均による移動平均線)

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

Source Code

I write python code. stocks.db has stock data taken from Yahoo!Finance.
# -*- coding: utf8 -*- 
#!\usr\local\bin\python3

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 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(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=df[-short_term:]
        df['fluctuation'] = pd.Series([i-j for i, j in zip(mavg_short[-short_term:], mavg_long[-short_term:])], index=df.index[-short_term:])
        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):
                prev_day = kys[i-1]
                the_day = j
                #print(ccode, signal[j], signal[kys[i-1]])
                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(nice, 'out of', all)

Conclusion: Just Golden cross and Death cross are not enough signal

As you saw above, 50 and 100 days moving averages are best at 11/51 = 21.6% ratio to get benefit. It is not worth

Discussion: Watch out a long term moving average is rising or falling

In order to avoid fluctuation and extract just variation of market's trend, we have to take care if long term moving averages are rising or falling at a golden cross and a death cross. A long term moving average sometimes cannot reflect a sudden market's change but a short term average can do. At cross points, two moving averages should rise or fall. Next time, I consider if long term moving averages go up or down at cross points.

コメント