Access to the real-time data of China's 50ETF options with the interface of sina finance(1)

in #option7 years ago (edited)

微信截图_20171129164039.png

通过新浪财经的接口获取中国50ETF期权实时行情数据

Access to the real-time data of China 50ETF options with the interface of sina finance

由于交易软件的有关期权组合的收益率绘图、希腊字母绘图和隐波绘制并不完善,使用新浪财经的接口绘制这些图以辅助期权交易,这样的工具对交易员来讲很重要。
Because the existed trade softwares for options are not perfect in ploting part, it's hard to get resonable portfolio payoff graphs, Greeks graphs and the implied volatility graphs, I used a sina finance interface to plot these graphs.

import requests
import json
res = requests.get("http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName")
res.text

u'{"result":{"status":{"code":0},"data":{"cateList":["50ETF","50ETF"],"contractMonth":["2017-12","2017-12","2018-01","2018-03","2018-06"],"stockId":"510050","cateId":"510050C1712A02250"}}}'

b=eval(res.text)
b['result']['data']['contractMonth']

['2017-12', '2017-12', '2018-01', '2018-03', '2018-06']

# to get a cleaned month list
c = b['result']['data']['contractMonth']
c = list(set(c))
c.sort()
c

['2017-12', '2018-01', '2018-03', '2018-06']

monthi = '2017-12'
res = requests.get("http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getRemainderDay?date="+monthi)
res.text

u'{"result":{"status":{"code":0},"data":{"expireDay":"2017-12-27","remainderDays":27,"stockId":"510050","cateId":"510050C1712A02250","zhulikanzhang":"","zhulikandie":""}}}'

b=eval(res.text)
expireDay = b['result']['data']['expireDay']
remainderDays = b['result']['data']['remainderDays']
expireDay,remainderDays

('2017-12-27', 27)

monthi = '1712'
res = requests.get("http://hq.sinajs.cn/list=OP_UP_510050"+monthi)
res.text

u'var hq_str_OP_UP_5100501712="CON_OP_10000897,CON_OP_10000887,CON_OP_10000888,CON_OP_10000889,CON_OP_10000890,CON_OP_10000891,CON_OP_10000899,CON_OP_10000915,CON_OP_10000923,CON_OP_10000931,CON_OP_10000951,CON_OP_10000959,CON_OP_10000967,CON_OP_10000983,CON_OP_10001085,CON_OP_10001007,CON_OP_10001086,CON_OP_10001037,CON_OP_10001087,CON_OP_10001045,CON_OP_10001088,CON_OP_10001089,CON_OP_10001053,CON_OP_10001061,CON_OP_10001067,";\n'

conList = res.text.split('"')[1].split(',')
conList.remove('')
conList

[u'CON_OP_10000897',
u'CON_OP_10000887',
u'CON_OP_10000888',
u'CON_OP_10000889',
u'CON_OP_10000890',
u'CON_OP_10000891',
u'CON_OP_10000899',
u'CON_OP_10000915',
u'CON_OP_10000923',
u'CON_OP_10000931',
u'CON_OP_10000951',
u'CON_OP_10000959',
u'CON_OP_10000967',
u'CON_OP_10000983',
u'CON_OP_10001085',
u'CON_OP_10001007',
u'CON_OP_10001086',
u'CON_OP_10001037',
u'CON_OP_10001087',
u'CON_OP_10001045',
u'CON_OP_10001088',
u'CON_OP_10001089',
u'CON_OP_10001053',
u'CON_OP_10001061',
u'CON_OP_10001067']

monthi = '1712'
res = requests.get("http://hq.sinajs.cn/list=OP_DOWN_510050"+monthi)
res.text

u'var hq_str_OP_DOWN_5100501712="CON_OP_10000898,CON_OP_10000892,CON_OP_10000893,CON_OP_10000894,CON_OP_10000895,CON_OP_10000896,CON_OP_10000900,CON_OP_10000916,CON_OP_10000924,CON_OP_10000932,CON_OP_10000952,CON_OP_10000960,CON_OP_10000968,CON_OP_10000984,CON_OP_10001090,CON_OP_10001008,CON_OP_10001091,CON_OP_10001038,CON_OP_10001092,CON_OP_10001046,CON_OP_10001093,CON_OP_10001094,CON_OP_10001054,CON_OP_10001062,CON_OP_10001068,";\n'

conList = res.text.split('"')[1].split(',')
conList.remove('')
conList

[u'CON_OP_10000898',
u'CON_OP_10000892',
u'CON_OP_10000893',
u'CON_OP_10000894',
u'CON_OP_10000895',
u'CON_OP_10000896',
u'CON_OP_10000900',
u'CON_OP_10000916',
u'CON_OP_10000924',
u'CON_OP_10000932',
u'CON_OP_10000952',
u'CON_OP_10000960',
u'CON_OP_10000968',
u'CON_OP_10000984',
u'CON_OP_10001090',
u'CON_OP_10001008',
u'CON_OP_10001091',
u'CON_OP_10001038',
u'CON_OP_10001092',
u'CON_OP_10001046',
u'CON_OP_10001093',
u'CON_OP_10001094',
u'CON_OP_10001054',
u'CON_OP_10001062',
u'CON_OP_10001068']

