Verification of Golden cross and Death cross Should short and long moving averages be close before golden cross6 ゴールデンクロスとデッドクロスの検証6 ゴールデンクロス前の短期及び長期移動平均のは近い方がよいか

Method

I extracted an adjacent golden cross and a death cross only with following condition:
in the last 10 days before a golden cross,the difference of the short moving average's mean and long moving's average is less than short moving average's standard deviation.
隣接したゴールデンクロスとデッドクロスを抽出した。ゴールデンクロスについては、直近10日間の短期移動平均、長期移動平均の平均の差が、短期移動平均の直近10日間の標準偏差以下になるもののみ使った。

Source Code


cond3 is to comparing short and long average's last 10 days mean difference and short moving average's standard deviation
cond3は、短期移動平均、長期移動平均の直近10日間の平均の差と、短期移動平均の直近10日間の標準検査を比較しています


# -*- 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 golden_vs_death(short_term=50, long_term=200):
    ccodes = [i[0] for i in c.execute('SELECT ccode FROM brand_data ORDER BY ccode').fetchall()]
    all = 0
    nice = 0
    for ccode in ccodes:
        df = get_df(ccode)
        if len(df)<=short_term:
            continue
        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
            cond3 = abs(df.ix[i-10:i, 'mavg_short'].mean()-df.ix[i-10:i,'mavg_long'].mean())0 and df['fluctuation'].iloc[i-1]<0 data-blogger-escaped-and="" data-blogger-escaped-cond1_long="" data-blogger-escaped-cond3:="" 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)))

Result

5 days and 10 days moving averages


