Here we introduce the use of free and open source Pytdx interface to obtain real-time market data and store it in the database in real time (mongodb/myslq).
Pytdx is an implementation of market data interface similar to TradeX developed in pure Python language. It provides real-time market data of various stocks / futures / options and so on.
The types of data provided include:
market category name short_name
0 1 1 temporary shares TP
1 4 12 Zhengzhou Commodity Option OZ
2.5.12 Dalian Commodity Option OD
3 6 12 Shanghai Commodity Options
4.7.12 CFSE option OJ
5 8 12 Shanghai individual stock option QQ
6 9 12 Shenzhen individual stock options SQ
7 10 4 basic exchange rate FE
8 11 4 cross exchange rate FX
9 12 5 international index WI
10 13 3 international precious metals GO
11 14 3 London Metal LM
12 15 3 London petroleum IP
13 16 3 New York commodity CO
14 17 3 New York oil NY
15 18 3 Chicago Valley CB
16 19 3 Tokyo Industrial Products Co., Ltd
17 20 3 New York Futures NB
18 27 5 Hong Kong Index FH
19 28 3 Zhengzhou Commodity QZ
20 29 3 Dalian Commodity QD
21 30 3 Shanghai Futures QS
22 31 2 Hong Kong mainboard KH
23 33 8 open end fund FU
24 34 9 Monetary Fund FB
25 35 8 brokerage financial products LC
26 36 9 brokerage monetary products LB
27 37 11 global index (static) FW
28 38 10 macro index HG
29 39 3 Malay futures
30 40 11 China concept stocks CH
31 41 11 MG, a well-known US stock company
32 43 1 B share to H share HB
33 44 1 share transfer system SB
34 46 11 Shanghai Gold SG
35 47 3 CICC futures CZ
36 48 2 HK gem KG
37 49 2 HK fund KT
38 50 3 Bohai commodity BH
39 56 8 sunshine private equity fund TA
40 57 8 brokerage collective wealth management TB
41 58 9 brokerage money management TC
42 60 3 main futures contracts MA
43 62 5 China Securities Index ZZ
44 70 5 expansion plate index UZ
45 71 2 HK Stock connect GH
46 102 5 national securities index GZ
Support python2 and python3 programming languages.
First, we need to introduce
from pytdx.exhq import *
from pytdx.hq import *
1, List of standard api methods
from pytdx.hq import *
1: get the stock market
You can get the market information of multiple stocks
A list needs to be passed in. Each list is composed of a market code and a stock code [(market code 1, stock code 1), (market code 2, stock code 2)... (market code n, stock code n)]
Such as API get_ security_ quotes([(0, ‘000001’), (1, ‘600300’)])
2: get k line
K line type: 0.5 minutes K line 1.15 minutes K line 2.30 minutes K line 3.1 hours K line 4 days K line 5 weeks K line June K line 7.1 minutes 81 minutes K line 9 days K line 10 seasons K line 11 years K line
api.get_security_bars(9,0, '000001', 4, 3)
3: get the number of stocks in the market
api.get_security_count(0)
4: get stock list
api.get_security_list(1, 0)
5: get index k line
api.get_index_bars(9,1, '000001', 1, 2)
6: query time-sharing Market
api.get_minute_time_data(1, '600300')
7: query historical time-sharing Market
api.get_history_minute_time_data(TDXParams.MARKET_SH, '600300', 20161209)
8: query transaction by transaction
api.get_transaction_data(TDXParams.MARKET_SZ, '000001', 0, 30)
9: query the history of transaction by transaction
api.get_history_transaction_data(TDXParams.MARKET_SZ, '000001', 0, 10, 20170209)
10: query company information directory
api.get_company_info_category(TDXParams.MARKET_SZ, '000001')
11: read company information details
api.get_company_info_content(0, '000001', '000001.txt', 0, 100)
12: read ex right and ex dividend information
api.get_xdxr_info(1, '600300')
13: read financial information
api.get_finance_info(0, '000001')
14: read k-line information
get_k_data('000001','2017-07-03','2017-07-10')
15: Read plate information
api.get_and_parse_block_info(TDXParams.BLOCK_SZ)
2, Extended quotation interface API
from pytdx.exhq import *
1: Get market code
api.get_markets()
2: Query code list
api.get_instrument_info(0, 100)
3: Query the quantity of goods in the market
api.get_instrument_count()
4: Query the market of the fifth gear
api.get_instrument_quote(47, "IF1709")
5: Query time-sharing Market
api.get_minute_time_data(47, "IF1709")
6: Query historical time-sharing Market
api.get_history_minute_time_data(31, "00020", 20170811)
7: Query k-line data
api.get_instrument_bars(TDXParams.KLINE_TYPE_DAILY, 8, "10000843", 0, 100)
8: Query transaction by transaction
api.get_transaction_data(31, "00020")
api.get_history_transaction_data(47, "IFL0", 20170810, start=1800)
9: Query history transaction by transaction
api.get_history_transaction_data(31, "00020", 20170810)
3, Other functions
Multithreading support, heartbeat package, exception throwing, reconnection mechanism, as well as reading the daily K-line, historical professional financial data and transaction related (such as creating orders, canceling orders, etc.), please refer to the reference https://github.com/rainx/pytdx/issues/5
4, Application case source code
The following is a python method to obtain the real-time quotes of stocks / 50ETF/300ETF and their stock options in real time:
def fetcHQRisk(self, isSaveSTkRealtime=False, isSaveOptRealtime=False): optList= self.contraManager.contrOptIdList riskM=RISKMAN.RiskMan() rate=4. hv=20. #default if self.db=='mysql': try: sql='select * from %s \ where tradeDate in (select max(tradeDate) from %s);'%(C.OPT_HV,C.OPT_HV) res=dbcom.select_tuple_dict(sql) hvDf=pd.DataFrame(list(res)) hv=(hvDf[hvDf['xmode']=='yz'][C.HVX]).values[0] hv=round(hv*100.,1) except Exception,e: print (e) elif self.db=='mongo': hv=mdbcom.find(C.STK_HV,sortListOfTuple=[('date',-1)]) if hv is not None and len(hv)>0: hv=hv[0][C.HVX] hv=round(hv*100.,1) api_hq=None api_exhq=None try: api_hq = TdxHq_API() with self.isConnected(api_hq,type='hq',verbose=True) as api_hq: quote_hq=api_hq.get_security_quotes([(self.market_good, self.good[:-5])]) spotPrice=quote_hq[0]['price']/10. #add on 2019-04-17 #daily 50etf info:today's cummulative info. cummulative!!! #********************************* etf_open=quote_hq[0]['open']/10. etf_high=quote_hq[0]['high']/10. etf_low=quote_hq[0]['low']/10. etf_last_close=quote_hq[0]['last_close']/10. #today's cummulative info etf_vol=quote_hq[0]['vol'] etf_amount=quote_hq[0]['amount'] #********************************* print(spotPrice) if quote_hq is not None or spotPrice>0.000001: if isSaveSTkRealtime: ''' category-> K Line type 0 5 minute K Line 1 15 minute K Line 2 30 minute K Line 3 1 hour K Line 4 day K Line 5 week K Line 6 month K Line 7 1 minute 8 1 minute K Line 9 day K Line 10 season K Line 11 year K Line market -> Market code 0:Shenzhen, 1:Shanghai stockcode -> Securities code; start -> Specified range start position; count -> User to request K Number of lines, max. 800 ''' stk_1min_K=api_hq.get_security_bars(8, 1, self.good[:-5], 0, 10) nowTime=xtool.nowTime() stk_1min_K_df=pd.DataFrame(stk_1min_K) stk_1min_K_df['xtime']=nowTime hr_min=[t[-5:] for t in stk_1min_K_df['datetime']] stk_1min_K_df['_id']=hr_min record=stk_1min_K_df.to_dict(orient='record') mdbcom.saveBatch(C.STK_MINS,record) stk_1min_K_df['_id']=stk_1min_K_df['datetime'] stk_1min_K_df["tradeTime"]= stk_1min_K_df['datetime'] stk_1min_K_df["tradeMins"]= hr_min stk_1min_K_df["tradeDate"]= [t[:10] for t in stk_1min_K_df['datetime']] stk_1min_K_df["volume"]= stk_1min_K_df["vol"] stk_1min_K_df=stk_1min_K_df[[ "_id" , "tradeMins" , "volume" , "tradeDate", "high" , "amount", "tradeTime" , "low", "close" , "open" ]] # 1-min K fig, vol NOT cummulative mdbcom.saveBatch('etf50_date_mins',stk_1min_K_df.to_dict(orient='record')) api_exhq = TdxExHq_API() with self.isConnected(api_exhq, type='exhq', verbose=True) as api_exhq: for optID in optList: try: quote_exhq=api_exhq.get_instrument_quote(self.market_opt, optID) settlePrice=quote_exhq[0]['price'] settlePrice_high=settlePrice settlePrice_low=settlePrice settlePrice_open=settlePrice if self.all_settlePrice_high.has_key(optID): if settlePrice>self.all_settlePrice_high[optID]: self.all_settlePrice_high[optID]=settlePrice else: settlePrice_high=self.all_settlePrice_high[optID] else: self.all_settlePrice_high[optID]=settlePrice if self.all_settlePrice_low.has_key(optID): if settlePrice<self.all_settlePrice_low[optID]: self.all_settlePrice_low[optID]=settlePrice else: settlePrice_low=self.all_settlePrice_low[optID] else: self.all_settlePrice_low[optID]=settlePrice if self.all_settlePrice_open.has_key(optID): pass else: self.all_settlePrice_open[optID]=settlePrice_open chicang=quote_exhq[0]['chicang'] if quote_exhq is None: xlog.info('no settlePrice') else: if isSaveOptRealtime: save=quote_exhq[0] save['_id']=optID save['xtime']=xtool.nowTime() mdbcom.saveOne(C.OPT_QUOTE,save) contractType=self.contraManager.contrDict['contractType'][optID] contractStatus=self.contraManager.contrDict['contractStatus'][optID] strikePrice=self.contraManager.contrDict['strikePrice'][optID] dayToExpire=self.contraManager.contrDict['dayToExpire'][optID] expDate=self.contraManager.contrDict['expDate'][optID] tickerSymbol=self.contraManager.contrDict['tickerSymbol'][optID] settlePrice_chg=0. iv=0. theoryPrice=0. delta=0. gamma=0. theta=0. vega=0. rho=0. if dayToExpire>0.5 and contractStatus=='L': chg_symbol="" pre_settlePrice=quote_exhq[0]['pre_close'] if pre_settlePrice >0.000001: settlePrice_chg =(settlePrice/pre_settlePrice-1.00)*100. if settlePrice_chg>0.000001: chg_symbol='#fa9e66',# #ee5734' elif settlePrice_chg<-0.000001: chg_symbol='#b6f050' else: chg_symbol='#666666' suffix_CP=None if contractType=='C': suffix_CP='x' elif contractType=='P': suffix_CP='y' if spotPrice*settlePrice<0.000001 or contractType is None or contractType=='None': xlog.info('no data') else: res=None res=riskM.calu_mibian(contractType, spotPrice, strikePrice, settlePrice, rate, dayToExpire, hv) if res is not None: iv=round(res['iv'],1) theoryPrice=round(res['theoryPrice'],4) delta=round(res['delta'],4) gamma=round(res['gamma'],4) theta=round(res['theta'],4) vega=round(res['vega'],4) rho=round(res['rho'],4) realtimeMsg={ optID+'_settlePrice_chg_'+suffix_CP:round(settlePrice_chg,1), optID+'_settlePrice_'+suffix_CP:round(settlePrice,4), optID+'_iv_'+suffix_CP:iv, optID+'_theoryPrice_'+suffix_CP:theoryPrice, optID+'_delta_'+suffix_CP:delta, optID+'_gamma_'+suffix_CP:gamma, optID+'_theta_'+suffix_CP:theta, optID+'_vega_'+suffix_CP:vega, optID+'_rho_'+suffix_CP:rho } keyList=realtimeMsg.keys() xtime=xtool.nowTime() xmins=xtime[:16] saveMongoDBMsg={ '_id': optID, 'optID': optID, 'tickerSymbol':tickerSymbol, 'contractType':contractType, 'contractStatus':contractStatus, 'strikePrice':strikePrice, 'dayToExpire':dayToExpire, 'expDate':expDate, 'spotPrice':spotPrice, 'settlePrice_chg':round(settlePrice_chg,1), 'settlePrice':round(settlePrice,4), 'chicang':chicang, 'pre_settlePrice':round(pre_settlePrice,4), 'iv':iv, 'theoryMargin':round(theoryPrice-settlePrice,4), 'theoryPrice':round(theoryPrice,4), 'delta':round(delta,4), 'gamma':round(gamma,4), 'theta':round(theta,4), 'vega':round(vega,4), 'rho':round(rho,4), 'etf_open':etf_open, 'etf_high':etf_high, 'etf_low':etf_low, 'etf_last_close':etf_last_close, 'etf_vol':etf_vol, 'etf_amount':etf_amount, 'settlePrice_high':settlePrice_high, 'settlePrice_low':settlePrice_low, 'settlePrice_open':settlePrice_open, 'xtime':xtime, 'xmins':xmins } mdbcom.saveOne(C.OPT_RISK,saveMongoDBMsg) saveMongoDBMsg_mins=saveMongoDBMsg.copy() saveMongoDBMsg_mins['_id']='%s_%s'%(xtime[11:16].replace(":",""),optID) mdbcom.saveOne(C.OPT_RISK_MINS,saveMongoDBMsg_mins) #change _id, and save to OPT_RISK_DATE_MINS saveMongoDBMsg_mins['_id']='%s_%s'%(xtime[:16].replace(" ","_"),optID) mdbcom.saveOne(C.OPT_RISK_DATE_MINS,saveMongoDBMsg_mins) if C.isDebug: saveMongoDBMsg['_id']= xtool.nowDate()+'_'+optID saveMongoDBMsg['tradeDate']=xtool.nowDate() saveMongoDBMsg['remark']=C.HVX+'r=4,mibian' mdbcom.saveOne(C.OPT_RISK_DAILY,saveMongoDBMsg) else: if xtool.nowOnlyTime()>='14:45:00': saveMongoDBMsg['_id']= xtool.nowDate()+'_'+optID saveMongoDBMsg['tradeDate']=xtool.nowDate() saveMongoDBMsg['remark']=C.HVX+'r=4,mibian' mdbcom.saveOne(C.OPT_RISK_DAILY,saveMongoDBMsg) xlog.info('%s fetch/sent/saved'%(optID)) time.sleep(interval) except Exception,e: xlog.error(e) time.sleep(interval) xlog.info('****************************************************************************') except Exception,e: xlog.error(e) self.sendEmail('hqserv fetcHQRisk load', e) finally: if api_hq is not None: api_hq.disconnect() if api_exhq is not None: api_exhq.disconnect()