[PYTHON] [Data analysis for 5 years] Is the stock price rising a few days after the Golden Cross?


Synopsis
How useful is the basic "golden cross" of technical analysis in practice? We verified TOPIX500 stocks using data for 5 years.


Conclusion
・ ** Do not judge the buying timing by the Golden Cross alone **

Golden cross

Golden Cross: A phenomenon in which the short-term moving average breaks through the long-term moving average from bottom to top. ⇒ ** Signal to enter the rising period / Buy signal / Buy sign ** I'm sure anyone interested in technical analysis has heard of it. I think it is no exaggeration to say that it is the first technical analysis to be learned.

Although it is such a golden cross, it has a big drawback. "Be careful of deception" "It cannot be said that it will rise steadily" "It is a tendency and not always" Many of the articles that introduce Golden Cross are insured. If I say that, I don't think I should refer to the Golden Cross after all.

Therefore, I would like to use past data to verify ** whether the stock price is rising a few days after the Golden Cross **.

Degree of reliability

Golden Cross is said to have ** reliability **. Intuitively, the larger the angle of intersection, the more likely it is to rise.

▼ Golden Cross reliability goldcross.png

I would also like to verify how reliable this reliability is.

Method of verification

The general flow is to find the timing of the golden cross from the data for 5 years for TOPIX 500 stocks, and investigate whether the stock price is rising a few days later. In addition, golden crosses are classified and evaluated according to reliability based on the direction and angle at the time of intersection.

I will list the verification parameters once, but I will explain them later using an example.

[Verification period] Approximately 5 years from January 1, 2015 to January 8, 2020

[Target brand] TOPIX 500 brands

[Moving average period]

  1. Short term: 5 days, long term: 25 days
  2. Short term: 5 days, long term: 50 days
  3. Short term: 5 days, long term: 75 days
  4. Short term: 25 days, long term: 50 days
  5. Short term: 25 days, long term: 75 days

[Day to confirm the rise in stock price]

  1. 1 day later 2.3 days later 3.5 days later
  2. 10 days later

【conditions】

  1. None
  2. Direction: Short-term line is upward, long-term line is downward
  3. Direction: Short-term line upwards, long-term line upwards
  4. Angle: 30 degrees or more
  5. Angle: 45 degrees or more
  6. Angle: 60 degrees or more
  7. Direction: Short-term line upwards, long-term line upwards, and angle: 60 degrees or more

Example

Check if the stock price is rising a few days after the Golden Cross as shown below. The figure is exaggerated to see the intersection, but in reality the distance between the intersection and the date is much narrower. ▼ Overview of verification method SnapCrab_NoName_2021-1-15_1-9-52_No-00.png

Next is how to judge the reliability. Add a condition to the golden cross to verify how reliable the confidence is. Use the method shown in the figure below to determine the direction/angle that will be the criterion for determining the conditions. ▼ How to judge reliability SnapCrab_NoName_2021-1-15_1-9-37_No-00.png

The case of Toyota Motor Corporation is described as an example. The green circle indicates the timing of the golden cross. Golden Cross: A phenomenon in which the short-term moving average breaks through the long-term moving average from bottom to top. ⇒ ** The blue line breaks through the yellow line from bottom to top ** ▼ Short term: 25-day moving average, long term: 75-day moving average toyota.png

Next, we will conditionally select the golden cross to verify the reliability. The figure below shows only the golden cross with ** angle: 45 degrees or more . ▼ Select Golden Cross from the intersection conditions ( Angle: 45 degrees or more **) toyota2.png

Check if it has risen 〇 days after the last selected timing.

Using the above method, we verified the data for 5 years for TOPIX500 stocks.

inspection result

Here are some of the results. Other results are summarized at the end of the article. As mentioned above, the data used is 5 years for TOPIX500 stocks.

[Moving average period]

  1. Short term: 5 days, long term: 25 days
  2. Short term: 25 days, long term: 75 days

[Day to confirm the rise in stock price]

  1. 1 day later 2.3 days later 3.5 days later
  2. 10 days later

【conditions】

  1. None
  2. Direction: Short-term line upwards, long-term line upwards
  3. Angle: 45 degrees or more

Immediately, I have summarized the results in a table.
Result
▼ Short term: 5 days, long term: 25 days result_s5_l25.png ▼ Short term: 25 days, long term: 75 days result_s25_l75.png

As shown in the table, in each case, the probability of increase after ** 〇 days is about 50% **. It is about 50% with or without conditions. It turns out that neither the golden cross nor the reliability is completely meaningful. As mentioned at the end of the article, the same result can be obtained by changing the short-term and long-term period/reliability conditions.

** What does Golden Cross mean? ... **

Summary

When we examined the most famous golden cross as a ** buy sign , we found that neither the golden cross nor the reliability was meaningful. Since the moving average is a line connecting the average prices over a period of time, the movement of the moving average lags behind the movement of the price. Therefore, it is considered that the timing to buy Golden Cross is too late. We also know that many wise investors who own the stock before the Golden Cross think " It's a buy sign because the Golden Cross came out **". Therefore, a wise investor thinks that "Golden Cross is a sell sign" and sells the stocks that have been prepared. It is recommended that ** Golden Cross alone does not determine when to buy ** so that it does not become that nutrient.

next

