由于tushare返回的数据格式是pandas库中的dataFrame,所以有必要研究一下dataFrame这种数据结构,参考http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
>>> df = ts.get_stock_basics()
>>> df.index
Index([u'601698', u'600744', u'300224', u'603648', u'000544', u'300140',
u'603867', u'002463', u'300243', u'603863',
...
u'688003', u'688002', u'688001', u'603983', u'603687', u'603530',
u'603256', u'603236', u'601236', u'600074'],
dtype='object', name=u'code', length=3641)
>>> df.dtypes
name object
industry object
area object
pe float64
outstanding float64
totals float64
totalAssets float64
liquidAssets float64
fixedAssets float64
reserved float64
reservedPerShare float64
esp float64
bvps float64
pb float64
timeToMarket int64
undp float64
perundp float64
rev float64
profit float64
gpr float64
npr float64
holders float64
dtype: object
df = ts.get_hist_data('xxx')
>>> df.columns
Index([u'open', u'high', u'close', u'low', u'volume', u'price_change',
u'p_change', u'ma5', u'ma10', u'ma20', u'v_ma5', u'v_ma10', u'v_ma20'],
dtype='object')
每只股票的振幅=df.price_change/df.open
源码:
#coding=utf-8
import tushare as ts
#获取当日所有股票代码
df = ts.get_stock_basics()
codes = df.index
#对每一只股票计算涨幅,提取出涨幅大于5的全部股票
for i in range(0, len(codes)):
try:
df1 = ts.get_hist_data(codes[i])
filename = './result/2019-06-30.txt'
fp = open(filename, 'a')
val = df1.p_change[0]#price_change[0]/df1.open[0]
if val > 5:
print('%s %s %s' %(codes[i], df.name[i], val))
fp.write('%s %s %s' %(codes[i], df.name[i], val))
fp.close()
except:
pass