1377 2014-08-15 1398.0 BUY -> 2014-09-01 1385.0 SELL
1377 2014-10-08 1375.0 SELL -> 2014-10-15 1457.0 BUY
1871 2014-07-14 448.0 SELL -> 2014-07-23 470.0 BUY
1871 2014-07-23 470.0 BUY -> 2014-08-04 453.0 SELL
1906 2015-01-07 155.0 SELL -> 2015-01-08 156.0 BUY
1906 2015-01-08 156.0 BUY -> 2015-01-15 153.0 SELL
1936 2014-08-11 452.0 SELL -> 2014-11-13 459.0 BUY
1992 2014-06-27 147.0 SELL -> 2014-10-23 166.0 BUY
2006 2014-12-25 115.0 SELL -> 2015-01-05 119.0 BUY
2145 2015-01-13 1125.0 SELL -> 2015-01-19 1145.0 BUY
2376 2014-06-27 618.0 SELL -> 2014-07-04 646.0 BUY
2408 2014-11-06 545.0 BUY -> 2014-12-18 523.0 SELL
2481 2014-09-02 410.0 SELL -> 2014-09-08 490.0 BUY
2481 2014-09-08 490.0 BUY -> 2015-01-19 403.0 SELL
2533 2014-07-18 245.0 BUY -> 2014-10-09 238.0 SELL
2538 2014-08-12 45.0 SELL -> 2014-08-20 47.0 BUY
2538 2014-08-20 47.0 BUY -> 2014-12-18 45.0 SELL
2806 2014-10-31 1807.0 SELL -> 2014-11-28 1840.0 BUY
2806 2014-11-28 1840.0 BUY -> 2015-01-05 1823.0 SELL
2806 2015-01-09 1842.0 SELL -> 2015-01-14 1844.0 BUY
3010 2014-06-30 30.0 SELL -> 2014-07-01 31.0 BUY
3050 2014-10-31 739.0 BUY -> 2014-12-17 716.0 SELL
3065 2014-10-06 239.0 BUY -> 2014-12-19 250.0 SELL RIGHT
3172 2014-09-02 833.0 BUY -> 2014-12-10 805.0 SELL
3251 2014-08-12 191.0 SELL -> 2015-01-20 222.0 BUY
3372 2014-06-19 147.0 SELL -> 2014-10-27 157.0 BUY
3372 2014-10-27 157.0 BUY -> 2014-12-11 157.0 SELL
3641 2014-08-07 1971.0 SELL -> 2014-08-13 2175.0 BUY
3663 2014-10-30 506.0 SELL -> 2014-11-07 631.0 BUY
4041 2014-07-23 542.0 SELL -> 2014-07-29 569.0 BUY
4041 2014-07-29 569.0 BUY -> 2014-08-08 529.0 SELL
4206 2014-06-13 2096.0 SELL -> 2014-11-17 2275.0 BUY
4286 2014-10-23 849.0 BUY -> 2015-01-16 812.0 SELL
4344 2014-08-01 677.0 SELL -> 2014-08-11 685.0 BUY
4344 2014-08-11 685.0 BUY -> 2014-08-14 666.0 SELL
4344 2014-08-14 666.0 SELL -> 2014-08-18 715.0 BUY
4409 2014-09-03 289.0 SELL -> 2014-09-17 293.0 BUY
4409 2014-09-17 293.0 BUY -> 2014-10-07 289.0 SELL
4471 2014-08-07 678.0 SELL -> 2014-09-22 688.0 BUY
4582 2014-08-01 246.0 SELL -> 2014-10-23 291.0 BUY
4686 2014-08-04 819.0 SELL -> 2014-08-27 840.0 BUY
4819 2014-08-06 1614.0 SELL -> 2014-08-14 1810.0 BUY
4951 2014-12-17 997.0 SELL -> 2014-12-29 1008.0 BUY
4976 2014-08-14 1580.0 SELL -> 2014-08-26 1620.0 BUY
5310 2014-06-10 2123.0 SELL -> 2014-07-03 2377.0 BUY
5310 2014-07-03 2377.0 BUY -> 2014-09-02 2286.0 SELL
5391 2014-08-11 113.0 SELL -> 2014-08-19 116.0 BUY
5391 2014-08-19 116.0 BUY -> 2014-12-15 112.0 SELL
5398 2014-11-26 133.0 BUY -> 2014-12-19 130.0 SELL
5697 2014-10-28 292.0 SELL -> 2014-11-05 325.0 BUY
6082 2014-10-23 3035.0 BUY -> 2014-11-21 2717.0 SELL
6144 2014-10-23 679.0 SELL -> 2014-12-15 690.0 BUY
6248 2014-07-18 845.0 SELL -> 2014-12-12 1426.0 BUY
6297 2014-07-15 494.0 SELL -> 2014-07-18 551.0 BUY
6407 2014-06-26 943.0 SELL -> 2014-06-30 976.0 BUY
6407 2014-06-30 976.0 BUY -> 2014-07-10 921.0 SELL
6408 2014-10-16 310.0 SELL -> 2014-10-27 316.0 BUY
6408 2014-10-27 316.0 BUY -> 2014-12-24 320.0 SELL RIGHT
6454 2014-07-09 1113.0 SELL -> 2014-12-25 1222.0 BUY
6516 2014-10-24 813.0 BUY -> 2014-12-12 774.0 SELL
6620 2014-07-04 253.0 SELL -> 2014-07-11 330.0 BUY
6643 2014-08-08 275.0 SELL -> 2014-10-27 320.0 BUY
6737 2014-07-04 2750.0 BUY -> 2014-07-22 2679.0 SELL
6891 2014-10-28 86.0 BUY -> 2015-01-19 94.0 SELL RIGHT
7220 2014-09-08 2395.0 BUY -> 2014-12-01 2315.0 SELL
7246 2014-06-30 388.0 SELL -> 2014-07-01 396.0 BUY
7267 2014-12-16 3424.5 SELL -> 2014-12-25 3661.0 BUY
7267 2014-12-25 3661.0 BUY -> 2015-01-06 3453.5 SELL
7296 2014-08-25 1800.0 SELL -> 2014-09-05 1826.0 BUY
7448 2014-08-12 208.0 SELL -> 2014-08-19 210.0 BUY
7490 2014-08-04 851.0 SELL -> 2014-10-28 862.0 BUY
7514 2014-08-18 1076.0 BUY -> 2014-08-27 1052.0 SELL
7606 2014-07-18 3955.0 BUY -> 2014-08-07 3745.0 SELL
7747 2014-06-12 3880.0 SELL -> 2014-06-19 4130.0 BUY
7748 2014-10-02 442.0 SELL -> 2014-10-09 564.0 BUY
7775 2014-10-27 1040.0 BUY -> 2015-01-07 1092.0 SELL RIGHT
7920 2014-11-17 120.0 BUY -> 2014-12-11 117.0 SELL
7923 2014-08-12 419.0 SELL -> 2014-11-20 437.0 BUY
7989 2014-11-06 687.0 BUY -> 2014-12-09 659.0 SELL
8078 2014-09-11 418.0 BUY -> 2014-10-02 400.0 SELL
8103 2014-09-17 466.0 BUY -> 2014-10-02 423.0 SELL
8179 2014-10-29 1733.0 BUY -> 2015-01-05 1669.0 SELL
8274 2014-10-29 282.0 BUY -> 2014-12-12 280.0 SELL
8362 2014-08-07 238.0 SELL -> 2014-08-15 248.0 BUY
4/84 = 4.8%