It became clear that a simple moving average golden cross was too late to buy. Therefore, I would like to examine what happens with MACD, which uses an exponential moving average with less delay.
Past articles
Verify the investment efficiency of all Nikkei 225 stocksI want to find a stock that will rise the day after the Nikkei Stock Average risesWhich crypto assets can be earned efficientlyInvest in stocks with large price fluctuations of 1,000 yen or less per share to improve performanceInvestigating the top 10 stocks raised at the time of the 1st State of Emergency

List of results

List of verification conditions and results [Verification period] Approximately 5 years from January 1, 2015 to January 8, 2020

[Target stocks] TOPIX 500 brands

[Moving average period]

  1. Short term: 5 days, long term: 25 days
  2. Short term: 5 days, long term: 50 days
  3. Short term: 5 days, long term: 75 days
  4. Short term: 25 days, long term: 50 days
  5. Short term: 25 days, long term: 75 days

[Day to confirm the rise in stock price]

  1. 1 day later 2.3 days later 3.5 days later
  2. 10 days later

【conditions】

  1. None
  2. Direction: Short-term line is upward, long-term line is downward
  3. Direction: Short-term line upwards, long-term line upwards
  4. Angle: 30 degrees or more
  5. Angle: 45 degrees or more
  6. Angle: 60 degrees or more
  7. Direction: Short-term line upwards, long-term line upwards, and angle: 60 degrees or more

▼ Conditions 1 ~ 4 result_1-4.png ▼ Conditions 5 ~ 7 result5-7.png

Verification program

python


#A collection of self-made functions for stock price analysis
from trade_module import trade_module as tm
import pandas as pd
import numpy as np

#Reading the brand code
stock = tm.get_topix500()

##result Create data frame
day = [1,3,5,10]    #Set 〇 to ​​see if it is rising after 〇 days
col=["g_p_count"]   #Total number of golden crosses
period = [[5,25],[5,50],[5,75],[25,50],[25,75]] #Moving average period
for d in day:
    col.append("roc_d"+str(d)+"_plus")
result = pd.DataFrame(data=0,index=range(len(period)),columns=col)

for p in range(len(period)):
    for code in stock.code:
        print(code)
        #Read the prepared stock price data
        data = pd.read_csv("./data/"+code+".csv",index_col=0, parse_dates=True)
        
        #Calculate simple moving average
        tm.add_sma_tmp(data, period=(period[p][0],50,period[p][1]))
        data["g_point"] = False
        
        #Get the timing of the golden cross
        for i in range(len(data.index)-1):
            if(data.sma_s[i]<data.sma_l[i] and data.sma_s[i+1]>data.sma_l[i+1]):
                data["g_point"].iat[i+1] = True
               
        #Tilt at the time of golden cross/Calculate the angle
        data["sma_s_line"] = np.nan
        data["sma_l_line"] = np.nan
        data["sma_s_slop"] = np.nan
        data["sma_l_slop"] = np.nan
        data["sma_deg"] = np.nan
        for g in data.index[data.g_point]:
             #Calculate a straight line between two points
             a1 = data.sma_s[g] - data.sma_s[len(data.Close[:g])-1-1]
             b1 = data.sma_s[g] - a1*len(data.Close[:g])
             data["sma_s_slop"].at[g]=a1 
             a2 = data.sma_l[g] - data.sma_l[len(data.Close[:g])-1-1]
             b2 = data.sma_l[g] - a2*len(data.Close[:g])
             data["sma_l_slop"].at[g]=a2
             data["sma_deg"].at[g] = np.rad2deg(np.arctan(abs((a1-a2)/(1+a1*a2))))
             #Add straight line data for plot
             for i in range(11):
                 x = len(data.Close[:g])-1+i-5
                 if(x<=len(data.Close)-1):
                    data["sma_s_line"].iat[x]= a1*x+b1
                    data["sma_l_line"].iat[x]= a2*x+b2
                
        #Select Golden Cross
        for g in data.index[data.g_point]:
            #Angle condition setting
            if(data["sma_deg"].at[g] < 60):
                 data["g_point"].at[g] = False
            
            #Orientation (tilt) condition setting
            if(not(data["sma_s_slop"].at[g]>0 and data["sma_l_slop"].at[g]>0)):
                  data["g_point"].at[g] = False
        
        #〇 Check if it is rising after a day
        for g in data.index[data.g_point]:
            for d in day:
                if(len(data.Close[:g])+d<=len(data.Close)):
                    data.at[g,"roc_d"+str(d)] = (data.Close[len(data.Close[:g])+d-1]-data.Close[g])/data.Close[g]*100
                else:
                    data.at[g,"roc_d"+str(d)] = (data.Close[-1]-data.Close[g])/data.Close[g]*100
                if(data.at[g,"roc_d"+str(d)]>0):
                    data.at[g,"roc_d"+str(d)+"_point"] = 1
                else:
                    data.at[g,"roc_d"+str(d)+"_point"] = 0
        
        #If it is rising, add the counter
        data.dropna(inplace=True)
        if(len(data.index[data.g_point])>0):
            result["g_p_count"].iat[p] += len(data.index)
            for d in day:
                result["roc_d"+str(d)+"_plus"].iat[p]+=sum(data["roc_d"+str(d)+"_point"])

print(result)

Recommended Posts

[Data analysis for 5 years] Is the stock price rising a few days after the Golden Cross?
Check for a few more days until the certificate expires
A Python beginner first tried a quick and easy analysis of weather data for the last 10 years.
Data Science Virtual Machines is the best environment for data analysis from now on!
Prepare a programming language environment for data analysis