ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-20 21:33:53  阅读:219  来源: 互联网

标签:loc elif ma kdj df result iloc total 技术指标


接上一节,我们计算获取了技术指标的结果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 输出结果

将代码封装在get_analyse函数里,实现result_df=get_analyse(total_df)

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

    for i in range(2,len(total_df)):
        date= total_df.index[i].strftime('%Y-%m-%d')
        result_df.loc[i,'日 期'] = date
        result_df.loc[i,'收盘价'] = total_df.iloc[i,9]
        #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形态分析
        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天线下'
    return result_df

完整代码

import tushare as ts
import pandas as pd
import datetime
import pandas_ta as ta

token='你的token'
ts.set_token(token)
pro=ts.pro_api()

def get_stock(num):
    stocknum=num
    today = datetime.datetime.today()
    startday=today+datetime.timedelta(days=-365)
    today = today.strftime('%Y%m%d')
    startday =startday.strftime('%Y%m%d')

    stock_df= pro.daily(ts_code=stocknum, start_date=startday,end_date=today)

    stock_df['trade_date'] = pd.to_datetime(stock_df['trade_date'])
    stock_df.set_index('trade_date',inplace=True)
    stock_df=stock_df.rename(columns={'vol':'volume'})
    stock_df=stock_df.iloc[::-1]
    return stock_df

def get_technical(stock_df):
    #MACD
    macd_df = ta.macd(stock_df['close'])
    #KDJ
    kdj_df = ta.kdj(stock_df['high'],stock_df['low'],stock_df['close'])
    #均线 5、10、20天
    ma5_df = pd.DataFrame(ta.sma(stock_df['close'],length=5))
    ma10_df = pd.DataFrame(ta.sma(stock_df['close'],length=10))
    ma20_df = pd.DataFrame(ta.sma(stock_df['close'],length=20))

    #连接所有技术指标结果与收盘价以列形式在一个DataFrame
    total_df = pd.concat([macd_df,kdj_df,ma5_df,ma10_df,ma20_df,stock_df['close']],axis=1)
    #获取前12天的数据
    total_df=total_df.iloc[-12:,:]
    return total_df

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

    for i in range(2,len(total_df)):
        date= total_df.index[i].strftime('%Y-%m-%d')
        result_df.loc[i,'日 期'] = date
        result_df.loc[i,'收盘价'] = total_df.iloc[i,9]
        #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形态分析
        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天线下'
    return result_df


get_stocknum =pd.read_excel('股票代码.xlsx')
stock_df = get_stock(get_stocknum.iloc[0,0])
total_df=get_technical(stock_df)
result_df=get_analyse(total_df)

标签:loc,elif,ma,kdj,df,result,iloc,total,技术指标
来源: https://blog.csdn.net/m0_64902855/article/details/122606899

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有