某知名门户网站财经频道的行情系统,采用了自行开发的分布式K-V NoSQL引擎,支持http和websocket协议,支持高并发、短延时的行情展示能力。
注意,本文公布的所有行情接口仅限于个人兴趣研究之目的,任何人不得对接口实施恶意攻击或商业性用途,商业盈利性应用本文行情接口应取得SINA公司书面许可!
股票实时行情
比如,获取浦发银行的实时行情接口:
http://hq.sinajs.cn/list=sh600000
返回的数据如下所示:
var hq_str_sh600000="浦发银行,14.96,15.03,14.90,14.99,14.84,14.89,14.90,21517664,320750084,147500,14.89,115636,14.88,49300,14.87,174200,14.86,272100,14.85,2900,14.90,1300,14.91,30610,14.92,146500,14.93,11400,14.94,2015-09-21,11:24:45,00";
各个字段的含义:
var 代码 =“证券简称,今日开盘价,昨日收盘价,最近成交价,最高成交价,最低成交价,买入价,卖出价,成交数量,成交金额,买数量一,买价位一,买数量二,买价位二,买数量三 ,买价位三,买数量四,买价位四,买数量五,买价位五,卖数量一,卖价位一,卖数量二,卖价位二,卖数量三,卖价位三,卖数量四,卖价位四,卖数量五,卖价位五,行情日期,行情时间,停牌状态”
如果嫌上面的接口返回字段太多,可以使用简版接口:
http://hq.sinajs.cn/list=s_sh600000
在股票代码前面加上"s_"即可;
返回的数据如下:
var hq_str_s_sh600000="浦发银行,14.91,-0.12,-0.80,219647,32741";
各个字段的含义:
var 代码=“证券简称,最新价,涨跌额,涨跌幅,成交量,成交额”
list参数支持批量获取多个股票的行情,也包括B股、港股、美股,例如:
http://hq.sinajs.cn/list=sh600000,sz200625,rt_hk01988,gb_jd
返回的数据如下:
var hq_str_sh600000="浦发银行,14.96,15.03,15.00,15.08,14.84,14.98,14.99,28917538,431606931,38300,14.98,40100,14.97,16500,14.96,357400,14.95,132598,14.94,10000,14.99,650605,15.00,11800,15.01,16600,15.02,56700,15.03,2015-09-21,13:56:47,00";
var hq_str_sz200625="长 安B,12.450,12.550,12.390,12.540,12.350,12.390,12.400,1272820,15834945.530,8884,12.390,14500,12.380,15868,12.370,6900,12.360,7800,12.350,400,12.400,700,12.470,1000,12.480,3954,12.490,5700,12.500,2015-09-21,13:56:52,00";
var hq_str_hk01988="MINSHENG BANK,民生银行,7.520,7.660,7.570,7.420,7.480,-0.180,-2.350,7.480,7.490,77661532,10400676,5.965,2.473,11.880,6.700,2015/09/21,13:39";
var hq_str_usr_jd="京东,26.43,-4.38,2015-09-19 08:17:16,-1.21,27.02,27.54,26.29,38.00,21.55,12731158,14505212,36132079411,-0.21,--,0.00,0.00,0.00,0.00,1367085865,57.00,26.43,0.00,0.00,Sep 18 07:46PM EDT,Sep 18 04:01PM EDT,27.64,223094.00";
其中,
港股返回的字段含义如下:
var 股票代码=“英文名,中文名,开盘,昨收,最高,最低,当前,涨跌额,涨跌幅,卖一,买一,成交额,成交量,市盈率,收益率,52周最高,52周最低,行情日期,行情时间”
美股返回的字段含义如下:
gb_symbol="美股中文名称,最新价,涨跌幅,数据更新时间(北京时间),涨跌额,开盘价,最高价,最低价,52周最高价,52周最低价,成交量(单位股),平均成交量(单位股),市值(单位元),每股收益,市盈率,fpe,贝塔系数,股息,收益率,总股本(单位元),instown,盘前盘后价,盘前盘后涨跌幅,盘前盘后涨跌额,盘前盘后数据更新时间(美东),盘中数据更新时间(美东),昨收价,盘前盘后成交量"
美股静态数据接口:
http://hq.sinajs.cn/list=gb_jd_i
返回的数据如下:
var hq_str_gb_jd_i="O,e-commerce discretionary,19.0000";
各个字段的含义:
gb_symbol_i=交易市场(O,N,A),行业,发行价
注:O=纳斯达克交易所,N=纽交所,A=美国交易所
此外,该接口还支持两个可选的参数:func 和 format,例如:
http://hq.sinajs.cn/format=js&func=callback();&list=sh600000
其中,
format参数有两个取值:js和text,且js是默认的格式;
func参数是供给前端回调使用,原样输出;
A股分时数据
请求个股一天的分时数据,例如:
http://hq.sinajs.cn/list=ml_sh600000
返回的是base64编码的字符串,从头开始每16个字符对应1分钟的数据,将其解码成3个int型整数,依次为均价,实时价格,和成交量,其中均价和价格需要除以1000。
下面用Python代码演示如何解码接口返回的字符串:
import re
import base64
import urllib2
import struct
def ParseStock(url):
fp = urllib2.urlopen(url, timeout=5)
content = fp.read()
patt = re.compile(r'\"(.*)\"')
m = patt.search(content)
start = m.start() + 1
end = m.end() - 1
while start < end:
min = (content[start:start+16])
start += 16
b = base64.b64decode(min)
avg = struct.unpack('<L', bytes(b[:4]))[0] / 1000.0
price = struct.unpack('<L', bytes(b[4:8]))[0] / 1000.0
amount = struct.unpack('<L', bytes(b[8:12]))[0]
print(avg, price, amount)
URL = "http://hq.sinajs.cn/list=ml_sh000001"
ParseStock(URL)
更多数据接口:
5分、15分、30分、60分
http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sh600000&scale=15&ma=no&datalen=1023
5日、1年数据接口:
http://finance.sina.com.cn/realstock/company/sh600000/hisdata/klc_cm.js
日K:
http://finance.sina.com.cn/realstock/company/sh600000/hisdata/klc_kl.js
环球股指
还可以请求全球主要市场的股票指数:
http://hq.sinajs.cn/list=gb_$ixic,gb_$dji,gb_$inx,hkHSI,nb_UKX,nb_NKY
返回的数据如下:
var hq_str_gb_$ixic="纳斯达克,4827.23,-1.36,2015-09-21 18:00:45,-66.72,4828.71,4878.71,4819.08,5231.94,4116.60,3275437123,1872127002,0,0.00,--,0.00,0.00,0.00,0.00,0,0.00,0.00,0.00,0.00,,Sep 18 05:15PM EDT,4893.95,0.00";
var hq_str_gb_$dji="道琼斯,16384.58,-1.74,2015-09-19 12:12:53,-290.16,16674.74,16674.74,16343.76,18351.36,15370.33,177090000,138606998,0,0.00,0.0,0.00,0.00,0.00,0.00,0,0.00,0.00,0.00,0.00,,Sep 19 12:12AM EDT,16674.74,0.00";
var hq_str_gb_$inx="标普指数,1958.03,-1.62,2015-09-19 12:12:59,-32.17,1989.66,1989.66,1953.45,2134.72,1820.66,0,3726753997,0,0.00,0.0,0.00,0.00,0.00,0.00,0,0.00,0.00,0.00,0.00,,Sep 19 12:12AM EDT,1990.20,0.00";
var hq_str_hkHSI="Hang Seng Main Index,恒生指数,21614.43,21920.83,21778.06,21590.70,21756.93,-163.90,-0.75,0.000,0.000,70237827,0,0.000,0.00,28588.52,20524.88,2015/09/21,16:01";
var hq_str_nb_UKX="富时100指数,6122.81,6104.1099,2015-09-21,11:20:00";
var hq_str_nb_NKY="日经225指数,18070.211,18422,2015-09-18,15:00:00";
各个字段的含义:
var 代码="名称,点位,涨跌额,涨跌幅,日期时间"
财务数据
除了成交价、成交量这些实时行情数据以外,接口也能提供一些财务数据,例如:
http://hq.sinajs.cn/list=sh600000_i
返回的数据:
var hq_str_sh600000_i="A,pfyh,2.5210,2.5877,1.2810,13.7120,3811.7996,1865347.1415,1492277.7132,1492277.7132,0,CNY,470.2600,482.7300,10.0000,1,9.3500,707.0100,239.0300";
各个字段的含义:
var 代码=“代码=证券类型,证券拼音,最近年度摊薄每股收益(eps),最近4个季度摊薄每股收益之和,今年迄今为止的每股收益,每股净资产,过去5个交易日平均每分钟成交量,总股本 (万股),流通股本(万股),流通A股 (万股),流通B股 (万股),计价单位(CNY,US,HK) ,最近年度净利润,q4_profit_sum,发行价,状态【0-无该记录 1-上市正常交易 2-未上市 3-退市】, 最近一个报告期的净资产收益率(%),最近一个报告期的主营收入(亿元),最近一个报告期的净利润(亿元)”
股指期货行情
例如,请求IF1510合约的行情:
http://hq.sinajs.cn/list=CFF_RE_IF1510
返回的数据:
var hq_str_CFF_RE_IF1510="3107.4,3234.8,3100.0,3232.2,16556,15751900000,23505,3232.2,,3453.2,2825.6,,,3139.0,3139.4,21569,0,0,--,--,--,--,--,--,--,--,0,0,--,--,--,--,--,--,--,--,2015-09-21,15:15:15,0,1,3234.800,2946.800,3305.000,2946.800,3385.800,2614.600,3385.800,2614.600,0.000";
各个字段的含义:
var CFF_RE_合约代码=“今开盘,最高价,最低价, 最新价,成交量,成交金额,持仓量,今收盘,今结算,涨停板价,跌停板价,昨虚实度,今虚实度, 昨收盘,昨结算,昨持仓量,申买价一,申买量一,申买价二,申买量二, 申买价三,申买量三,申买价四,申买量四,申买价五,申买量五,申卖价一,申卖量一, 申卖价二,申卖量二,申卖价三,申卖量三,申卖价四,申卖量四,申卖价五,申卖量五,交易日,行情时间,行情时间毫秒”
基金行情数据
该接口包括分级基金、ETF,例如:
http://hq.sinajs.cn/list=f_502004,f_502005,f_510050,f_510710
返回的数据如下:
var hq_str_f_502004="易方达军工分级A,1.0099,1.0099,1.0097,2015-09-18,0.250769";
var hq_str_f_502005="易方达军工分级B,0.3509,0.3509,0.3377,2015-09-18,0.250769";
var hq_str_f_510050="华夏上证50ETF,2.202,2.951,2.198,2015-09-18,137.68";
var hq_str_f_510710="博时上证50ETF,2.1769,0.673,2.1727,2015-09-18,1.0822";
各个字段的含义:
var 基金代码="<基金简称>,<最新净值>,<累计净值>,<昨日净值>,<净值日期>,<基金规模(亿份)>"
开放基金的行情接口:
http://hq.sinajs.cn/list=fu_150181,fu_150182,fu_150019
返回的数据如下:
var hq_str_fu_150181="富国中证军工指数分级A,15:04:00,1.0220,1.0220,1.0850,0,0,2015-09-21";
var hq_str_fu_150182="富国中证军工指数分级B,15:04:00,0.4630,0.4100,2.4820,0.1298,12.9268,2015-09-21";
var hq_str_fu_150019="银华锐进,15:04:00,0.7311,0.6770,0.6770,0.1233,7.9911,2015-09-21";
各个字段的含义:
var 基金代码="名称,时间,最新估值,单位净值,累计单位净值,五分钟涨速(乘100后的),涨跌幅(乘100后的),日期"
银行汇率
中国银行当然牌价:
http://hq.sinajs.cn/?list=h_RMBUSD,h_RMBGBP,h_RMBHKD
返回的数据如下:
var hq_str_h_RMBUSD="美元,635.080,629.990,637.620,637.620,636.070,2015-09-20,10:30:00";
var hq_str_h_RMBGBP="英镑,985.300,954.890,992.220,992.220,989.980,2015-09-20,10:30:00";
var hq_str_h_RMBHKD="港币,81.950,81.290,82.260,82.260,82.070,2015-09-20,10:30:00";
各个字段的含义:
var 代码="名称,点位,涨跌额,涨跌幅,日期时间"
黄金行情
请求黄金行情的接口:
http://hq.sinajs.cn/list=SGE_AG999
返回的数据如下:
var hq_str_SGE_AG999="AG999,白银999,Ag99.9,--,--,--,--,--,--,--,--,--,--,--,--,--,2015-09-20 20:00:00,0.00%";
各个字段的含义:
var 代码="代码,品种,显示名称,最新价,均价,昨日均价,开盘价,最高价,最低价,昨收价,买价,卖价,买入量,卖出量,总成交量,总成交额,更新时间,涨跌幅"
websocket 接口
如果程序要实时刷新股票行情数据,代码里面需要轮询HTTP接口,但每次请求的时候,HTTP Header占了大部分流量,真正的有效数据其实很小,为了更高效的刷新行情,本文提及的部分接口也提供了WS协议,可以由服务器主动推送每次更新后的数据。
有话要说