0
点赞
收藏
分享

微信扫一扫

基于macd、kdj、ma技术指标分析股票多空方向——应用开发4 分析技术指标一系列形态结果

书呆鱼 2022-01-20 阅读 42

接上一节,我们计算获取了技术指标的结果total_df,结果如下图

我们需要显示股票最近10天的分析结果,对此我们只需要截取total_df前12天数据就可以了。

#获取前12天的数据
total_df=total_df.iloc[-12:,:]

total_df 对应列的数字0~9,待会作数据分析时用得上

 我们要初始一个储存分析结果的数据,要包含日期、技术指标结果、收盘价

result_df = pd.DataFrame(columns=['日期','MACD','KDJ','均线','收盘价'])

用for循环遍历把被分析天的数据与其前一天的数据作比较 

for i in range(2,len(total_df)):

把每做完一次分析要把total_df日期与收盘价赋予给result_df对应列 

result_df = pd.DataFrame(columns=['日 期','MACD','KDJ','均线','收盘价'])

for i in range(2,len(total_df)):
    date= toatal_df.index[i].strftime('%Y-%m-%d')
    result_df.loc[i,'日 期'] = date
    result_df.loc[i,'收盘价'] = total_df.iloc[i,9]

基于MACD形态分析

例子:低位金叉

原理:

如果 total_df[ t-1 , 0 ] < total_df[ t-1 , 2 ] & total_df[ t0 , 0 ]>total[ t0 , 2 ] & total_df[ t-1 , 1 ] < 0 & total_df[ t0 , 1 ] > 0 结果为 真(True),result_df[ t0 , MACD ] 赋值为 低位金叉

#MACD形态分析
if total_df.iloc[i-1,0]<total_df.iloc[i-1,2] and total_df.iloc[i,0]>total_df.iloc[i,2] and total_df.iloc[i-1,1]<0 and total_df.iloc[i,1]>0:
    result_df.loc[i,'MACD']='低位金叉'

同理可得MACD其余形态分析:

#MACD形态分析
if total_df.iloc[i-1,0]<total_df.iloc[i-1,2] and total_df.iloc[i,0]>total_df.iloc[i,2] and total_df.iloc[i-1,1]<0 and total_df.iloc[i,1]>0:
    result_df.loc[i,'MACD']='低位金叉'
elif total_df.iloc[i-1,0]<total_df.iloc[i-1,2] and total_df.iloc[i,0]>total_df.iloc[i,2]:
    result_df.loc[i,'MACD']='金叉'
elif total_df.iloc[i-1,0]>0 and total_df.iloc[i-1,2]>0 and total_df.iloc[i,0]>0 and total_df.iloc[i,2]>0 and total_df.iloc[i-1,0]>total_df.iloc[i-1,2] and total_df.iloc[i,0]<total_df.iloc[i,2] and total_df.iloc[i-1,1]>0 and total_df.iloc[i,1]<0:
    result_df.loc[i,'MACD']='高位死叉'
elif total_df.iloc[i-1,0]>total_df.iloc[i-1,2] and total_df.iloc[i,0]<total_df.iloc[i,2]:
    result_df.loc[i,'MACD']='死叉'
elif total_df.iloc[i-1,0]<0 and total_df.iloc[i,0]>0:
    result_df.loc[i,'MACD']='DIF上穿0轴'
else:
    result_df.loc[i,'MACD']='中性'

基于KDJ形态分析

#KDJ形态分析
if total_df.iloc[i-1,3]<total_df.iloc[i-1,4] and total_df.iloc[i,3]>total_df.iloc[i,4] and total_df.iloc[i,3]<20 and total_df.iloc[i,4]<20 and total_df.iloc[i,5]<20:
    result_df.loc[i,'KDJ']='低位金叉'
elif total_df.iloc[i-1,3]<total_df.iloc[i-1,4] and total_df.iloc[i,3]>total_df.iloc[i,4]:
    result_df.loc[i,'KDJ']='金叉'
elif total_df.iloc[i-1,3]>total_df.iloc[i-1,4] and total_df.iloc[i,3]<total_df.iloc[i,4] and total_df.iloc[i,3]>50 and total_df.iloc[i,4]>50 and total_df.iloc[i,5]>50:
    result_df.loc[i,'KDJ']='高位死叉'
elif total_df.iloc[i-1,3]>total_df.iloc[i-1,4] and total_df.iloc[i,3]<total_df.iloc[i,4]:
    result_df.loc[i,'KDJ']='死叉'
elif total_df.iloc[i-1,5]<0 and total_df.iloc[i,5]>0:
    result_df.loc[i,'KDJ']='J线上穿0轴'
elif total_df.iloc[i-1,5]>90 and total_df.iloc[i,5]>90 and total_df.iloc[i-1,5]>total_df.iloc[i,5]:
    result_df.loc[i,'KDJ']='适当减仓'
elif total_df.iloc[i-1,5]<20 and total_df.iloc[i,5]<20 and total_df.iloc[i-1,5]<total_df.iloc[i,5]:
    result_df.loc[i,'KDJ']='适当关注'
else:
    result_df.loc[i,'KDJ']='中性'

基于均线形态分析

#定义判断均线多种形态函数
if total_df.iloc[i-1,6]<total_df.iloc[i-1,7] and total_df.iloc[i,6]>total_df.iloc[i,7]:
    result_df.loc[i,'均线']='5交10金叉'
elif total_df.iloc[i-1,6]<total_df.iloc[i-1,8] and total_df.iloc[i,6]>total_df.iloc[i,8]:
    result_df.loc[i,'均线']='5交20金叉'
elif total_df.iloc[i-1,6]>total_df.iloc[i-1,7] and total_df.iloc[i,6]<total_df.iloc[i,7]:
    result_df.loc[i,'均线']='5交10死叉'
elif total_df.iloc[i-1,6]>total_df.iloc[i-1,8] and total_df.iloc[i,6]<total_df.iloc[i,8]:
    result_df.loc[i,'均线']='5交20死叉'
elif total_df.iloc[i-2,6]<total_df.iloc[i-1,6] and total_df.iloc[i-1,6]>total_df.iloc[i,6]:
    result_df.loc[i,'均线']='5天线向下拐'
elif total_df.iloc[i-2,6]>total_df.iloc[i-1,6] and total_df.iloc[i-1,6]<total_df.iloc[i,6]:
    result_df.loc[i,'均线']='5天线向上拐'
elif total_df.iloc[i,9]>total_df.iloc[i,6]:
    result_df.loc[i,'均线']='5天线上'
elif total_df.iloc[i,9]<total_df.iloc[i,6]:
    result_df.loc[i,'均线']='5天线下'

最后result_df 输出结果

 

举报

相关推荐

0 条评论