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

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

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

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

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

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

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形态分析

例子:低位金叉

原理:

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

如果 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 输出结果

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

将代码封装在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)

上一篇:FL studio 20简易入门教程 -- 第二篇 -- 菜单栏讲解(上)


下一篇:Table 'XXXX' is marked as crashed and should be repaired 解决办法