【原创】用python开发股票自动技术分析的软件(四)#coding=utf-8importtushareastsimporttalibastaimportnumpyasnpimportpandasaspdimportos,time,sys,re,datetimeimportcsvimportscipyimportsmtplibfromemail.mime.textimportMIMETextfromemail.MIMEMultipartimportMIMEMultipart#获取股票列表#code,代码name,名称industry,所属行业area,地区pe,市盈率outstanding,流通股本totals,总股本(万)totalAssets,总资产(万)liquidAssets,流动资产#fixedAssets,固定资产reserved,公积金reservedPerShare,每股公积金eps,每股收益bvps,每股净资pb,市净率timeToMarket,上市日期defGet_Stock_List():df=ts.get_stock_basics()returndf#修改了的函数,按照多个指标进行分析#按照MACD,KDJ等进行分析defGet_TA(df_Code,Dist):operate_array1=[]operate_array2=[]operate_array3=[]count=0forcodeindf_Code.index:#index,0-6date:日期open:开盘价high:最高价close:收盘价low:最低价volume:成交量price_change:价格变动p_change:涨跌幅#7-12ma5:5日均价ma10:10日均价ma20:20日均价v_ma5:5日均量v_ma10:10日均量v_ma20:20日均量df=ts.get_hist_data(code,start='2014-11-20')dflen=df.shape[0]count=count+1ifdflen>35:try:(df,operate1)=Get_MACD(df)(df,operate2)=Get_KDJ(df)(df,operate3)=Get_RSI(df)exceptException,e:Write_Blog(e,Dist)passoperate_array1.append(operate1)#round(df.iat[(dflen-1),16],2)operate_array2.append(operate2)operate_array3.append(operate3)ifcount00==0:Write_Blog(str(count),Dist)df_Code['MACD']=pd.Series(operate_array1,index=df_Code.index)df_Code['KDJ']=pd.Series(operate_array2,index=df_Code.index)df_Code['RSI']=pd.Series(operate_array3,index=df_Code.index)returndf_Code#通过MACD判断买入卖出defGet_MACD(df):#参数12,26,9macd,macdsignal,macdhist=ta.MACD(np.array(df['close']),fastperiod=12,slowperiod=26,signalperiod=9)SignalMA5=ta.MA(macdsignal,timeperiod=5,matype=0)SignalMA10=ta.MA(macdsignal,timeperiod=10,matype=0)SignalMA20=ta.MA(macdsignal,timeperiod=20,matype=0)#13-15DIFFDEADIFF-DEAdf['macd']=pd.Series(macd,index=df.index)#DIFFdf['macdsignal']=pd.Series(macdsignal,index=df.index)#DEAdf['macdhist']=pd.Series(macdhist,index=df.index)#DIFF-DEAdflen=df.shape[0]MAlen=len(SignalMA5)operate=0#2个数组1.DIFF、DEA均为正,DIFF向上突破DEA,买入信号。2.DIFF、DEA均为负,DIFF向下跌破DEA,卖出信号。#待修改ifdf.iat[(dflen-1),13]>0:ifdf.iat[(dflen-1),14]>0:ifdf.iat[(dflen-1),13]>df.iat[(dflen-1),14]anddf.iat[(dflen-2),13]<=df.iat[(dflen-2),14]:operate=operate+10#买入else:ifdf.iat[(dflen-1),14]<0:ifdf.iat[(dflen-1),13]=df.iat[(dflen-2),14]:operate=operate-10#卖出#3.DEA线与K线发生背离,行情反转信号。ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨ifSignalMA5[MAlen-1]<=SignalMA10[MAlen-1]andSignalMA10[MAlen-1]<=SignalMA20[MAlen-1]:#DEA下降operate=operate-1elifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降ifSignalMA5[MAlen-1]>=SignalMA10[MAlen-1]andSignalMA10[MAlen-1]>=SignalMA20[MAlen-1]:#DEA上涨operate=operate+1#4.分析MACD柱状线,由负变正,买入信号。ifdf.iat[(dflen-1),15]>0anddflen>30:foriinrange(1,26):ifdf.iat[(dflen-1-i),15]<=0:#operate=operate+5break#由正变负,卖出信号ifdf.iat[(dflen-1),15]<0anddflen>30:foriinrange(1,26):ifdf.iat[(dflen-1-i),15]>=0:#operate=operate-5breakreturn(df,operate)#通过KDJ判断买入卖出defGet_KDJ(df):#参数9,3,3slowk,slowd=ta.STOCH(np.array(df['high']),np.array(df['low']),np.array(df['close']),fastk_period=9,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)slowkMA5=ta.MA(slowk,timeperiod=5,matype=0)slowkMA10=ta.MA(slowk,timeperiod=10,matype=0)slowkMA20=ta.MA(slowk,timeperiod=20,matype=0)slowdMA5=ta.MA(slowd,timeperiod=5,matype=0)slowdMA10=ta.MA(slowd,timeperiod=10,matype=0)slowdMA20=ta.MA(slowd,timeperiod=20,matype=0)#16-17K,Ddf['slowk']=pd.Series(slowk,index=df.index)#Kdf['slowd']=pd.Series(slowd,index=df.index)#Ddflen=df.shape[0]MAlen=len(slowkMA5)operate=0#1.K线是快速确认线——数值在90以上为超买,数值在10以下为超卖;D大于80时,行情呈现超买现象。D小于20时,行情呈现超卖现象。ifdf.iat[(dflen-1),16]>=90:operate=operate-3elifdf.iat[(dflen-1),16]<=10:operate=operate+3ifdf.iat[(dflen-1),17]>=80:operate=operate-3elifdf.iat[(dflen-1),17]<=20:operate=operate+3#2.上涨趋势中,K值大于D值,K线向上突破D线时,为买进信号。#待修改ifdf.iat[(dflen-1),16]>df.iat[(dflen-1),17]anddf.iat[(dflen-2),16]<=df.iat[(dflen-2),17]:operate=operate+10#下跌趋势中,K小于D,K线向下跌破D线时,为卖出信号。#待修改elifdf.iat[(dflen-1),16]<df.iat[(dflen-1),17]anddf.iat[(dflen-2),16]>=df.iat[(dflen-2),17]:operate=operate-10#3.当随机指标与股价出现背离时,一般为转势的信号。ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨if(slowkMA5[MAlen-1]<=slowkMA10[MAlen-1]andslowkMA10[MAlen-1]<=slowkMA20[MAlen-1])or\(slowdMA5[MAlen-1]<=slowdMA10[MAlen-1]andslowdMA10[MAlen-1]<=slowdMA20[MAlen-1]):#K,D下降operate=operate-1elifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降if(slowkMA5[MAlen-1]>=slowkMA10[MAlen-1]andslowkMA10[MAlen-1]>=slowkMA20[MAlen-1])or\(slowdMA5[MAlen-1]>=slowdMA10[MAlen-1]andslowdMA10[MAlen-1]>=slowdMA20[MAlen-1]):#K,D上涨operate=operate+1return(df,operate)#通过RSI判断买入卖出defGet_RSI(df):#参数14,5slowreal=ta.RSI(np.array(df['close']),timeperiod=14)fastreal=ta.RSI(np.array(df['close']),timeperiod=5)slowrealMA5=ta.MA(slowreal,timeperiod=5,matype=0)slowrealMA10=ta.MA(slowreal,timeperiod=10,matype=0)slowrealMA20=ta.MA(slowrea