根据大家的要求,及时更新了AQF编程语言Python丨金融数据获取之tushare (下)~历史阅读:金融数据获取之tushare(上)~

2 投资参考数据

import tushare as tsimport pandas as pd

2.1 分配预案

# argm: top=n,最近公布的那条数据# return: divi:每10股分红金额# return: shares:每10股转增和送股数df = ts.profit_data(year=2017, top=25)

df.head()

图2.1

2.2 业绩预告

# 获取2016年年报(第四季度)预报# return:report_date: 预报发布时间# return:pre_eps:去年同期每股收益# return:range:预报业绩变动范围df = ts.forecast_data(year=2017, quarter=3)

df.head()

图2.2

2.3 限售股解禁

# 获取特定年月限售股解禁数据# return: count:解禁数量# return: ratio:占总股本比例df = ts.xsg_data(year=2017, month=1)

df.head()

图2.3

2.4 基金持股

# 获取特定季度基金持有上市公司股票的数据# return: nums:基金家数# return:nlast: 与上期相比持仓基金家数变动# return:count:基金持股数(万股)# return:clast: 与上期相比基金持股数量变动# return:amount:基金持股市值# return:ratio:基金持股占流通股比例df = ts.fund_holdings(year=2016, quarter=4)

df.head()

图2.4.1

# 获取IPO数据# return: ipo_date: 上网发行日期# return:issue_date: 上市日期# return: amount: 发行数量(万股)# return:markets: 上网发行数量(万股)# return:price:发行价格# return:pe: 发行市盈率# return:limit:个人申购上线(万股)# return:funds:募集资金(亿元)# return: ballot: 网上中签率(%)df = ts.new_stocks()

df.head()

图2.4.2

2.5 融资融券

2.5.1 汇总数据

# 获取融资融券数据# return: rzye: 当日融资余额(元)# return: rzmre: 当日融资买入金额(元)# return: rqyl: 当日融券余量# return: rqylje: 当日融券余量金额(元)# return: rqmcl: 当日融券卖出量# return: rzrqjyzl: 当日融资融券余额

# 沪市融资融券数据df = ts.sh_margins(start='2017-01-01', end='2017-01-31')

df.head()

图2.5.1.1

# 深市融资融券数据df = ts.sz_margins(start='2017-01-01', end='2017-01-31')

df.head()

图2.5.1.2

2.5.2 明细数据

# 获取特定标的物的融资融券数据# return: rzche: 当日融资偿还额(元)# return:rqchl: 当日融券偿还量

# 沪市融资融券明细df = ts.sh_margin_details(start='2017-01-01', end='2017-01-31', symbol='600018')

df.head()

图2.5.2.1

# 深市融资融券明细# 深市融资融券名字每次只能获取一天的数据df = ts.sz_margin_details('2017-09-01')

df.head()

图2.5.2.2

2.6 应用实例

统计2016年末基金持仓比例最高和最低的100只股票在其后一个月和三个月的涨跌幅情况。

import tushare as tsimport pandas as pd

import seabornimport matplotlib.pyplot as plt

fund_holdings = ts.fund_holdings(year=2016, quarter=4)fund_holdings = fund_holdings[(fund_holdings.code.map(len)==6) & (fund_holdings.name.map(len)<5)]fund_holdings = fund_holdings.astype({'code': str, 'ratio':float}, copy=True)fund_holdings.head()

图2.6.1

fund_holdings = fund_holdings.sort_values('ratio')

bottom_stocks = fund_holdings.code[:100].tolist()top_stocks = fund_holdings.code[-100:].tolist()top_stocks[:5]

['002321', '600759', '600502', '002117', '600118']

series_list = []for code in top_stocks+bottom_stocks: s = ts.bar(code, '2016-12-30', '2017-03-31', adj='qfq').close s.name = code series_list.append(s)close = pd.DataFrame(series_list).Tclose.head()

图2.6.2

def cum_return(close_df, start:str, end:str):

close_df = close_df.astype('float')

close_df.index = pd.to_datetime(close_df.index)

change = close_df/close_df.shift(1)

start_dt, end_dt = pd.to_datetime((start, end))

change_part = change[start_dt:end_dt]

period_return = change_part.aggregate('prod') return period_return - 1def calc_return(close_df, top, bottom, start:str, end:str):

period_return = cum_return(close_df, start, end)

top_return = period_return[top].reset_index(drop=True)

bottom_return = period_return[bottom].reset_index(drop=True) return pd.DataFrame({'top': top_return, 'bottom':bottom_return})

return1 = calc_return(close, top_stocks, bottom_stocks, '2016-12-30','2017-01-31')return3 = calc_return(close, top_stocks, bottom_stocks, '2016-12-30','2017-03-31')return1.head()

图2.6.3

fig, ax = plt.subplots(1,2)fig.figsize = (90,45)ax[0].set_title('1 month return')ax[1].set_title('3 month return')ax[0].set_xlabel('period return')ax[1].set_xlabel('period return')return3.plot.hist(alpha=0.5, ax=ax[1])return1.plot.hist(alpha=0.5, ax=ax[0])ylim = ax[1].get_ylim()ax[0].set_ylim(ylim)plt.show()

图2.6.5

