def get_instrument_info(): api = TdxExHq_API(heartbeat=True) host = "180.153.18.176" #通信达的api地址 port = 7721 #通信达的连接端口 #开始连接通信达服务器 api.connect(host, port) insts = [] count = 500 curr_index = 0 while (True): insts_tmp = api.get_instrument_info(curr_index, count) if insts_tmp is None: break insts.extend(insts_tmp) curr_index = curr_index + len(insts_tmp) if len(insts_tmp) < count: break #查看通信达提供的市场列表 #print api.to_df(api.get_markets()) df_inst = api.to_df(insts) #这里笔者选择的美国知名公司列表, 所以market = 41 df_inst[df_inst['market'] == 41] #这里教程获取AAPL单一数据,如果需要全部数据可以使用列表循环下载整个数据 #笔者获取的是苹果公司最近300天交易日行情Day版 his_kline = api.get_instrument_bars(TDXParams.KLINE_TYPE_DAILY, 41, "AAPL", 0, 300) datadf = api.to_df(his_kline) #保存为csv格式,命名为APPL-demo/按照逗号分隔 datadf.to_csv(os.getcwd() + "/tdx_file/" + 'APPL-demo.csv', index=False, sep=',')
def QA_fetch_get_extensionmarket_info(ip=None, port=None): ip, port = get_extensionmarket_ip(ip, port) apix = TdxExHq_API() with apix.connect(ip, port): global extension_market_info extension_market_info = apix.to_df(apix.get_markets()) return extension_market_info
def __exhq_ping(self, ip, port): api = TdxExHq_API() with api.connect(ip, port, time_out=0.7): assert api.get_instrument_count() > 20000 api.disconnect() return True return False
def QA_fetch_get_future_day(code, start_date, end_date, frequence='day', ip=best_ip['future'], port=7727): '期货数据 日线' apix = TdxExHq_API() start_date = str(start_date)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) global extension_market_info extension_market_info = QA_fetch_get_future_list( ) if extension_market_info is None else extension_market_info with apix.connect(ip, port): code_market = extension_market_info.query('code=="{}"'.format(code)) data = pd.concat([ apix.to_df( apix.get_instrument_bars(_select_type(frequence), int(code_market.market), str(code), (int(lens / 700) - i) * 700, 700)) for i in range(int(lens / 700) + 1) ], axis=0) data = data.assign(date=data['datetime'].apply(lambda x: str(x[0:10]))).assign(code=str(code))\ .assign(date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(str(x)[0:10]))).set_index('date', drop=False, inplace=False) return data.drop( ['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)[start_date:end_date].assign( date=data['date'].apply(lambda x: str(x)[0:10]))
def QA_fetch_get_future_transaction(code, start, end, retry=2, ip=None, port=None): '期货历史成交分笔' ip, port = get_extensionmarket_ip(ip, port) apix = TdxExHq_API() global extension_market_list extension_market_list = QA_fetch_get_extensionmarket_list( ) if extension_market_list is None else extension_market_list real_start, real_end = QA_util_get_real_datelist(start, end) if real_start is None: return None real_id_range = [] with apix.connect(ip, port): code_market = extension_market_list.query('code=="{}"'.format(code)) data = pd.DataFrame() for index_ in range(trade_date_sse.index(real_start), trade_date_sse.index(real_end) + 1): try: data_ = __QA_fetch_get_future_transaction( code, trade_date_sse[index_], retry, int(code_market.market), apix) if len(data_) < 1: return None except Exception as e: QA_util_log_info('Wrong in Getting {} history transaction data in day {}'.format( code, trade_date_sse[index_])) else: QA_util_log_info('Successfully Getting {} history transaction data in day {}'.format( code, trade_date_sse[index_])) data = data.append(data_) if len(data) > 0: return data.assign(datetime=data['datetime'].apply(lambda x: str(x)[0:19])) else: return None
def connect(self): """ 连接API :return: """ # 创建api连接对象实例 try: if self.api is None or self.connection_status == False: self.strategy.writeCtaLog(u'开始连接通达信行情服务器') TdxFutureData.api = TdxExHq_API(heartbeat=True, auto_retry=True, raise_exception=True) # 选取最佳服务器 self.best_ip = self.select_best_ip() self.api.connect(self.best_ip['ip'], self.best_ip['port']) # 尝试获取市场合约统计 c = self.api.get_instrument_count() if c < 10: err_msg = u'该服务器IP {}/{}无响应'.format(self.best_ip['ip'], self.best_ip['port']) self.strategy.writeCtaError(err_msg) else: self.strategy.writeCtaLog(u'创建tdx连接, IP: {}/{}'.format(self.best_ip['ip'], self.best_ip['port'])) # print(u'创建tdx连接, IP: {}/{}'.format(self.best_ip['ip'], self.best_ip['port'])) TdxFutureData.connection_status = True # 更新 symbol_exchange_dict , symbol_market_dict self.qryInstrument() except Exception as ex: self.strategy.writeCtaLog(u'连接服务器tdx异常:{},{}'.format(str(ex), traceback.format_exc())) return
def __init__(self): self.cf = config self.tools = tdx_tools self.qihuo_api = TdxExHq_API() self.gupiao_api = TdxHq_API() self.cflen = 0 self.table = pt.PrettyTable()
def QA_fetch_get_future_realtime(code, ip=None, port=None): '期货实时价格' global best_ip if ip is None and port is None and best_ip['future']['ip'] is None and best_ip['future']['port'] is None: best_ip = select_best_ip() ip = best_ip['future']['ip'] port = best_ip['future']['port'] elif ip is None and port is None and best_ip['future']['ip'] is not None and best_ip['future']['port'] is not None: ip = best_ip['future']['ip'] port = best_ip['future']['port'] else: pass apix = TdxExHq_API() global extension_market_info extension_market_info = QA_fetch_get_future_list( ) if extension_market_info is None else extension_market_info __data = pd.DataFrame() code_market = extension_market_info.query('code=="{}"'.format(code)) with apix.connect(ip, port): __data = apix.to_df(apix.get_instrument_quote( int(code_market.market),code)) __data['datetime'] = datetime.datetime.now() # data = __data[['datetime', 'active1', 'active2', 'last_close', 'code', 'open', 'high', 'low', 'price', 'cur_vol', # 's_vol', 'b_vol', 'vol', 'ask1', 'ask_vol1', 'bid1', 'bid_vol1', 'ask2', 'ask_vol2', # 'bid2', 'bid_vol2', 'ask3', 'ask_vol3', 'bid3', 'bid_vol3', 'ask4', # 'ask_vol4', 'bid4', 'bid_vol4', 'ask5', 'ask_vol5', 'bid5', 'bid_vol5']] return _data.set_index('code', drop=False, inplace=False)
def __init__(self, **kwargs): try: default = settings.get('SERVER').get('EX')[0] self.bestip = config.get('BESTIP').get('EX', default) except ValueError: self.bestip = ('112.74.214.43', 7727) self.client = TdxExHq_API(**kwargs)
def QA_fetch_get_future_list(ip=best_ip['future'], port=7727): '期货代码list' apix = TdxExHq_API() with apix.connect(ip, port): num = apix.get_instrument_count() return pd.concat([apix.to_df( apix.get_instrument_info((int(num / 500) - i) * 500, 500)) for i in range(int(num / 500) + 1)], axis=0).set_index('code', drop=False)
def QA_fetch_get_extensionmarket_list(ip=None, port=None): '期货代码list' ip, port = get_extensionmarket_ip(ip, port) apix = TdxExHq_API() with apix.connect(ip, port): num = apix.get_instrument_count() return pd.concat([apix.to_df( apix.get_instrument_info((int(num / 500) - i) * 500, 500)) for i in range(int(num / 500) + 1)], axis=0).set_index('code', drop=False)
def xapi_x(retry_count=3): for _ in range(retry_count): try: api = TdxExHq_API(heartbeat=True) api.connect(ct._get_xxserver(), ct.X_PORT) except Exception as e: print(e) else: return api raise IOError(ct.NETWORK_URL_ERROR_MSG)
def __init__(self, host: Optional[str] = None): self._env = env = Env() env.read_env() tdx_host = host if host else env.str('TDX_HOST') self._api = TdxExHq_API(heartbeat=True, multithread=True) self._ip, self._port = tdx_host.split(':') self._server_tz = timezone('Asia/Shanghai') self._pill = Event() self._market_mapping = dict( zip(["SHFE", "CZCE", "DCE", "CFFEX", "US"], [30, 28, 29, 47, 74]))
def exhq(): ex_api = TdxExHq_API(auto_retry=True, raise_exception=False) try: is_tdx_ex_connect = ex_api.connect('106.14.95.149', 7727, time_out=30) except Exception as e: print('time out to connect to pytdx') print(e) if is_tdx_ex_connect is not False: # 失败了返回False,成功了返回地址 print('connect to pytdx extend api successful') else: ex_api = None return ex_api
def test_get_history_instrument_bars_range(): log.info("查询代码列表") api = TdxExHq_API(auto_retry=True) try: with api.connect('61.152.107.141', 7727): data = api.get_history_instrument_bars_range( 74, "BABA", 20170613, 20170620) assert data is not None assert type(data) is list assert len(data) > 0 except socket.timeout as e: pass
def connect(self, is_reconnect=False): """ 连接API :return: """ # 创建api连接对象实例 try: if self.api is None or not self.connection_status: self.write_log(u'开始连接通达信行情服务器') self.api = TdxExHq_API(heartbeat=True, auto_retry=True, raise_exception=True) # 选取最佳服务器 if is_reconnect or len(self.best_ip) == 0: self.best_ip = get_cache_json(TDX_FUTURE_CONFIG) last_datetime_str = self.best_ip.get('datetime', None) if last_datetime_str: try: last_datetime = datetime.strptime( last_datetime_str, '%Y-%m-%d %H:%M:%S') if (datetime.now() - last_datetime ).total_seconds() > 60 * 60 * 2: self.best_ip = {} except Exception as ex: # noqa self.best_ip = {} else: self.best_ip = {} if len(self.best_ip) == 0: self.best_ip = self.select_best_ip() self.api.connect(self.best_ip['ip'], self.best_ip['port']) # 尝试获取市场合约统计 c = self.api.get_instrument_count() if c < 10: err_msg = u'该服务器IP {}/{}无响应'.format( self.best_ip['ip'], self.best_ip['port']) self.write_error(err_msg) else: self.write_log(u'创建tdx连接, IP: {}/{}'.format( self.best_ip['ip'], self.best_ip['port'])) # print(u'创建tdx连接, IP: {}/{}'.format(self.best_ip['ip'], self.best_ip['port'])) self.connection_status = True # if not is_reconnect: # 更新 symbol_exchange_dict , symbol_market_dict # self.qryInstrument() except Exception as ex: self.write_log(u'连接服务器tdx异常:{},{}'.format(str(ex), traceback.format_exc())) return False return True
def QA_fetch_get_future_min(code, start, end, frequence='1min', ip=best_ip['future'], port=7727): '期货数据 分钟线' apix = TdxExHq_API() type_ = '' start_date = str(start)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) global extension_market_info extension_market_info = QA_fetch_get_future_list( ) if extension_market_info is None else extension_market_info if str(frequence) in ['5', '5m', '5min', 'five']: frequence, type_ = 0, '5min' lens = 48 * lens elif str(frequence) in ['1', '1m', '1min', 'one']: frequence, type_ = 8, '1min' lens = 240 * lens elif str(frequence) in ['15', '15m', '15min', 'fifteen']: frequence, type_ = 1, '15min' lens = 16 * lens elif str(frequence) in ['30', '30m', '30min', 'half']: frequence, type_ = 2, '30min' lens = 8 * lens elif str(frequence) in ['60', '60m', '60min', '1h']: frequence, type_ = 3, '60min' lens = 4 * lens if lens > 20800: lens = 20800 with apix.connect(ip, port): code_market = extension_market_info.query('code=="{}"'.format(code)) data = pd.concat([ apix.to_df( apix.get_instrument_bars(frequence, int(code_market.market), str(code), (int(lens / 700) - i) * 700, 700)) for i in range(int(lens / 700) + 1) ], axis=0) data = data\ .assign(datetime=pd.to_datetime(data['datetime']), code=str(code))\ .drop(['year', 'month', 'day', 'hour', 'minute'], axis=1, inplace=False)\ .assign(date=data['datetime'].apply(lambda x: str(x)[0:10]))\ .assign(date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(x)))\ .assign(time_stamp=data['datetime'].apply(lambda x: QA_util_time_stamp(x)))\ .assign(type=type_).set_index('datetime', drop=False, inplace=False)[start:end] return data.assign(datetime=data['datetime'].apply(lambda x: str(x)))
def __exhq_list(self): self.__paralle_exhq_ping() api = TdxExHq_API() ip, port = self.__ex.get() with api.connect(ip, port): df = list() nums = api.get_instrument_count() for i in range((nums // 500 + (1 if nums % 500 > 0 else 0)) + 1): df.append(api.to_df(api.get_instrument_info(i * 500, 500))) self.__ex.put((ip, port)) df = pandas.concat(df, sort=False).set_index('code') return df return None
def __init__(self, market, code, date, start=None, ip='140.207.226.39', port=7722): self.market = market self.code = code self.date = date self.start = start self.api = TdxExHq_API() connect = self.api.connect(ip, port)
def TdxexInit(ip='106.14.95.149', port=7727): global tdxapiex tdxapiex = TdxExHq_API(auto_retry=True, raise_exception=False) try: is_tdx_ex_connect = tdxapiex.connect(ip, port, time_out=30) except Exception as e: #print('time out to connect to pytdx') print(e) if is_tdx_ex_connect is not False: # 失败了返回False,成功了返回地址 pass #print('connect to pytdx extend api successful') else: tdxapiex = None return tdxapiex
def QA_fetch_get_future_transaction_realtime(code, ip=None, port=None): '期货历史成交分笔' ip, port = get_extensionmarket_ip(ip, port) apix = TdxExHq_API() global extension_market_list extension_market_list = QA_fetch_get_extensionmarket_list( ) if extension_market_list is None else extension_market_list code_market = extension_market_list.query('code=="{}"'.format(code)) with apix.connect(ip, port): data = pd.DataFrame() data = pd.concat([apix.to_df(apix.get_transaction_data( int(code_market.market), code, (30 - i) * 1800)) for i in range(31)], axis=0) return data.assign(datetime=pd.to_datetime(data['date'])).assign(date=lambda x: str(x)[0:10])\ .assign(code=str(code)).assign(order=range(len(data.index))).set_index('datetime', drop=False, inplace=False)
def QA_fetch_get_future_transaction_realtime(ip=None, port=None): '期货历史成交分笔' global best_ip if ip is None and port is None and best_ip['future']['ip'] is None and best_ip['future']['port'] is None: best_ip = select_best_ip() ip= best_ip['future']['ip'] port = best_ip['future']['port'] elif ip is None and port is None and best_ip['future']['ip'] is not None and best_ip['future']['port'] is not None: ip= best_ip['future']['ip'] port = best_ip['future']['port'] else: pass apix = TdxExHq_API() with apix.connect(ip, port): pass
def connect(self): if self.isstock: self.api = TdxHq_API(heartbeat=self.heartbeat) port = 7709 TDX_IP_SETS = self.TDX_IP_SETS_STOCK else: self.api = TdxExHq_API(heartbeat=self.heartbeat) port = 7727 TDX_IP_SETS = self.TDX_IP_SETS for ip in TDX_IP_SETS: try: if self.api.connect(ip, port): return except: pass
def get_option_daily(code_list): """ 从pytdx模块中获取期权价格。 :param code_list: :return: """ api = TdxExHq_API() df_list = [] with api.connect(ip=PYTDX_EXHQ_SERVER): for code in code_list: df = api.to_df( api.get_instrument_bars(TDXParams.KLINE_TYPE_DAILY, 8, code, 0, 100)) df_list.append(df) df_merge = pd.concat(df_list) df_merge.to_csv('pytdx_price.csv', encoding=TA_CSV_CODING) return df_merge
def get_hk_quote_by_tdx(code_list): """ 获取香港主板市场行情数据 需要使用拓展行情接口 通达信接口的港股行情数据比较简陋,只有当前价格,缺乏bid,ask以及对应的量能 :param code_list: :return: """ api_ex = TdxExHq_API() result = [] if api_ex.connect('140.143.179.226', 7727): for code in code_list: data = api_ex.get_instrument_quote(31, code) result.append( [data[0]["code"], data[0]["price"], data[0]["xianliang"]]) api_ex.disconnect() df = pd.DataFrame(data=result, columns=["code", "price", "xianliang"]) return df
def get_option_price_by_tdx(code): ''' 通过通达信获取option的实时价格 上海期权的市场代码是:8 :param code: :return:df df columns: ['market', 'code', 'pre_close', 'open', 'high', 'low', 'price', 'kaicang', 'zongliang', 'xianliang', 'neipan', 'waipan', 'chicang', 'bid1', 'bid2', 'bid3', 'bid4', 'bid5', 'bid_vol1', 'bid_vol2', 'bid_vol3', 'bid_vol4', 'bid_vol5', 'ask1', 'ask2', 'ask3', 'ask4', 'ask5', 'ask_vol1', 'ask_vol2', 'ask_vol3', 'ask_vol4', 'ask_vol5'] ''' api_ex = TdxExHq_API() data = pd.DataFrame() if api_ex.connect('61.49.50.181', 7727): data = api_ex.to_df(api_ex.get_instrument_quote(8, code)) return data
def ping(ip, port=7709, type_='stock'): api = TdxHq_API() apix = TdxExHq_API() __time1 = datetime.datetime.now() try: if type_ in ['stock']: with api.connect(ip, port, time_out=0.7): res = api.get_security_list(0, 1) #print(len(res)) if res is not None: if len(res) > 800: print('GOOD RESPONSE {}'.format(ip)) return datetime.datetime.now() - __time1 else: print('BAD RESPONSE {}'.format(ip)) return datetime.timedelta(9, 9, 0) else: print('BAD RESPONSE {}'.format(ip)) return datetime.timedelta(9, 9, 0) elif type_ in ['future']: with apix.connect(ip, port, time_out=0.7): res = apix.get_instrument_count() if res is not None: if res > 20000: print('GOOD RESPONSE {}'.format(ip)) return datetime.datetime.now() - __time1 else: print('️Bad FUTUREIP REPSONSE {}'.format(ip)) return datetime.timedelta(9, 9, 0) else: print('️Bad FUTUREIP REPSONSE {}'.format(ip)) return datetime.timedelta(9, 9, 0) except Exception as e: if isinstance(e, TypeError): print(e) print('Tushare内置的pytdx版本和最新的pytdx 版本不同, 请重新安装pytdx以解决此问题') print('pip uninstall pytdx') print('pip install pytdx') else: print('BAD RESPONSE {}'.format(ip)) return datetime.timedelta(9, 9, 0)
def QA_fetch_get_future_realtime(code, ip=None, port=None): '期货实时价格' ip, port = get_extensionmarket_ip(ip, port) apix = TdxExHq_API() global extension_market_list extension_market_list = QA_fetch_get_extensionmarket_list( ) if extension_market_list is None else extension_market_list __data = pd.DataFrame() code_market = extension_market_list.query('code=="{}"'.format(code)) with apix.connect(ip, port): __data = apix.to_df(apix.get_instrument_quote( int(code_market.market), code)) __data['datetime'] = datetime.datetime.now() # data = __data[['datetime', 'active1', 'active2', 'last_close', 'code', 'open', 'high', 'low', 'price', 'cur_vol', # 's_vol', 'b_vol', 'vol', 'ask1', 'ask_vol1', 'bid1', 'bid_vol1', 'ask2', 'ask_vol2', # 'bid2', 'bid_vol2', 'ask3', 'ask_vol3', 'bid3', 'bid_vol3', 'ask4', # 'ask_vol4', 'bid4', 'bid_vol4', 'ask5', 'ask_vol5', 'bid5', 'bid_vol5']] return __data.set_index('code', drop=False, inplace=False)
def get_fast_exhq_ip(self) -> (str, int): fast_exhq_ip_dict = {} exhq_api = TdxExHq_API() for name, ip in self.exhq_ips_dict.items(): with exhq_api.connect(ip, self.exhq_port): start_time = time.time() instrument_count = exhq_api.get_instrument_count() cost_time = time.time() - start_time self.log.write_log(f"{name}({ip}), time: {cost_time:.3f}s, response: {instrument_count}") fast_exhq_ip_dict[f"{ip}:{self.exhq_port}"] = cost_time ip_str, port_str = min(fast_exhq_ip_dict, key=fast_exhq_ip_dict.get).split(":") self.log.write_log(f"-"*50) self.log.write_log(f"Select ({ip_str} : {port_str})") self.log.write_log(f"-"*50) return ip_str, int(port_str)
def QA_fetch_get_future_list(ip=None, port=None): '期货代码list' global best_ip if ip is None and port is None and best_ip['future']['ip'] is None and best_ip['future']['port'] is None: best_ip = select_best_ip() ip = best_ip['future']['ip'] port = best_ip['future']['port'] elif ip is None and port is None and best_ip['future']['ip'] is not None and best_ip['future']['port'] is not None: ip = best_ip['future']['ip'] port = best_ip['future']['port'] else: pass apix = TdxExHq_API() with apix.connect(ip, port): market_info = apix.get_markets() num = apix.get_instrument_count() return pd.concat([apix.to_df( apix.get_instrument_info((int(num / 500) - i) * 500, 500)) for i in range(int(num / 500) + 1)], axis=0).set_index('code', drop=False)