# 5.获得实时期权行情
# 5.get the real-time option data
# http://hq.sinajs.cn/list=CON_OP_10000898
coni = 'CON_OP_10001038'
res = requests.get("http://hq.sinajs.cn/list="+coni)
res.text

u'var hq_str_CON_OP_10001038="70,0.0692,0.0692,0.0695,37,65479,41.51,2.8960,0.0489,0.0501,0.3379,0.0001,0.0706,4,0.0703,30,0.0700,14,0.0698,1,0.0695,37,0.0692,70,0.0691,58,0.0690,365,0.0688,13,0.0686,10,2017-11-30 15:00:00,1,E 01,EBS,510050,50ETF\u6cbd12\u67082896A,57.06,0.0734,0.0455,52227,31449602.53,A";\n'

# 上一条中返回信息各个字段的意思:var hq_str_CON_OP_代码=“买量(0),买价,最新价,卖价,卖量,持仓量,涨幅,行权价,昨收价,开盘价,涨停价,跌停价(11), 申卖 价五,申卖量五,申卖价四,申卖量四,申卖价三,申卖量三,申卖价二,申卖量二,申卖价一,申卖量一,申买价一,申买量一 ,申买价二,申买量二,申买价三,申买量三,申买价四,申买量四,申买价五,申买量五,行情时间,主力合约标识,状态码, 标的证券类型,标的股票,期权合约简称,振幅(38),最高价,最低价,成交量,成交额
# attributes meaning(from left to right): bid volume, bid price, price, ask price, ask volume, position amount, increase, strike price, previous close price, open price...
varList = res.text.split('"')[1].split(',')
XPrice = float(varList[7])
OpPrice = float(varList[2])
OpShortName = varList[37]
print XPrice,OpPrice,OpShortName

2.896 0.0692 50ETF沽12月2896A

OpShortName==u'50ETF沽12月2896A'

True

# 6.50ETF实时行情
# 6.the underlying 50etf real-time data
# http://hq.sinajs.cn/list=sh510050
res = requests.get("http://hq.sinajs.cn/list=sh510050")
res.text

u'var hq_str_sh510050="50ETF,2.880,2.890,2.858,2.898,2.850,2.854,2.855,635741908,1825466242.000,120600,2.854,118300,2.853,541600,2.852,52000,2.851,245000,2.850,378700,2.855,279100,2.856,1905000,2.858,279100,2.859,940800,2.860,2017-11-30,15:00:00,00";\n'

#1.股票名 Stock 
#2.今开 Open
#3.昨收 PreClose
#4.现价 Price
#5.最高 Highest
#6.最低 Lowest
#7.竞买价 Bid
#8.竞卖价 Ask
#9.成交手数 Amount
#10.成交额 Turnover
#11.买一量 Buy1Volume
#12.买一价 Buy1Price
#13.买二量 Buy2Volume
#14.买二价 Buy2Price
#15.买三量 Buy3Volume
#16.买三价 Buy3Price
#17.买四量 Buy4Volume
#18.买四价 Buy4Price
#19.买五量 Buy5Volume
#20.买五价 Buy5Price
#21.卖一量 Sell1Volume
#22.卖一价 Sell1Price
#23.卖二量 Sell2Volume
#24.卖二价 Sell2Price
#25.卖三量 Sell3Volume
#26.卖三价 Sell3Price
#27.卖四量 Sell4Volume
#28.卖四价 Sell4Price
#29.卖五量 Sell5Volume
#30.卖五价 Sell5Price
#31.日期 Date
#32.时间 Time
varList = res.text.split('"')[1].split(',')
Price = float(varList[3])
Price

2.858

# 7.隐含波动率和希腊字母
# 7.Implied volatility and Greeks
# http://hq.sinajs.cn/list=CON_SO_10001038

coni = '10001038'
res = requests.get("http://hq.sinajs.cn/list=CON_SO_"+coni)
res.text

u'var hq_str_CON_SO_10001038="50ETF\u6cbd12\u67082896A,,,,52227,-0.6168,4.0835,-0.1685,0.2967,0.1706,0.0734,0.0455,510050P1712A02950,2.8960,0.0692,0.054,A";\n'

# 字段:期权合约简称,,,,成交量,Delta,Gamma,Theta,Vega,隐含波动率,最高价,最低价,交易代码,行权价,最新价,理论价值。
# attributes: opShortName,,,,amount,opDelta,opGamma,opTheta,opVega,opIV,highest,lowest,transactionID,xPrice,opPrice,theo_price
varList = res.text.split('"')[1].split(',') 
opShortName = varList[0]
opDelta = float(varList[5])
opGamma = float(varList[6])
opTheta = float(varList[7])
opVega = float(varList[8])
opIV = float(varList[9])
xPrice = float(varList[13])
opPrice = float(varList[14])


print opShortName,opDelta,opGamma,opTheta,opVega,opIV,xPrice,opPrice

50ETF沽12月2896A -0.6168 4.0835 -0.1685 0.2967 0.1706 2.896 0.0692

Sort:  

新浪财经的数据接口,实时的。正在学

Coin Marketplace

STEEM 0.18
TRX 0.14
JST 0.030
BTC 59190.13
ETH 3187.58
USDT 1.00
SBD 2.45