tushare获取股票每日重要的基本面指标数据,并存入Elasticsearch
tushare是一个开放的,免费的金融数据平台,包含沪深股票数据,指数数据,基金数据,期货数据,期权数据,债券数据,外汇数据,港股数据,行业经济数据,宏观经济数据以及新闻快讯等特色数据。其中以沪深股票数据最为丰富,包含了有:
基本包含了沪深股票全部常用数据。
tushare 目前提供了四种获取数据的方式,分别为 http, Python SDK, Matlab SDK, R SDK。
这里介绍如何用Python SDK获取股票的每日指标数据。
(1)注册tushare用户,获取 token
注册网页链接为 https://tushare.pro/register?reg=369571
注册完成后可以在个人主页的接口TOKEN下看到自己的token

(2)安装 tushare
个人使用的python开发的IDE为 pycharm
pip install tushare -i https://pypi.tuna.tsinghua.edu.cn/simple
tushare依赖了numpy,pandas等一些库,安装完之后可能需要根据报错提示安装对应的库
(4)安装Elasticsearch
pip3 install elasticsearch -i https://pypi.tuna.tsinghua.edu.cn/simple
(5)调用tushare
这里把调用tushare的函数都封装在了一个文件里面,代码如下
import datetime
import time
import numpy as np
import tushare as ts ts.set_token('b15148f5ca285bd0e85bbc3f659daefff549ade3bba06fae6a037f03')
pro = ts.pro_api() # 股票列表
def get_all_stock():
stocks = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,fullname,area,industry,list_date')
return stocks # 每日指标
def get_daily_basic(share_code, start_date, end_date):
while 1:
try:
df = pro.daily_basic(ts_code=share_code, start_date=start_date, end_date=end_date, timeout=60)
return df
except:
print("get_daily_basic 获取失败,参数为:", share_code, start_date, end_date)
time.sleep(0.5)
stock_basic接口用于获取股票列表,本接口文档网址:https://tushare.pro/document/2?doc_id=25
daily_basic接口用于获取每日指标,网址:https://tushare.pro/document/2?doc_id=32。 这里用一个循环来获取,因为tushare对每分钟调用次数有限制(这也是为啥我要把数据保存到本地),超过次数限制时会报错,所以我这里用一个except获取异常,等待0.5s后重新再试。
(6)保存到elasticsearch
保存到elasticsearch之前当然需要本机已经启动了elasticsearch。
关于elasticsearch的安装配置见我的另一篇博客https://www.cnblogs.com/betterwgo/p/11240821.html
python 调用 tushare,并将数据保存到elasticsearch的代码如下:
# 每日指标
import configparser
import logging import numpy as np
from elasticsearch import Elasticsearch
from elasticsearch import helpers import stock_parser as parser logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
handler = logging.FileHandler("log_daily_basic.txt")
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Start print log") config = configparser.ConfigParser()
config.read("config.ini")
latest_daily_basic_tscode = config.get("daily", "latest_daily_basic_tscode") es = Elasticsearch([{'host': '127.0.0.1', 'port': 9200}]) # ts_code str TS股票代码
# trade_date str 交易日期
# close float 当日收盘价
# turnover_rate float 换手率(%)
# turnover_rate_f float 换手率(自由流通股)
# volume_ratio float 量比
# pe float 市盈率(总市值/净利润)
# pe_ttm float 市盈率(TTM)
# pb float 市净率(总市值/净资产)
# ps float 市销率
# ps_ttm float 市销率(TTM)
# total_share float 总股本 (万股)
# float_share float 流通股本 (万股)
# free_share float 自由流通股本 (万)
# total_mv float 总市值 (万元)
# circ_mv float 流通市值(万元)
body = {
"mappings": {
"properties": {
"ts_code": {
"type": "keyword"
},
"trade_date": {
"type": "integer"
},
"close": {
"type": "float"
},
"turnover_rate": {
"type": "float"
},
"turnover_rate_f": {
"type": "float"
},
"volume_ratio": {
"type": "float"
},
"pe": {
"type": "float"
},
"pe_ttm": {
"type": "float"
},
"pb": {
"type": "float"
},
"ps": {
"type": "float"
},
"ps_ttm": {
"type": "float"
},
"total_share": {
"type": "float"
},
"float_share": {
"type": "float"
},
"free_share": {
"type": "float"
},
"total_mv": {
"type": "float"
},
"circ_mv": {
"type": "float"
}
}
}
}
index = 'index_daily_basic'
es.indices.create(index=index, body=body, ignore=400) def check_float(item, x_name):
x = item[x_name]
if x is None or np.isnan(x):
x = 0.0
logger.info("%s %s %s is None or nan" % (item['ts_code'], item['trade_date'], x_name))
return x def es_insert_daily_basic(df):
actions = []
for i in range(len(df)):
df_item = df.iloc[i]
tscode = df_item['ts_code']
trade_date = int(df_item['trade_date'])
x = tscode.split('.', 1)
col_name = x[1] + x[0]
_id = col_name + df_item['trade_date'] close = check_float(df_item, 'close')
turnover_rate = check_float(df_item, 'turnover_rate')
turnover_rate_f = check_float(df_item, 'turnover_rate_f')
volume_ratio = check_float(df_item, 'volume_ratio')
pe = check_float(df_item, 'pe')
pe_ttm = check_float(df_item, 'pe_ttm')
pb = check_float(df_item, 'pb')
ps = check_float(df_item, 'ps')
ps_ttm = check_float(df_item, 'ps_ttm')
total_share = check_float(df_item, 'total_share')
float_share = check_float(df_item, 'float_share')
free_share = check_float(df_item, 'free_share')
total_mv = check_float(df_item, 'total_mv')
circ_mv = check_float(df_item, 'circ_mv')
action = {
"_index": index,
"_type": "_doc",
"_id": _id,
"_source": {
"ts_code": ts_code,
"trade_date": trade_date,
"close": close,
"turnover_rate": turnover_rate,
"turnover_rate_f": turnover_rate_f,
"volume_ratio": volume_ratio,
"pe": pe,
"pe_ttm": pe_ttm,
"pb": pb,
"ps": ps,
"ps_ttm": ps_ttm,
"total_share": total_share,
"float_share": float_share,
"free_share": free_share,
"total_mv": total_mv,
"circ_mv": circ_mv
}
}
# 形成一个长度与查询结果数量相等的列表
actions.append(action)
if i % 1000 == 0 or i == (len(df) - 1):
helpers.bulk(client=es, actions=actions)
actions.clear()
actions.clear() def update_latest_daily_basic_tscode(tscode):
config.set("daily", "latest_daily_basic_tscode", tscode)
# write to file
with open("config.ini", "w+") as f:
config.write(f) # 更新单只股票
def update_daily_basic(tscode, start_date, end_date):
df = parser.get_daily_basic(tscode, start_date, end_date)
es_insert_daily_basic(df)
return len(df) if __name__ == "__main__":
# 获取全部上市股票代码
stocks = parser.get_all_stock()
bIn = True
for i in range(len(stocks)):
stock = stocks.iloc[i]
ts_code = stock['ts_code']
if latest_daily_basic_tscode == ts_code:
bIn = False
if not bIn:
count = update_daily_basic(ts_code, '20000101', '')
print(i, ts_code, count)
update_latest_daily_basic_tscode(ts_code)
else:
print(i, ts_code)
这里日志用的logging,没具体研究一股脑全搬上来了,反正我只需要打印个错误日志就行。
然后还用了一个 configparser 来解析 ini 配置文件,config.ini文件中配置如下信息:
[daily]
latest_daily_basic_tscode = 000001.SZ
配置文件的目的是再程序中断后重新启动不用从第一个开始,直接从配置文件中的开始。获取股票列表的接口的第一条是 000001.SZ,所以这里初始配置为它,这里其实可以优化一下。
数据保存到elasticsearch用的是 helps中的bulk函数,做批量索引
看一下保存的结果情况:

tushare注册: https://tushare.pro/register?reg=369571

tushare获取股票每日重要的基本面指标数据,并存入Elasticsearch的更多相关文章
- python+tushare获取股票每日停复牌信息
接口:suspend 更新时间:不定期 描述:获取股票每日停复牌信息 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称 | 类型 | 必 ...
- 使用tushare获取股票历史数据和实时分笔数据
使用tushare获取股票历史数据和实时分笔数据 财经数据接口包tushare的使用(一) Tushare是一款开源免费的金融数据接口包,可以用于获取股票的历史数据.年度季度报表数据.实时分笔数据 ...
- 用python+tushare获取股票前复权后复权行情数据
接口名称 :pro_bar 接口说明 :复权行情通过通用行情接口实现,利用Tushare Pro提供的复权因子进行计算,目前暂时只在SDK中提供支持,http方式无法调取. Python SDK版本要 ...
- 使用tushare获取股票实时分笔数据延时有多大
使用tushare获取股票实时分笔数据延时有多大 前几天分享了一段获取所有股票实时数据的代码,有用户积极留言,提出一个非常棒的问题:如果数据本生的延时非常严重,通过代码获取数据再快又有什么用呢? 一直 ...
- 金融量化分析-python量化分析系列之---使用python获取股票历史数据和实时分笔数据
财经数据接口包tushare的使用(一) Tushare是一款开源免费的金融数据接口包,可以用于获取股票的历史数据.年度季度报表数据.实时分笔数据.历史分笔数据,本文对tushare的用法,已经存在的 ...
- python调用tushare获取股票日线实时行情数据
接口:daily 数据说明:交易日每天15点-16点之间.本接口是未复权行情,停牌期间不提供数据. 调取说明:基础积分每分钟内最多调取200次,每次4000条数据,相当于超过18年历史,具体请参阅本文 ...
- python+tushare获取股票和基金每日涨跌停价格
接口:stk_limit 描述:获取全市场(包含A/B股和基金)每日涨跌停价格,包括涨停价格,跌停价格等,每个交易日8点40左右更新当日股票涨跌停价格. 限量:单次最多提取4800条记录,可循环调取, ...
- python调用tushare获取股票月线数据
接口:monthly 描述:获取A股月线数据 限量:单次最大3700,总量不限制 积分:用户需要至少300积分才可以调取,具体请参阅本文最下方积分获取办法 注:tushare库下载和初始化教程,请查阅 ...
- python获取全部股票每日基本面指标,用于选股分析、报表展示等
接口:daily_basic 更新时间:交易日每日15点-17点之间 描述:获取全部股票每日重要的基本面指标,可用于选股分析.报表展示等. 积分:用户需要至少300积分才可以调取,具体请参阅本文最下方 ...
随机推荐
- 5、组件注册-@Scope-设置组件作用域
5.组件注册-@Scope-设置组件作用域 IOC容器默认都是单实例的 /** * * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SIN ...
- python的logging日志模块(一)
最近修改了项目里的logging相关功能,用到了Python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 ...
- python自动华 (八)
Python自动化 [第八篇]:Python基础-Socket编程进阶 本节内容: Socket语法及相关 SocketServer实现多并发 1. Socket语法及相关 sk = socket.s ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree
题 OvO http://codeforces.com/contest/963/problem/B CF 963B 964D 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...
- SIGAI深度学习第五集 自动编码器
深度学习模型-自动编码器(AE),就是一个神经网络的映射函数,f(x)——>y,把输入的一个原始信号,如图像.声音转换为特征. 大纲: 自动编码器的基本思想 网络结构 损失函数与训练算法 实际使 ...
- Start Failed, Internal error: recovering IDE to the working state after the critical startup error
Start Failed, Internal error: recovering IDE to the working state after the critical startup error F ...
- Oracle 审计 部署监控 user DML操作
1.移动audit表及索引到dbadmin表空间 alter table aud$ move tablespace DBADMIN;alter table AUDIT$ move tablespace ...
- python while for 语句
while 语句: >>> x = >>> : ... x += ... print(x) ... for 语句: >>> words = ['a ...
- qt 给父窗体设置样式不影响子控件样式以及子控件设置透明
1.父窗体样式不生效? this->setAttribute(Qt::WA_StyledBackground); 2.父窗样式体影响子控件? this->setStyleSheet(&qu ...
- CentOS 7.5 ——如何开放80、8080、3306等端口
CentOS 7.5 ——如何开放80.8080.3306等端口 ——说明:CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙——1.关闭firewall: s ...