財經(jīng)行情系統(tǒng)接口
某知名門戶網(wǎng)站財經(jīng)頻道的行情系統(tǒng)髓抑,采用了自行開發(fā)的分布式K-V NoSQL引擎嘴拢,支持http和websocket協(xié)議,支持高并發(fā)胳喷、短延時的行情展示能力湃番。
注意,本文公布的所有行情接口僅限于個人興趣研究之目的吭露,任何人不得對接口實施惡意攻擊或商業(yè)性用途吠撮,商業(yè)盈利性應用本文行情接口應取得SINA公司書面許可!
股票實時行情
比如讲竿,獲取浦發(fā)銀行的實時行情接口:
http://hq.sinajs.cn/list=sh600000
返回的數(shù)據(jù)如下所示:
var hq_str_sh600000="浦發(fā)銀行,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 代碼 =“證券簡稱,今日開盤價,昨日收盤價,最近成交價,最高成交價,最低成交價,買入價,賣出價,成交數(shù)量,成交金額,買數(shù)量一,買價位一,買數(shù)量二,買價位二,買數(shù)量三 ,買價位三,買數(shù)量四,買價位四,買數(shù)量五,買價位五,賣數(shù)量一,賣價位一,賣數(shù)量二,賣價位二,賣數(shù)量三,賣價位三,賣數(shù)量四,賣價位四,賣數(shù)量五,賣價位五,行情日期,行情時間,停牌狀態(tài)”
如果嫌上面的接口返回字段太多泥兰,可以使用簡版接口:
http://hq.sinajs.cn/list=s_sh600000
在股票代碼前面加上"s_"即可;
返回的數(shù)據(jù)如下:
var hq_str_s_sh600000="浦發(fā)銀行,14.91,-0.12,-0.80,219647,32741";
各個字段的含義:
var 代碼=“證券簡稱,最新價,漲跌額,漲跌幅,成交量,成交額”
list參數(shù)支持批量獲取多個股票的行情题禀,也包括B股鞋诗、港股、美股迈嘹,例如:
http://hq.sinajs.cn/list=sh600000,sz200625,rt_hk01988,gb_jd
返回的數(shù)據(jù)如下:
var hq_str_sh600000="浦發(fā)銀行,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="美股中文名稱,最新價,漲跌幅,數(shù)據(jù)更新時間(北京時間),漲跌額,開盤價,最高價,最低價,52周最高價,52周最低價,成交量(單位股),平均成交量(單位股),市值(單位元),每股收益,市盈率,fpe,貝塔系數(shù),股息,收益率,總股本(單位元),instown,盤前盤后價,盤前盤后漲跌幅,盤前盤后漲跌額,盤前盤后數(shù)據(jù)更新時間(美東),盤中數(shù)據(jù)更新時間(美東),昨收價,盤前盤后成交量"
美股靜態(tài)數(shù)據(jù)接口:
http://hq.sinajs.cn/list=gb_jd_i
返回的數(shù)據(jù)如下:
var hq_str_gb_jd_i="O,e-commerce discretionary,19.0000";
各個字段的含義:
gb_symbol_i=交易市場(O,N,A),行業(yè),發(fā)行價
注:O=納斯達克交易所,N=紐交所吹害,A=美國交易所
此外螟凭,該接口還支持兩個可選的參數(shù):func 和 format,例如:
http://hq.sinajs.cn/format=js&func=callback();&list=sh600000
其中它呀,
format參數(shù)有兩個取值:js和text螺男,且js是默認的格式棒厘;
func參數(shù)是供給前端回調使用,原樣輸出下隧;
A股分時數(shù)據(jù)
請求個股一天的分時數(shù)據(jù)奢人,例如:
http://hq.sinajs.cn/list=ml_sh600000
返回的是base64編碼的字符串,從頭開始每16個字符對應1分鐘的數(shù)據(jù)淆院,將其解碼成3個int型整數(shù)何乎,依次為均價,實時價格迫筑,和成交量宪赶,其中均價和價格需要除以1000。
下面用Python代碼演示如何解碼接口返回的字符串:
import reimport base64import urllib2import 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() - 1whilestart < 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)
環(huán)球股指
還可以請求全球主要市場的股票指數(shù):
http://hq.sinajs.cn/list=gb_$ixic,gb_$dji,gb_$inx,hkHSI,nb_UKX,nb_NKY
返回的數(shù)據(jù)如下:
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="標普指數(shù),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,恒生指數(shù),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指數(shù),6122.81,6104.1099,2015-09-21,11:20:00";
var hq_str_nb_NKY="日經(jīng)225指數(shù),18070.211,18422,2015-09-18,15:00:00";
各個字段的含義:
var 代碼="名稱脯燃,點位,漲跌額蒙保,漲跌幅辕棚,日期時間"
財務數(shù)據(jù)
除了成交價、成交量這些實時行情數(shù)據(jù)以外邓厕,接口也能提供一些財務數(shù)據(jù)逝嚎,例如:
http://hq.sinajs.cn/list=sh600000_i
返回的數(shù)據(jù):
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個季度攤薄每股收益之和,今年迄今為止的每股收益,每股凈資產(chǎn),過去5個交易日平均每分鐘成交量,總股本 (萬股),流通股本(萬股),流通A股 (萬股),流通B股 (萬股),計價單位(CNY,US,HK) ,最近年度凈利潤,q4_profit_sum,發(fā)行價,狀態(tài)【0-無該記錄 1-上市正常交易 2-未上市 3-退市】, 最近一個報告期的凈資產(chǎn)收益率(%),最近一個報告期的主營收入(億元),最近一個報告期的凈利潤(億元)”
股指期貨行情
例如详恼,請求IF1510合約的行情:
http://hq.sinajs.cn/list=CFF_RE_IF1510
返回的數(shù)據(jù):
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_合約代碼=“今開盤,最高價,最低價, 最新價,成交量,成交金額,持倉量,今收盤,今結算,漲停板價,跌停板價,昨虛實度,今虛實度, 昨收盤,昨結算,昨持倉量,申買價一,申買量一,申買價二,申買量二, 申買價三,申買量三,申買價四,申買量四,申買價五,申買量五,申賣價一,申賣量一, 申賣價二,申賣量二,申賣價三,申賣量三,申賣價四,申賣量四,申賣價五,申賣量五,交易日,行情時間,行情時間毫秒”
基金行情數(shù)據(jù)
該接口包括分級基金补君、ETF,例如:
http://hq.sinajs.cn/list=f_502004,f_502005,f_510050,f_510710
返回的數(shù)據(jù)如下:
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 基金代碼="<基金簡稱>,<最新凈值>,<累計凈值>,<昨日凈值>,<凈值日期>,<基金規(guī)模(億份)>"
開放基金的行情接口:
http://hq.sinajs.cn/list=fu_150181,fu_150182,fu_150019
返回的數(shù)據(jù)如下:
var hq_str_fu_150181="富國中證軍工指數(shù)分級A,15:04:00,1.0220,1.0220,1.0850,0,0,2015-09-21";
var hq_str_fu_150182="富國中證軍工指數(shù)分級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
返回的數(shù)據(jù)如下:
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
返回的數(shù)據(jù)如下:
var hq_str_SGE_AG999="AG999,白銀999,Ag99.9,--,--,--,--,--,--,--,--,--,--,--,--,--,2015-09-20 20:00:00,0.00%";
各個字段的含義:
var 代碼="代碼,品種,顯示名稱劲厌,最新價膛薛,均價,昨日均價脊僚,開盤價相叁,最高價遵绰,最低價,昨收價,買價增淹,賣價椿访,買入量,賣出量虑润,總成交量成玫,總成交額,更新時間拳喻,漲跌幅"
websocket 接口
如果程序要實時刷新股票行情數(shù)據(jù)哭当,代碼里面需要輪詢HTTP接口,但每次請求的時候冗澈,HTTP Header占了大部分流量钦勘,真正的有效數(shù)據(jù)其實很小,為了更高效的刷新行情亚亲,本文提及的部分接口也提供了WS協(xié)議彻采,可以由服務器主動推送每次更新后的數(shù)據(jù)。