Verification of Golden cross and Death cross 5 long moving average's up and down ゴールデンクロスとデッドクロスの検証5 長期移動平均の増減

Verification of Golden cross and Death cross5 ゴールデンクロスとデッドクロスの検証5

I looked for a golden cross with a long average goes up in the last 3 days and the nearest death cross with a long average falls down in the last 3 days. To avoid a cross caused by a fluctuation, I watched a long term average's up and down.

ゴールデンクロスの直近3日間で長期移動平均が増加すること、デッドクロスの直近三日間で長期移動平均が減少することを条件にして、隣接する2クロスの終値を比較して利益が出るか調べた。


Source Code

stocks.db is a database file which contains brand, financial and stocks information about each company.

# -*- 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') # which includes
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
            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-cond2_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 and not cond2_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)))


ccc

Results

5 days and 25 days moving averages

1301 2014-05-12 256 SELL -> 2014-06-19 260 BUY
1301 2014-06-19 260 BUY -> 2014-07-07 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
1301 2014-10-10 251 SELL -> 2014-10-30 257 BUY
1332 2014-09-11 293.0 SELL -> 2014-10-09 313.0 BUY
1333 2014-05-30 1606.0 BUY -> 2014-07-11 1603.0 SELL
1352 2014-05-16 125.0 SELL -> 2014-06-03 131.0 BUY
1352 2014-06-03 131.0 BUY -> 2014-12-10 128.0 SELL
1376 2014-05-30 882.0 SELL -> 2014-09-29 982.0 BUY
1377 2014-03-27 1379.0 BUY -> 2014-05-15 1403.0 SELL RIGHT
1377 2014-07-11 1364.0 SELL -> 2014-07-17 1396.0 BUY
...
8359 2014-03-04 553.0 SELL -> 2014-03-28 585.0 BUY
8359 2014-03-28 585.0 BUY -> 2014-04-14 543.0 SELL
8359 2014-04-14 543.0 SELL -> 2014-07-18 624.0 BUY
8359 2014-07-18 624.0 BUY -> 2014-08-06 616.0 SELL
8359 2014-08-06 616.0 SELL -> 2014-08-18 631.0 BUY
8360 2014-05-26 451.0 BUY -> 2014-12-12 479.0 SELL RIGHT
8361 2014-03-26 278.0 BUY -> 2014-04-14 263.0 SELL
8361 2014-05-12 267.0 SELL -> 2014-05-27 280.0 BUY
8362 2014-03-28 250.0 BUY -> 2014-04-11 235.0 SELL
8362 2014-04-11 235.0 SELL -> 2014-07-30 249.0 BUY
8362 2014-07-30 249.0 BUY -> 2015-01-07 260.0 SELL RIGHT
766/5835 = 13.1%

5 days and 100 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
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
1383 2014-11-19 1533.0 BUY -> 2014-12-15 1444.0 SELL
1413 2014-11-06 1378.0 BUY -> 2014-11-14 1366.0 SELL
1418 2014-06-09 199.0 SELL -> 2014-12-01 267.0 BUY
1514 2014-08-06 117.0 SELL -> 2014-08-21 122.0 BUY
1514 2014-08-21 122.0 BUY -> 2014-12-15 119.0 SELL
...
8350 2014-09-24 211.0 SELL -> 2014-11-06 213.0 BUY
8355 2014-11-05 1171.0 BUY -> 2014-12-16 1055.0 SELL
8362 2014-07-28 248.0 BUY -> 2014-08-07 238.0 SELL
8362 2014-08-07 238.0 SELL -> 2014-08-15 248.0 BUY
14/958 = 1.5%



Discussion

still bad.

コメント