从以上分布图可以看到,2016年底基金持仓比例前100名的个股在之后一个月和三个月的总体表现都优于基金持仓比例最后100名的个股。需要指出的是,由于样本有限,这里得到的结果并不具有统计意义。此处仅提供一个可能的研究方向和代码实现思路,希望可以对大家有所帮助。附上GIF动图版,让你能更直观了解Python那些事儿~那么,就祝大家学习愉快! >>>点击咨询Python金融应用实战

  

量化金融分析师AQF交流答疑群:737550026

声明▎更多内容请关注微信号量化金融分析师。原创文章,欢迎分享,若需引用或转载请保留此处信息。

python从tushare获取数据_Python丨金融数据获取之tushare (下)相关推荐

  1. python从tushare获取数据_python调用tushare获取沪股通、深股通成份股数据

    python调用tushare获取沪股通.深股通成份股数据 发布时间:2020-07-20 17:30:07 来源:51CTO 阅读:195 作者:tushare01 接口:hs_const 描述:获 ...

  2. python从tushare获取数据_python调用tushare获取股票月线数据

    接口:monthly 描述:获取A股月线数据 限量:单次最大3700,总量不限制 积分:用户需要至少300积分才可以调取,具体请参阅本文最下方积分获取办法 注:tushare库下载和初始化教程,请查阅 ...

  3. python调用接口获取数据_python:接口间数据传递与调用方法

    如下所示: import requests import unittest import json from pubulic_way.get_token import getSession class ...

  4. 金融学习之一——使用Tushare获取数据并制图

    Tushare是金融数据获取的重要来源之一,并且已经有了非常好的Python第三方包配合使用.Tushare的官方网站是Tushare官网,里面提供了大量的金融相关数据,非常适合平时的数据练手,但需要 ...

  5. python从ip端口 获取数据_python 如何获得Ip地址和端口啊?

    1.什么是爬虫 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.想抓取什么?这个由你来控制它咯. ...

  6. python爬取下拉列表数据_Python+selenium之获取文本值和下拉框选择数据

    Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...

  7. python怎么从excel获取数据_python如何读取excel表数据

    python读取excel表数据的方法:首先安装Excel读取数据的库xlrd:然后获取Excel文件的位置并且读取进来:接着读取指定的行和列的内容,并将内容存储在列表中:最后运行程序即可. pyth ...

  8. 金融学习之十四——使用Tushare获取数据计算投资组合的预期收益率和波动率

    为了降低风险,投资者在购买股票时往往会构建一个投资组合,以对冲风险和最大获益.在投资组合中,描述该投资组合效果的两个重要变量是预期收益率及其波动率. 1.投资组合的预期收益率 预期收益率的计算公式为: ...

  9. python怎么从excel获取数据_python怎么从excel中读取数据?/python 读取 excle

    如何通过python快速输出数据库数据到excel 扩展库 xlrd 读excle xlwt 写excle 直接度就能下载 下载后使用 import xlrd 就可excle文件了 打开文件: xls ...

  10. python多进程爬虫保存数据_Python多进程爬虫东方财富盘口异动数据+Python读写Mysql与Pandas读写Mysql效率对比...

    先上个图看下网页版数据.mysql结构化数据 通过Python读写mysql执行时间为:1477s,而通过Pandas读写mysql执行时间为:47s,方法2速度几乎是方法1的30倍.在于IO读写上, ...

最新文章

  1. Spring单实例、多线程安全、事务解析
  2. python import出错_python import的一些问题
  3. 【互联网今日大事儿】陌陌今日上市马云变亚洲首富!
  4. 数据库最最常用语句(10年工作笔记)
  5. mysql 子查询 根据查询结果更新表
  6. 反射 字段_巧用 Protobuf 反射来优化代码,拒做 PB Boy
  7. 软件测试——测试基础
  8. 照片审核处理工具_不需要Photoshop,这5款在线处理图片工具能帮你好好处理照片!...
  9. day 09 学习Python——Python模块读取xls、slx文件,python发邮件
  10. 爬虫框架开发(4)--- 项目实战——新浪滚动新闻资讯实时数据采集
  11. matlab 求股票斜率,「matlab 求股票斜率」同花顺斜率抓强势股指标公式
  12. 两数之和——python
  13. HDU6438(贪心技巧)
  14. UnsatisfiedDependencyException: Error creating bean with name ‘personRecordServiceImpl‘:...
  15. 第5章-LC3, latency and QoS
  16. 如何把pdf转换成excel
  17. 金庸小说人物知识图谱构建——图谱可视化
  18. Python报错ReadTimeoutError
  19. 6亿视频号的8种变现模式
  20. Problem : [usaco2007 Feb]Lilypad Pond

热门文章

  1. 飞桨高阶使用教程:自定义CPU算子的实现和使用
  2. 马哥教育42期第三周作业
  3. linux yum安装xz,CentOS 7 上安装 xz utils 解压缩工具
  4. 【云原生】MYSQL语法总结
  5. 关于Win11家庭版安装Ansys2021R1遇到的问题
  6. 未名down了,人生无趣
  7. 页面加载数学公式,mathjax转html
  8. 架构师日常-技术or业务
  9. Ubuntu系统下基本配置Edison
  10. 笔记十一:提升高效技术领导的创新能力