获取股票数据
Python 库
一、 baostock
为什么把bs推荐为第一个?因为稳定,很多其他的开源python库,提供一段时间数据后,就要收费。bs一直免费。
官方提供的安装方法
方式1:pip install baostock
使用国内源安装:
pip install baostock -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
方式2:
访问 https://pypi.python.org/pypi/baostock 下载安装python setup.py install或pip install xxx.whl
注意:程序运行时,文件名、文件夹名不能是baostock。
使用例子一 、获取 股票的列表
import baostock as bs
lg = bs.login()
# 获取行业分类数据
rs = bs.query_stock_industry()
while (rs.error_code == '0') & rs.next() :
# 获取一条记录,将记录合并在一起
row = rs.get_row_data()
print(row)
执行结果的片段如下:
['2022-03-21', 'sh.600262', '北方股份', '机械设备', '申万一级行业']
['2022-03-21', 'sh.600263', '路桥建设', '', '申万一级行业']
['2022-03-21', 'sh.600265', '*ST景谷', '农林牧渔', '申万一级行业']
['2022-03-21', 'sh.600266', '城建发展', '房地产', '申万一级行业']
['2022-03-21', 'sh.600267', '海正药业', '医药生物', '申万一级行业']
['2022-03-21', 'sh.600268', '国电南自', '电气设备', '申万一级行业']
['2022-03-21', 'sh.600269', '赣粤高速', '交通运输', '申万一级行业']
['2022-03-21', 'sh.600270', '外运发展', '', '申万一级行业']
['2022-03-21', 'sh.600271', '航天信息', '计算机', '申万一级行业']
['2022-03-21', 'sh.600272', '开开实业', '医药生物', '申万一级行业']
['2022-03-21', 'sh.600273', '嘉化能源', '化工', '申万一级行业']
['2022-03-21', 'sh.600275', '*ST昌鱼', '农林牧渔', '申万一级行业']
['2022-03-21', 'sh.600276', '恒瑞医药', '医药生物', '申万一级行业']
['2022-03-21', 'sh.600277', '亿利洁能', '化工', '申万一级行业']
['2022-03-21', 'sh.600278', '东方创业', '商业贸易', '申万一级行业']
['2022-03-21', 'sh.600279', '重庆港', '交通运输', '申万一级行业']
['2022-03-21', 'sh.600280', '中央商场', '商业贸易', '申万一级行业']
['2022-03-21', 'sh.600281', '华阳新材', '综合', '申万一级行业']
使用例子 二 、获取 股票的日K
import baostock as bs
lg = bs.login()
kdata = bs.query_history_k_data_plus('sh.600276','date,open,high,low,close,volume', start_date='2022-04-20',frequency='d')
data = kdata.get_data()
print(data)
执行结果:
date open high low close volume
0 2022-04-20 33.3000 35.1700 33.2100 34.3200 75469579
1 2022-04-21 34.2000 34.6000 32.9200 33.1700 47158907
2 2022-04-22 32.9600 33.6000 31.6100 33.0000 45291438
3 2022-04-25 30.1500 30.7900 29.7000 29.7000 103961707
4 2022-04-26 29.0000 29.0300 27.6800 27.7400 108285815
5 2022-04-27 27.0100 28.1600 27.0000 28.0600 70800384
6 2022-04-28 27.9600 28.7200 27.8000 28.4500 57221982
7 2022-04-29 28.4400 29.6900 28.3700 29.4800 57047871
这里隐藏了一个参数结束日期。如果想抓去分钟线,或者周线。只要更改frequency参数即可。
参数含义:
- code:股票代码,sh或sz.+6位数字代码,或者指数代码,如:sh.601398。sh:上海;sz:深圳。此参数不可为空;
- fields:指示简称,支持多指标输入,以半角逗号分隔,填写内容作为返回类型的列。详细指标列表见历史行情指标参数章节,日线与分钟线参数不同。此参数不可为空;
- start:开始日期(包含),格式“YYYY-MM-DD”,为空时取2015-01-01;
- end:结束日期(包含),格式“YYYY-MM-DD”,为空时取最近一个交易日;
- frequency:数据类型,默认为d,日k线;d=日k线、w=周、m=月、5=5分钟、15=15分钟、30=30分钟、60=60分钟k线数据,不区分大小写;指数没有分钟线数据;周线每周最后一个交易日才可以获取,月线每月最后一个交易日才可以获取。
- adjustflag:复权类型,默认不复权:3;1:后复权;2:前复权。已支持分钟线、日线、周线、月线前后复权。 BaoStock提供的是涨跌幅复权算法复权因子,具体介绍见:复权因子简介或者BaoStock复权因子简介。
缺点是日K需要等到晚上才能更新,如果想白天实时查看数据,就没有办法了。
二、 pytdx
pytdx 就是通达信数据,非官方提供的库。
安装
pip3 install pytdx
安装完成后,可以用自带的命令hqget测试:
-->rainx@JingdeMacBook-Pro:~/dev/pytdx [master]$ hqget
连接中....
请选择服务器
--------------------
[1] :招商证券深圳行情 (119.147.212.81:7709)
[2] :华泰证券(南京电信) (221.231.141.60:7709)
[3] :华泰证券(上海电信) (101.227.73.20:7709)
[4] :华泰证券(上海电信二) (101.227.77.254:7709)
[5] :华泰证券(深圳电信) (14.215.128.18:7709)
[6] :华泰证券(武汉电信) (59.173.18.140:7709)
[7] :华泰证券(天津联通) (60.28.23.80:7709)
[8] :华泰证券(沈阳联通) (218.60.29.136:7709)
[9] :华泰证券(南京联通) (122.192.35.44:7709)
[10] :华泰证券(南京联通) (122.192.35.44:7709)
--------------------
请输入序号 [1]:
获取日K数据
from pytdx.hq import TdxHq_API
api = TdxHq_API()
if api.connect('119.147.212.81', 7709):
#获取10天的
data = api.get_security_bars(9, 0, '000001', 0, 10)
print(data)
data = api.get_security_bars(9, 1, '600600', 0, 10)
print(data)
api.disconnect()
-
如果上面的IP连接慢,大家可以试着连接下面的服务器IP:
-
39.98.198.249:7709
- 8.142.137.74:7709
第一个参数,9表示日K,第二个参数0表示深市,1表示沪市,0,"000001",就是平安,1,"600600",青岛啤酒。 第四个参数0,表示最后一个交易日,10,表示取10个交易日的数据。
获取财务信息
api.get_finance_info(0, '000001')
获取板块
from pytdx.hq import TdxHq_API
api = TdxHq_API()
if api.connect('119.147.212.81', 7709):
all_list = api.get_security_list(1, 0)
for i in all_list:
print(i)
结果片段:
OrderedDict([('code', '880421'), ('volunit', 100), ('decimal_point', 2), ('name', '广告包装'), ('pre_close', 1104.02001953125)])
OrderedDict([('code', '880422'), ('volunit', 100), ('decimal_point', 2), ('name', '文教休闲'), ('pre_close', 1272.5799560546875)])
OrderedDict([('code', '880423'), ('volunit', 100), ('decimal_point', 2), ('name', '酒店餐饮'), ('pre_close', 1440.739990234375)])
OrderedDict([('code', '880424'), ('volunit', 100), ('decimal_point', 2), ('name', '旅游'), ('pre_close', 4186.5)])
OrderedDict([('code', '880425'), ('volunit', 100), ('decimal_point', 2), ('name', '旅游服务'), ('pre_close', 6163.16015625)])
OrderedDict([('code', '880426'), ('volunit', 100), ('decimal_point', 2), ('name', '旅游景点'), ('pre_close', 1131.199951171875)])
OrderedDict([('code', '880430'), ('volunit', 100), ('decimal_point', 2), ('name', '航空'), ('pre_close', 1539.6500244140625)])
OrderedDict([('code', '880431'), ('volunit', 100), ('decimal_point', 2), ('name', '船舶'), ('pre_close', 496.94000244140625)])
OrderedDict([('code', '880432'), ('volunit', 100), ('decimal_point', 2), ('name', '运输设备'), ('pre_close', 997.1199951171875)])
OrderedDict([('code', '880437'), ('volunit', 100), ('decimal_point', 2), ('name', '通用机械'), ('pre_close', 1069.699951171875)])
OrderedDict([('code', '880438'), ('volunit', 100), ('decimal_point', 2), ('name', '机床制造'), ('pre_close', 595.02001953125)])
OrderedDict([('code', '880439'), ('volunit', 100), ('decimal_point', 2), ('name', '机械基件'), ('pre_close', 1170.68994140625)])
OrderedDict([('code', '880440'), ('volunit', 100), ('decimal_point', 2), ('name', '工业机械'), ('pre_close', 1457.18994140625)])
OrderedDict([('code', '880441'), ('volunit', 100), ('decimal_point', 2), ('name', '化工机械'), ('pre_close', 1119.7900390625)])
OrderedDict([('code', '880442'), ('volunit', 100), ('decimal_point', 2), ('name', '轻工机械'), ('pre_close', 873.2000122070312)])
pytdx 的优点,就是数据实时,还能板块数据。做个股和板块对应关系分析十分方便。
网页接口
三、 sina
https://quotes.sina.cn/cn/api/json_v2.php/CN_MarketDataService.getKLineData?symbol=sh000300&scale=30&ma=no&datalen=1023
symnol = 股票代码
scale = 5,15,30,60
datalen = 获取数据长度,最大1023
例如获取青岛啤酒的日K
https://quotes.sina.cn/cn/api/json_v2.php/CN_MarketDataService.getKLineData?symbol=sh600600&scale=240&ma=no&datalen=1023
结果片段:
{"day":"2018-02-08","open":"37.000","high":"37.800","low":"36.640","close":"37.480","volume":"4438518"},{"day":"2018-02-09","open":"36.390","high":"36.640","low":"35.080","close":"35.740","volume":"6105094"},{"day":"2018-02-12","open":"35.990","high":"36.970","low":"35.860","close":"36.750","volume":"3295634"},{"day":"2018-02-13","open":"37.030","high":"38.100","low":"37.030","close":"37.320","volume":"3505706"},{"day":"2018-02-14","open":"37.340","high":"37.950","low":"37.090","close":"37.820","volume":"1732576"},{"day":"2018-02-22","open":"38.370","high":"38.770","low":"38.040","close":"38.430","volume":"4764176"},{"day":"2018-02-23","open":"38.450","high":"38.680","low":"38.060","close":"38.630","volume":"2664284"},{"day":"2018-02-26","open":"38.740","high":"39.230","low":"38.250","close":"39.030","volume":"3721965"},{"day":"2018-02-27","open":"38.940","high":"38.990","low":"37.700","close":"38.130","volume":"4103654"},{"day":"2018-02-28","open":"37.810","high":"38.160","low":"36.700","close":"38.130","volume":"4420148"},{"day":"2018-03-01","open":"37.760","high":"38.470","low":"37.510","close":"38.250","volume":"3386758"},{"day":"2018-03-02","open":"37.680","high":"38.220","low":"37.390","close":"37.500","volume":"3128880"},{"day":"2018-03-05","open":"37.500","high":"37.880","low":"36.780","close":"36.960","volume":"3580367"},{"day":"2018-03-06","open":"37.120","high":"37.640","low":"36.800","close":"37.430","volume":"4122206"}
成交价格图:
https://market.finance.sina.com.cn/pricehis.php?symbol=sz000506&startdate=2021-12-27&enddate=2022-05-02

四、 腾讯接口
https://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600600,m1
五、 凤凰网
可以同时获取多只股票的交易数据
https://hq.finance.ifeng.com/q.php?l=sh600774,sh600600
api.finance.ifeng.com/aminhis/?code=sz002259&type=five
其他资源列表
- https://github.com/QUANTAXIS/QUANTAXIS 丰富的数据接口,
- https://github.com/pythonstock/stock 接口丰富的 股票数据和指标分析
- https://github.com/shidenggui/easyquant 量化框架
其中 下面网址有很多接口: quantaxis