克隆策略
In [1]:
def cal_MACD(df,short=12,lon=26,dea=9):
    df["EMA_short"]=df['real_price'].ewm(span=short,adjust=False).mean()
    df["EMA_long"]=df['real_price'].ewm(span=lon,adjust=False).mean()
    df['DIF']=df["EMA_short"]-df["EMA_long"]
    df['DEA']= df['DIF'].ewm(span=dea,adjust=False).mean()
    df['MACD']=(df['DIF']-df['DEA'])*2
    del df["EMA_short"],df["EMA_long"]
    return df
sdate="1900-01-15"
edate="2021-11-23"
#日线数据
df1 = DataSource("bar1d_CN_STOCK_A").read(start_date=sdate, end_date=edate,fields=["date","instrument","close","adjust_factor","turn","amount"])
df1["real_price"]=df1["close"]/df1["adjust_factor"]
df2=df1[df1.instrument=="000002.SZA"]
#print(df2)
df3=df2.reset_index(drop = True)
#print(df3)
df=cal_MACD(df3,short=12,lon=26,dea=9)
#print(df)
#print(df.items)
#寻找MACD金叉和死叉
datenumber = int(df.shape[0])
print(datenumber)
for i in range(datenumber-1):
    if (df.loc[i,"DIF"]<=df.loc[i,"DEA"] and df.loc[i+1,"DIF"]>=df.loc[i+1,"DEA"] and df.loc[i+1,"DEA"]!=0 and df.loc[i+1,"DIF"]!=0 ):
        #print("MACD金叉的日期:"+str(df.index[i+1]))
        df.loc[i,"SIGNAL"]=1
    if ((df.loc[i,"DIF"]>=df.loc[i,"DEA"]) and (df.loc[i+1,"DIF"]<=df.loc[i+1,"DEA"]) and df.loc[i+1,"DEA"]!=0 and df.loc[i+1,"DIF"]!=0):
        #print("MACD死叉的日期:"+str(df.index[i+1]))
        df.loc[i,"SIGNAL"]=0
print(df)
4104
           date        amount  instrument      turn  adjust_factor  \
0    2005-01-04  5.451423e+07  000002.SZA  0.656615      28.624964   
1    2005-01-05  9.478190e+07  000002.SZA  1.115507      28.624964   
2    2005-01-06  9.925340e+07  000002.SZA  1.168254      28.624964   
3    2005-01-07  9.756404e+07  000002.SZA  1.123904      28.624964   
4    2005-01-10  3.883998e+07  000002.SZA  0.449643      28.624964   
...         ...           ...         ...       ...            ...   
4099 2021-11-17  1.205064e+09  000002.SZA  0.641879     162.737427   
4100 2021-11-18  1.168011e+09  000002.SZA  0.627726     162.737427   
4101 2021-11-19  2.570653e+09  000002.SZA  1.346319     162.737427   
4102 2021-11-22  1.697702e+09  000002.SZA  0.903092     162.737427   
4103 2021-11-23  1.083646e+09  000002.SZA  0.579812     162.737427   

            close  real_price       DIF       DEA      MACD  SIGNAL  
0      150.853561    5.270000  0.000000  0.000000  0.000000     1.0  
1      156.292297    5.460000  0.015157  0.003031  0.024251     NaN  
2      155.433548    5.430000  0.024466  0.007318  0.034295     NaN  
3      156.864792    5.480000  0.035469  0.012948  0.045041     NaN  
4      156.006042    5.450000  0.041292  0.018617  0.045350     NaN  
...           ...         ...       ...       ...       ...     ...  
4099  3166.870361   19.460001 -0.263212 -0.437891  0.349358     NaN  
4100  3106.657471   19.090000 -0.255231 -0.401359  0.292256     NaN  
4101  3238.474854   19.900000 -0.181455 -0.357378  0.351848     NaN  
4102  3127.813232   19.219999 -0.175829 -0.321069  0.290478     NaN  
4103  3131.068115   19.240000 -0.167823 -0.290419  0.245193     NaN  

[4104 rows x 11 columns]