5 days and 25 days moving averages

1301 2014-05-12 256 SELL -> 2014-06-30 260 BUY
1301 2014-06-30 260 BUY -> 2014-07-01 259 SELL
1301 2014-08-01 258 SELL -> 2014-09-11 254 BUY RIGHT
1301 2014-09-11 254 BUY -> 2014-10-10 251 SELL
1377 2014-03-27 1379.0 BUY -> 2014-05-15 1403.0 SELL RIGHT
...
8343 2014-08-14 286.0 BUY -> 2014-12-15 316.0 SELL RIGHT
8344 2014-07-18 484.0 BUY -> 2014-08-07 480.0 SELL
8346 2014-04-14 313.0 SELL -> 2014-04-30 342.0 BUY
8346 2014-05-26 343.0 BUY -> 2014-10-09 380.0 SELL RIGHT
8356 2014-04-15 335.0 SELL -> 2014-05-02 360.0 BUY
8356 2014-05-02 360.0 BUY -> 2014-05-09 344.0 SELL
8358 2014-07-22 2002.0 BUY -> 2014-08-07 1915.0 SELL
8359 2014-03-04 553.0 SELL -> 2014-03-07 575.0 BUY
8359 2014-03-07 575.0 BUY -> 2014-03-17 524.0 SELL
288/1980 = 14.5%

5 days and 25 days moving averages (20 days before a golden cross were used in cond3)

1301 2014-05-12 256 SELL -> 2014-06-19 260 BUY
1301 2014-06-19 260 BUY -> 2014-07-01 259 SELL
1377 2014-08-06 1382.0 SELL -> 2014-08-15 1398.0 BUY
1377 2014-08-15 1398.0 BUY -> 2014-09-01 1385.0 SELL
1379 2014-08-28 1967.0 SELL -> 2014-09-22 1984.0 BUY
1379 2014-09-22 1984.0 BUY -> 2014-10-03 1953.0 SELL
1380 2014-12-19 437.0 SELL -> 2014-12-24 449.0 BUY
1381 2014-04-28 785.0 SELL -> 2014-05-01 795.0 BUY
1381 2014-05-01 795.0 BUY -> 2014-06-27 785.0 SELL
1382 2014-04-25 1250.0 BUY -> 2014-10-10 1541.0 SELL RIGHT
...
8356 2014-08-13 388.0 BUY -> 2014-10-06 398.0 SELL RIGHT
8359 2014-08-06 616.0 SELL -> 2014-08-18 631.0 BUY
8361 2014-05-12 267.0 SELL -> 2014-05-27 280.0 BUY
8361 2014-05-27 280.0 BUY -> 2014-07-11 279.0 SELL
8362 2014-04-11 235.0 SELL -> 2014-07-30 249.0 BUY
8362 2014-07-30 249.0 BUY -> 2014-08-05 241.0 SELL
8362 2014-08-05 241.0 SELL -> 2014-12-24 269.0 BUY
8362 2014-12-24 269.0 BUY -> 2015-01-07 260.0 SELL
550/3429 = 16.0%



this one seems pretty good. In ccode 8360,
the Fourier transformation seems to work fine. In ccode 1382
the results shows you should buy in April, but as you see, you will not find any sign to buy like skyrocketing or easily found golden cross.
In ccode 8356
seem fine.
In ccode 8343
the results tell you to buy in August and it seems good actually. In ccode 6408 the result with 5 days and 10 days moving averages shows October is good to buy,
it does not seem so good.

コメント

人気の投稿