使用tushare统计投资机构持股明细

以统计社保基金持股明细为例,程序会自动创建一个CSV统计表格文件,保存在c:\2018三季度社保基金持股明细.csv。路径可自己更改。

share_holder.py
#!/usr/bin/python

* coding: gbk *

import tushare as ts
import requests

pro = ts.pro_api(‘5683e168c0c9f0f7d9066cead21854067738e997dcd0e204275f8dd4’)
all_code = pro.stock_basic(exchange=’’, list_status=‘L’, fields=‘ts_code,name’)
ylj_list = []
f = open(r"c:\2018三季度社保基金持股明细.csv", ‘a+’)

for i in range(0, len(all_code)):
# if i == 300:
# break
try:
df_top10float = pro.top10_floatholders(ts_code=str(all_code.ts_code[i]), start_date=‘20180701’,
end_date=‘20180930’)
# print df_top10float
except requests.exceptions.ConnectTimeout:
continue
for j in range(0, len(df_top10float.holder_name)):
if df_top10float.holder_name[j].encode(“gbk”).find(“UBS”) >= 0:
ylj_list.append(str(all_code.ts_code[i]))
print “\n======”, len(ylj_list), str(all_code.ts_code[i]), “========”
# print “++++++++++”, df_top10float.ts_code[j], type(df_top10float.ts_code[j]), “++”
f.write(df_top10float.ts_code[j].encode(“gbk”))
f.write(",")
f.write(all_code.name[i].encode(“gbk”))
f.write(",")
f.write(df_top10float.holder_name[j].encode(“gbk”))
f.write(",")
f.write(str(round(df_top10float.hold_amount[j] / 10000.0, 2)))
f.write(“万股”)
f.write("\n")
else:
print “.”,
if i % 5 == 0:
print “.”

print “.”
print ylj_list
f.close()

你可能感兴趣的:(Python,Python,tushare,tushare,pro)