在tushare中埋泵,任何數(shù)據(jù)處理的前提都是獲取最新的股票代碼列表磺送,有兩種方式可以獲得:
方法一
使用ts.get_today_all()
返回結(jié)果如下:
,code,name,changepercent,trade,open,high,low,settlement,volume,turnoverratio,amount,per,pb,mktcap,nmc
0,
603999,讀者傳媒,-4.711,7.08,7.4,7.43,7.03,7.43,4208328.0,1.82653,30356269.0,54.046,2.4130000000000003,407808.0,163123.2
1,
603998,方盛制藥,0.0,9.43,0.0,0.0,0.0,9.43,0.0,0.0,0.0,58.938,4.066,406373.09121,400960.50696
2,
改方法的缺點(diǎn)是獲取數(shù)據(jù)的速度比較慢躯泰,實(shí)測(cè)大約10-15秒左右栈戳,根據(jù)用戶的網(wǎng)絡(luò)情況而定厨剪。
方法二
使用ts.get_stock_basics()
耗時(shí)約1-2秒
返回結(jié)果如下:
code,name,industry,area,pe,outstanding,totals,totalAssets,liquidAssets,fixedAssets,reserved,reservedPerShare,esp,bvps,pb,timeToMarket,undp,perundp,rev,profit,gpr,npr,holders
603348,N文燦,汽車配件,廣東,31.12,0.55,2.2,233744.7,80512.95,108315.56,47590.95,2.16,0.706,6.82,3.22,20180426,46519.78,2.11,27.98,0.39,26.63,9.97,51255.0
300216,千山藥機(jī),醫(yī)療保健,湖南,56.22,2.61,3.61,449811.91,224934.97,49833.87,22273.2,0.62,0.12300000000000001,3.36,2.74,20110511,55952.16,1.55,61.48,135.27,49.98,6.51,25120.0
結(jié)論
1.兩種方法對(duì)于系統(tǒng)的開(kāi)銷不同哄酝,視使用場(chǎng)景和需獲取的數(shù)據(jù)類型決定使用何種方法。
2.兩種方法均只能獲取傳統(tǒng)型的股票代碼祷膳,比如600陶衅、000、300等開(kāi)頭的代碼直晨,不支持510搀军、1599開(kāi)頭的ETF基金
3.對(duì)于ETF,我一般將經(jīng)常需要處理的代碼匯總成list抡秆,直接導(dǎo)入系統(tǒng)查詢奕巍,列表如下:['512880','510050','510180','510230','510300','510500','510880','510900','159901','159902','159915','159919','159920','159934','159937','159938','159949','159952']
特別提示:一個(gè)小坑
在list導(dǎo)入時(shí),python會(huì)將代碼認(rèn)定為int類型儒士,所以在查詢諸如000555的代碼時(shí)的止,會(huì)變成ts.get_k_data(555),從而會(huì)報(bào)錯(cuò)着撩,因此list處理時(shí)诅福,需要補(bǔ)0。此處需要使用int型數(shù)據(jù)補(bǔ)零方法
#int型數(shù)據(jù)補(bǔ)零方法
i = "%06d" % i
#文本型數(shù)據(jù)補(bǔ)零方法
i=i.zfill(6)
運(yùn)行代碼
def load_today_all():
'''
[加載當(dāng)日實(shí)時(shí)數(shù)據(jù)]######
從硬盤(pán)中獲取當(dāng)日交易的數(shù)據(jù)拖叙,數(shù)據(jù)由update_today_all提供更新
【行情不含基金和ETF】
讀取目錄在/data/today_all.csv
'''
allcode=[]
#載入代碼
df=pd.read_csv('~/environment/TuShare/data/today_all.csv')
#篩選代碼
df.set_index(['code'], inplace = True)
for i in df.index:
i = "%06d" % i
#i=i.zfill(6)
allcode.append(i)
print(i)
return allcode