#coding = utf-8
importtushareasts
importtalib
importpandasaspd
importnumpyasnp
fromdatetimeimportdatetime
importsys
# code:代码 name:名字,industry:所属行业 area:地区 pe:市盈率 outstanding:流通股本 totals:总股本(万) totalAssets:总资产(万)
# liqidAssets:流动资产 fixedAssets:固定资产 reserved:公积金 bvps:每股收益 pd:每股净资 timeToMarket:上市日期
defget_stock_list():
df = ts.get_stock_basics()
returndf
defget_ta(df_code,Dist):
operate_array1 = []
operate_array2 = []
operate_array3 = []
count =0
forcodeindf_code.index:
# index,0 - 6 date:日期 open:开盘价 high:最高价 close:收盘价 low:最低价 volume:成交量 price_change:价格变动 p_change:涨跌幅
# 7-12 ma5: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 +1
ifdflen >35:
(df,operate1) = get_macd(df)
(df,operate2) = get_KDJ(df)
(df,operate3) = Get_RSI(df)
operate_array1.append(operate1)#round(df.iat[(dflen-1),16],2)
operate_array2.append(operate2)
operate_array3.append(operate3)
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,9
macd,macdsignal,macdhist = talib.MACD(df['close'].values,fastperiod=12,slowperiod=26,signalperiod=9)
signal_ma5 = talib.MA(macdsignal,timeperiod=5,matype=0)
signal_ma10 = talib.MA(macdsignal,timeperiod=10,matype=0)
signal_ma20 = talib.MA(macdsignal,timeperiod=20,matype=0)
#13-15 DIF DEA DIF-DEA
df['macd'] = pd.Series(macd,index=df.index)#DIF
df['signal'] = pd.Series(macdsignal,index=df.index)#DEA
df['macdhist'] = pd.Series(macdhist,index=df.index)#DIF-DEA
dflen = df.shape[0]
MAlen =len(signal_ma5)
operator =0
#俩个数组 1.DIF、DEA均为正,DIF向上穿过DEA
# 2.DIF、DEA均为负,DIF向下穿过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]:
operator = operator+10#买进
else:
ifdf.iat[(dflen-1),14] <0:
ifdf.iat[(dflen-1),13] == df.iat[(dflen-2),14]:
operator = operator -10#卖出
#DEA与K线发生背离 K线趋势向上,MACD向下,顶背离,将要下降;K线趋势向下,MACD向上,底背离,将要上升
ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨
ifsignal_ma5[MAlen-1]<=signal_ma10[MAlen-1]andsignal_ma10[MAlen-1]<=signal_ma20[MAlen-1]:#DEA下降
operator = operator-1
ifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降
ifsignal_ma5[MAlen-1]>=signal_ma10[MAlen-1]andsignal_ma10[MAlen-1]>=signal_ma20[MAlen-1]:#DEA上升
operator = operator+1
#分析MACD柱状图 由负变正将上涨
ifdf.iat[(dflen-1),15] >0anddflen >30:
foriinrange(1,26):
ifdf.iat[(dflen-1-i),15] <=0:
operator = operator +5
break
#由正变负 将降低
ifdf.iat[(dflen-1),15] <0anddflen >30:
foriinrange(1,26):
ifdf.iat[(dflen-1-i),15] >=0:
operator = operator -5
break
returndf,operator
#通过KDJ判断买进和卖出
defget_KDJ(df):
#参数9,3,3
slowk,slowd = talib.STOCH(df['high'].values,df['low'].values,df['close'].values,fastk_period=9,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)
slowkMA5 = talib.MA(slowk,timeperiod=5,matype=0)
slowkMA10 = talib.MA(slowk,timeperiod=10,matype=0)
slowkMA20 = talib.MA(slowk,timeperiod=20,matype=0)
slowdMA5 = talib.MA(slowd,timeperiod=5,matype=0)
slowdMA10 = talib.MA(slowd,timeperiod=10,matype=0)
slowdMA20 = talib.MA(slowd,timeperiod=20,matype=0)
#16,17 K,D
df['slowk'] = pd.Series(slowk,index=df.index)#K
df['slowd'] = pd.Series(slowd,index=df.index)#D
dflen = df.shape[0]
MAlen =len(slowdMA5)
operator =0
#1.K线是快速确认线 -- 数值在90以上为超买信号,数值在10以下为超卖信号;2.D大于80为超卖状态,小于20为超卖状态
ifdf.iat[(dflen-1),16] >=90:
operator = operator -3
ifdf.iat[(dflen-1),16] <=10:
operator = operator +3
ifdf.iat[(dflen-1),17] >=80:
operator = operator -3
ifdf.iat[(dflen-1),17] <=20:
operator = operator +3
#上涨趋势中,K线向上穿过D线,黄金交叉,将进入多头市场,股价将上涨,应该买进
ifdf.iat[(dflen-1),16] > df.iat[(dflen-1),17]anddf.iat[(dflen-2),16] <= df.iat[(dflen-2),17]:
operator = operator +10
#下降趋势中,K线向下穿过D线,死亡交叉,将进入空头市场,股价将下降,应该卖出
ifdf.iat[(dflen-1),16] < df.iat[(dflen-1),17]anddf.iat[(dflen-2),16] >= df.iat[(dflen-2),17]:
operator = operator -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下降
operator = operator -1
elifdf.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上涨
operator = operator +1
return(df,operator)
#通过RSI判断买入卖出
defGet_RSI(df):
#参数14,5
slowreal = talib.RSI(np.array(df['close']),timeperiod=14)
fastreal = talib.RSI(np.array(df['close']),timeperiod=5)
slowrealMA5 = talib.MA(slowreal,timeperiod=5,matype=0)
slowrealMA10 = talib.MA(slowreal,timeperiod=10,matype=0)
slowrealMA20 = talib.MA(slowreal,timeperiod=20,matype=0)
fastrealMA5 = talib.MA(fastreal,timeperiod=5,matype=0)
fastrealMA10 = talib.MA(fastreal,timeperiod=10,matype=0)
fastrealMA20 = talib.MA(fastreal,timeperiod=20,matype=0)
#18-19 慢速real,快速real
df['slowreal']=pd.Series(slowreal,index=df.index)#慢速real 18
df['fastreal']=pd.Series(fastreal,index=df.index)#快速real 19
dflen = df.shape[0]
MAlen =len(slowrealMA5)
operate =0
#RSI>80为超买区,RSI<20为超卖区
ifdf.iat[(dflen-1),18]>80ordf.iat[(dflen-1),19]>80:
operate = operate -2
elifdf.iat[(dflen-1),18]<20ordf.iat[(dflen-1),19]<20:
operate = operate +2
#RSI上穿50分界线为买入信号,下破50分界线为卖出信号
if(df.iat[(dflen-2),18]<=50anddf.iat[(dflen-1),18]>50)or(df.iat[(dflen-2),19]<=50anddf.iat[(dflen-1),19]>50):
operate = operate +4
elif(df.iat[(dflen-2),18]>=50anddf.iat[(dflen-1),18]<50)or(df.iat[(dflen-2),19]>=50anddf.iat[(dflen-1),19]<50):
operate = operate -4
#RSI掉头向下为卖出讯号,RSI掉头向上为买入信号
ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨
if(slowrealMA5[MAlen-1]<=slowrealMA10[MAlen-1]andslowrealMA10[MAlen-1]<=slowrealMA20[MAlen-1])or\
(fastrealMA5[MAlen-1]<=fastrealMA10[MAlen-1]andfastrealMA10[MAlen-1]<=fastrealMA20[MAlen-1]):#RSI下降
operate = operate -1
elifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降
if(slowrealMA5[MAlen-1]>=slowrealMA10[MAlen-1]andslowrealMA10[MAlen-1]>=slowrealMA20[MAlen-1])or\
(fastrealMA5[MAlen-1]>=fastrealMA10[MAlen-1]andfastrealMA10[MAlen-1]>=fastrealMA20[MAlen-1]):#RSI上涨
operate = operate +1
#慢速线与快速线比较观察,若两线同向上,升势较强;若两线同向下,跌势较强;若快速线上穿慢速线为买入信号;若快速线下穿慢速线为卖出信号
ifdf.iat[(dflen-1),19]> df.iat[(dflen-1),18]anddf.iat[(dflen-2),19]<=df.iat[(dflen-2),18]:
operate = operate +10
elifdf.iat[(dflen-1),19]< df.iat[(dflen-1),18]anddf.iat[(dflen-2),19]>=df.iat[(dflen-2),18]:
operate = operate -10
return(df,operate)
defOutput_Csv(df,Dist):
TODAY = datetime.date.today()
CURRENTDAY=TODAY.strftime('%Y-%m-%d')
# reload(sys)
sys.setdefaultencoding("gbk")
df.to_csv(Dist+CURRENTDAY+'stock.csv',encoding='gbk')#选择保存
df = get_stock_list()
Dist ='data/TEST'
df = get_ta(df,Dist)
Output_Csv(df,Dist)