Python 閫氳繃 TALib 鍖呮瀯寤鸿偂绁ㄨ嚜鍔ㄦ妧鏈垎鏋�

#coding = utf-8

importtushareasts

importtalib

importpandasaspd

importnumpyasnp

fromdatetimeimportdatetime

importsys

# code:浠g爜 name:鍚嶅瓧锛宨ndustry:鎵�灞炶涓� 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鏃ュ潎閲弙_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銆丏EA鍧囦负姝o紝DIF鍚戜笂绌胯繃DEA

#聽 聽 聽 聽 2.DIF銆丏EA鍧囦负璐燂紝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绾胯秼鍔垮悜涓婏紝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绾垮悜涓婄┛杩嘍绾匡紝榛勯噾浜ゅ弶锛屽皢杩涘叆澶氬ご甯傚満锛岃偂浠峰皢涓婃定锛屽簲璇ヤ拱杩�

ifdf.iat[(dflen-1),16] > df.iat[(dflen-1),17]anddf.iat[(dflen-2),16] <= df.iat[(dflen-2),17]:

operator = operator +10

#涓嬮檷瓒嬪娍涓紝K绾垮悜涓嬬┛杩嘍绾匡紝姝讳骸浜ゅ弶锛屽皢杩涘叆绌哄ご甯傚満锛岃偂浠峰皢涓嬮檷锛屽簲璇ュ崠鍑�

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 鎱㈤�焤eal锛屽揩閫焤eal

df['slowreal']=pd.Series(slowreal,index=df.index)#鎱㈤�焤eal 18

df['fastreal']=pd.Series(fastreal,index=df.index)#蹇�焤eal 19

dflen = df.shape[0]

MAlen =len(slowrealMA5)

operate =0

#RSI>80涓鸿秴涔板尯锛孯SI<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)

你可能感兴趣的:(Python 閫氳繃 TALib 鍖呮瀯寤鸿偂绁ㄨ嚜鍔ㄦ妧鏈垎鏋�)