大家好,又见面了,我是你们的朋友全栈君。
做股票量化分析,获取股票行情数据是第一步,结合网上的信息,和我用过的一些东西,做个总结。以后有新信息,逐步完善。
获取上证指数1:ts-pro
import tushare as ts
ts.set_token('********')
pro = ts.pro_api()
df = pro.index_daily(ts_code='000001.SH',start_date="20190101")
df = df.sort_values(by='trade_date',ascending=True)
df = df.reset_index(drop=True)
df.rename(columns={'vol':'volume'}, inplace = True)
pro.index_daily返回:ts_code、trade_date、close、open、high、low、pre_close、vol、amount等
获取上证指数2:ts-ifeng
import tushare as ts
dfi=ts.get_hist_data('sh') #获取上证指数k线:
dfi.sort_index(inplace=True)
dfi = dfi.reset_index(drop=False)
dfi.rename(columns={'date':'trade_date'}, inplace = True)
ts.get_hist_data返回:’date’, ‘open’, ‘high’, ‘close’, ‘low’, ‘volume’, 等,无amount
获取上证指数3:ts-腾讯gtimg.cn
import tushare as ts
def test_ts2():
dfi=ts.get_k_data('sh') #获取上证指数k线数据,其它参数与个股一致,下同
print(dfi)
return dfi
ts.get_k_data返回:1990年开市以来的所有‘date’, ‘open’, ‘close’, ‘high’, ‘low’, ‘volume’, ‘code’等,无amount
获取上证指数4:ts-vsf-sina
import tushare as ts
def test_ts3():
dfi=ts.get_h_data('sh') #获取上证指数k线数据,其它参数与个股一致,下同
print(dfi)
return dfi
返回:此法已经失效,原来应该是获取‘vsf’: ‘vip.stock.finance.sina.com.cn’,不知道为何失效。
其它常用api df1=pro.trade_cal(exchange=‘SSE’, start_date=sdate,end_date=edate,is_open=‘1’) df = pro.daily(trade_date=‘20180810’) #获取日线行情,里面有昨收 df = pro.top_inst(trade_date=idate) #获取龙虎榜机构机构明细
https://akshare.readthedocs.io/
有一个dtshare,数据和调用方式都类似。
获取股票指数历史
1.股票指数数据是从新浪财经获取的数据
def test_sina():
import akshare as ak
stock_zh_index_daily_df = ak.stock_zh_index_daily(symbol="sh000001")
print(stock_zh_index_daily_df)
返回:date\open\close\high\low\volume
zh_sina_index_stock_hist_url = “https://finance.sina.com.cn/realstock/company/{}/hisdata/klc_kl.js”
1.股票指数数据是从腾讯获取的数据
def test_tx():
import akshare as ak
stock_zh_index_daily_tx_df = ak.stock_zh_index_daily_tx(symbol="sh000001")
print(stock_zh_index_daily_tx_df)
返回:date\open\close\high\low\amount
url = “http://web.ifzq.gtimg.cn/appstock/app/fqkline/get”
测试的时候速度比sina慢不少。
def test_bao():
import baostock as bs
import pandas as pd
# 登陆系统
lg = bs.login()
# 显示登陆返回信息
rs = bs.query_history_k_data_plus("sh.000001",
"date,code,open,high,low,close,preclose,volume,amount,pctChg",
start_date='1990-01-01', frequency="d")
# 打印结果集
data_list = []
while (rs.error_code == '0') & rs.next():
# 获取一条记录,将记录合并在一起
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
print(result)
# 登出系统
bs.logout()
return result
返回:1990-12-19开始的’date’, ‘code’, ‘open’, ‘high’, ‘low’, ‘close’, ‘preclose’, ‘volume’,’amount’,等。
数据较快;close比对还行,两位小数,和三位的略有误差;交叉调用时注意volume的单位
开源的数据提取工具,专注在各类网站上爬取数据,并通过简单易用的API方式使用
get_daily | 获取日线数据 | 雪球 |
---|
https://github.com/PKUJohnson/OpenData/wiki
用fix_yahoo_finance库修正雅虎源,用了一下,能用,但是据说在走下坡路,还是别太投入了。
def test_yahoo():
#可以通过如下获取上证指数的数据,缺点:
from pandas_datareader import data, wb
from datetime import datetime
start1=datetime(1990,1,1)
end1=datetime(1993,1,1)
df = data.DataReader("000001.SS", 'yahoo', start1, end1)
print(df)
return df
速度很慢,上述代码有一次是11秒运行完,另一次2分钟都没返回结果,直接重启内核了。(20200419)
用了优矿和聚宽,主要以优矿为主,特别是里面的涨跌停信息,做涨跌停有关的分析很方便。之前还有米筐等。
DataAPI.MktEqudGet # 取沪深AB股日行情信息,包含昨收价、开盘价、最高价、最低价、收盘价、成交量、成交金额等字段,每日16:00更新
DataAPI.MktLimitGet #获取股票历史涨跌停价格
需申请,可使用一个月
https://www.ricequant.com/welcome/
另有:VNPY-tqsdk -看界面不错
比如通达信的高级导出功能,注意上证指数等指数需要单独导出。 要注意下载数据可能不完整,逐步检查, 优缺点:这个数据的好处是速度快。
https://xueqiu.com/stock/forchartk/stocklist.json?symbol=<股票代码>&period=<采样频率>&type=<复权类型>&begin=<起始时间戳>&end=<终止时间戳>
http://quotes.money.163.com/service/chddata.html?code=<股票代码>&start=<起始时间>&end=<终止时间>&fields=<指标用分号分隔>
网易财经可以下载股票和指数的历史数据。通过在chrome的调试工具中观察请求信息,发现网址为 http://quotes.money.163.com/service/chddata.html?code=0000001&start=19901219&end=20200101&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER 这里0000001指的是上证指数。注意这串数字要分0和000001两部分看。0代表sh,1代表sz。所以0000001其实是sh000001的意思。同理,0 000300也就是sh000300 沪深300的代码。后面的start和end没什么问题。fields选项中,TCLOSE,HIGH,LOW,TOPEN分别表示当日的收盘,最高,最低,开盘价;LCLOSE表示昨日收盘价。CHG,PCHG,VOTURNOVER,VAT分别表示涨跌额,涨跌幅,成交量,成交金额。如果你在浏览器中直接输入网址的话,会下载一个csv文件。如果直接用urllib.request来打开的话,会得到一长串字符串,里面包含了所有的数据,如下所示。
以工商银行为例的URL:
日内实时盘口(JSON): http://api.money.126.net/data/feed/1000002,1000001,1000881,0601398,money.api
历史成交数据(CSV): http://quotes.money.163.com/service/chddata.html?code=0601398&start=20000720&end=20150508 财务指标(CSV): http://quotes.money.163.com/service/zycwzb_601398.html?type=report
资产负债表(CSV): http://quotes.money.163.com/service/zcfzb_601398.html
利润表(CSV): http://quotes.money.163.com/service/lrb_601398.html
现金流表(CSV): http://quotes.money.163.com/service/xjllb_601398.html
杜邦分析(HTML): http://quotes.money.163.com/f10/dbfx_601398.html
和讯接口的特点是可以控制采样的数量和方向。
http://webstock.quote.hermes.hexun.com/a/kline?code=<股票代码>&start=<时间定位点>&number=<采样数量和方向>&type=<采样频率代码>
和讯还提供了更简单的接口: 全部日线行情:
http://flashquote.stock.hexun.com/Quotejs/DA/<交易所标记>_<股票代码>DA.html
全部分时行情:
http://flashquote.stock.hexun.com/Quotejs/MA/<交易所标记><股票代码>_MA.html
交易所标记中的1表示沪市,2表示深市 接口差别在D和M,注意有两处
http://data.gtimg.cn/flashdata/hushen/latest/<采样频率>/<股票代码>.js
http://hq.sinajs.cn/list=<股票代码>
A股股票&基金 http://hq.sinajs.cn/list=sh601006 http://hq.sinajs.cn/list=sh502007 A股指数 http://hq.sinajs.cn/list=s_sz399001 港股股票 http://hq.sinajs.cn/list=hk02333 http://hq.sinajs.cn/list=rt_hkCSCSHQ #沪港通资金流量 港股指数 http://hq.sinajs.cn/list=int_hangseng http://hq.sinajs.cn/list=rt_hkHSI http://hq.sinajs.cn/list=hkHSI,hkHSCEI,hkHSCCI #恒生指数,恒生国企指数,恒生红筹指数 美股股票&基金 http://hq.sinajs.cn/list=gb_amzn http://hq.sinajs.cn/list=usr_amzn http://hq.sinajs.cn/list=usr_russ 美股指数 http://hq.sinajs.cn/list=int_nasdaq http://hq.sinajs.cn/list=gb_ixic #纳斯达克指数 http://hq.sinajs.cn/list=int_dji http://hq.sinajs.cn/list=int_sp500 http://hq.sinajs.cn/list=int_ftse #伦敦指数 http://hq.sinajs.cn/list=int_bloombergeuropean500 #彭博欧洲500指数 http://hq.sinajs.cn/list=int_dax30,int_djstoxx50 外汇行情 http://hq.sinajs.cn/list=XAUUSD http://hq.sinajs.cn/list=DINIW #美元指数 黄金&白银 http://hq.sinajs.cn/list=hf_XAU http://hq.sinajs.cn/list=hf_XAG http://hq.sinajs.cn/list=hf_GC #COMEX黄金 http://hq.sinajs.cn/list=hf_SI #COMEX白银 http://hq.sinajs.cn/list=hf_AUTD #黄金TD http://hq.sinajs.cn/list=hf_AGTD #白银TD http://hq.sinajs.cn/list=AU0 #黄金期货 http://hq.sinajs.cn/list=AG0 #白银期货 http://hq.sinajs.cn/list=hf_CL #NYMEX原油 期货 http://hq.sinajs.cn/list=CFF_LIST #金融期货合约 http://finance.sina.com.cn/iframe/futures_info_cff.js #商品与金融期货合约 http://hq.sinajs.cn/?list=CFF_RE_IF1705 #合约行情 期权合约的月份 http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName 期权合约到期日 http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getRemainderDay?date=201705 看涨期权合约 http://hq.sinajs.cn/list=OP_UP_5100501705 看跌期权合约 http://hq.sinajs.cn/list=OP_DOWN_5100501705 期权行情 http://hq.sinajs.cn/list=CON_OP_10000869 http://hq.sinajs.cn/list=CON_ZL_10000869 http://hq.sinajs.cn/list=CON_SO_10000869 热门股票 http://finance.sina.com.cn/realstock/company/hotstock_daily_a.js 新股日历 http://vip.stock.finance.sina.com.cn/corp/view/iframe/vAK_NewStockIssueFrame_2015.php?num=10 定增列表 http://vip.stock.finance.sina.com.cn/corp/view/vAK_IncreaseStockIssueFrame_2015.php?num=10 基金公司 http://vip.stock.finance.sina.com.cn/fund_center/api/jsonp.php/var%20companyList=/NetValue_Service.getAllCompany
停更数据: http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh000001&end_date=20120101 http://market.finance.sina.com.cn/downxls.php?date=2011-07-08&symbol=sh600900
内容:该业务已经下线。给您造成的不便,深表歉意。如果行情页无法正常访问,可反馈至:finproduct@staff.sina.com.cn 。如果是抓接口数据,臣妾就木有办法了。您赶紧找个其他的源抓吧,我们不再恢复这个接口了~
https://finance.yahoo.com/quote/<股票代码.交易所>/history
http://www.aigaogao.com/tools/history.html?s=<股票代码>
有钱了用收费的吧,还没用过,也没体验过。 比如东财choice(单用户5800元一年),wind等,有很多,有钱就用收费的。
有观点认为:
获取股票数据的渠道有很多,而且基本上是免费的。
目前股票端用的比较多的有通达信,tushare, Quantaxis等,
期货端有CTP,CTPBEE,VNPY,TQSDK等,
下面介绍几个~
一己之力可以通天
的大佬,同样使用QA
你可以很轻松的使用到历史数据,注意,他是将数据写入到本地mongodb
数据库。然后同时提供了取数据的代码。当然QA也支持股票数据。https://zhuanlan.zhihu.com/p/100110280
其它:
获取股票数据的源头主要有:数据超市、雅虎、新浪、Google、和讯、搜狐、ChinaStockWebService、东方财富客户端、证券之星、网易财经。
End
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127821.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有