Intro
Although articles have been written about calling security quote API of Sina.com . I'd like to take a dive into it via Python and try to setup a OO implementation. Sina.com API will offer 5-th market depth prices of Chinese Shanghai / Shenzhen Exchange Stock in real time style .
API structure
calling Sina.com Stock API can be straightforward , just type "http://hq.sinajs.cn/list=sh601012" in browser address . call back will shown as below ( click to enlarge ):
Cracking this call back is a bit trick ,but thanks to this . so I just translate to English . the return from calling can be easily consume by Python split function .
in the calling url , ”szxxxxxx“ / "shxxxxxx", "sz" stand for Shenzhen Exchange Market , "sh" stand for Shanghai Exchange Market . “xxxxx” stand for the security code . For other return string :
1: ”27.55″, today open price
2: ”27.25″, yesterday close price
3: ”26.91″, current price
4: ”27.55″, today highest price
5: ”26.20″, today lowest price
6: ”26.91″, bid price
7: ”26.92″, ask price
8: ”22114263″, trading volume
9: ”589824680″, RMB amount of trading volume
10: ”4695″,Bid 1 volume, number of stock that investor willing to buy
11: ”26.91″,Bid 1 price
12: ”57590″,Bid 2 volume
13: ”26.90″,Bid 2 price
14, 15 for bid 3 volume / price, 16,17 for bid 4 volume /price , 18,19 for bid 5 volume/price
20, ask 1 volume ,21, ask price , same for the field 22/23,24/25,26/27,28/29
30: ”2008-01-11″, date
31: ”15:05:32″, time
Implement in Python Class
Grab data from Sina Web API using python lib urlib :
>>> import urllib
>>> f = urllib.urlopen("http://hq.sinajs.cn/list=sz300301")
>>> f.read()
'var hq_str_sz300301="\xb3\xa4\xb7\xbd\xd5\xd5\xc3\xf7,7.250,7.270,8.000,8.000,7.200,8.000,0.000,16847560,128728143.620,1523956,8.000,25442,7.990,4400,7.980,4400,7.970,4400,7.960,0,0.000,0,0.000,0,0.000,0,0.000,0,0.000,2012-12-26,15:05:52,00";\n'
OK, I want to offer a shortcut instead of talking much about unicode in Python , just make sure there is one declaration line in top of your script will be good .
# -*- coding:utf8 -*-
Construct Python Class
For most data provided from web server is quite strait-forward and self explained .
import re
import datetime
class Tick():
def __init__(self,header,a,b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,z1,z2,z3,z4,dt,tm ):
self.exchange = re.search('.*_(\w{2})\d+',header).group(1) # exchange code , sz for Shenzhen and sh for Shanghai
self.code = re.search('.*_\w{2}(\d+)',header).group(1) # security code
self.name = re.search('.*_\w{2}\d+=\"(.*)',header).group(1) # sercirity name
self.open_price = float(a) # open price for stock
self.y_close_price = float(b) # yesterday close price
self.current_price = float(c) # current stock price
self.t_range = ( float(d), float(e) ) # lowest price for d, and highest for e
self.bid_ask = ( float(f), float(g) ) # bid / ask price
self.vol = int(h) # trading volume
self.amount = float(j) # trading total amount
self.bids = [(int(k),float(l)),(int(m),float(n)),(int(o),float(p)),(int(q),float(r)),(int(s),float(t))] # market depth for bid
self.asks = [(int(u),float(v)),(int(w),float(x)),(int(y),float(z)),(int(z1),float(z2)),(int(z3),float(z4))] # market depth for ask
self.time_stamp = datetime.datetime.strptime(dt+" "+tm, "%Y-%m-%d %H:%M:%S") # parse string into timestamp
Now ,all raw string data can be interpreted as string/ integer/ float /date time etc . Done ,Cheers !
>>> import urllib
>>> f = urllib.urlopen("http://hq.sinajs.cn/list=sz300301")
>>> s = f.read()
>>> t = Tick(*(s.split(",")[:-1]))
>>> t
<__main__.Tick instance at 0x02A11FD0>
>>> t.open_price
8.3
>>> t.bids
[(1700, 8.45), (4850, 8.43), (16700, 8.42), (10900, 8.41), (46500, 8.4)]
>>>
0 Comments.