Exemple #1
0
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=',')
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
class ExtQuotes(object):
    """扩展市场实时行情"""
    def __init__(self, **kwargs):
        self.client = TdxExHq_API(**kwargs)
        # self.bestip = os.environ.setdefault("MOOTDX_SERVER", '61.152.107.141:7727')
        # self.bestip = kwargs.get("bestip", '202.108.253.131:7709')
        # self.bestip = self.bestip.split(':')
        self.bestip = ('202.108.253.131', 7709)
        # self.bestip[1] = int(self.bestip[1])

    def markets(self):
        '''
        获取实时市场列表

        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_markets()
            return self.client.to_df(data)

        return None

    def quote5(self, market=47, symbol="IF1709"):
        '''
        查询五档行情

        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_instrument_quote(market, symbol)
            return self.client.to_df(data)

    def minute(self, market=47, symbol="IF1709"):
        '''
        查询五档行情

        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_minute_time_data(market, symbol)
            return self.client.to_df(data)

    def instrument(self, start=0, offset=100):
        '''
        查询代码列表
        :param start:
        :param offset:
        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            # nums = self.client.get_instrument_count()
            data = self.client.get_instrument_info(int(start), int(offset))
            return self.client.to_df(data)
Exemple #5
0
class ExQuotes(object):
    """扩展市场实时行情"""

    def __init__(self, **kwargs):
        self.client = TdxExHq_API(**kwargs)
        self.bestip = ('202.108.253.130', 7709)

    def bars(
            self,
            symbol='',
            category='1',
            market='0',
            start='0',
            offset='100'):
        '''
        获取实时日K线数据

        :param symbol:
        :param category:
        :param market:
        :param start:
        :param offset:
        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_security_bars(
                int(category), int(market), str(symbol), int(start), int(offset))
            return self.client.to_df(data)

    def markets(self):
        '''
        获取实时市场列表

        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_markets()
            return self.client.to_df(data)

    def instrument(self, start=0, offset=100):
        '''

        :param start:
        :param offset:
        :return: pd.dataFrame or None
        '''
        with self.client.connect(*self.bestip):
            data = self.client.get_instrument_info(int(start), int(offset))
            return self.client.to_df(data)
Exemple #6
0
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
Exemple #7
0
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]))
Exemple #8
0
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
Exemple #9
0
 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
Exemple #10
0
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 QA_fetch_get_future_day(code, start_date, end_date, frequence='day', 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()
    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]))
Exemple #12
0
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)
Exemple #13
0
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)
Exemple #14
0
class Datacent:
    def __init__(self):
        self.cf = config
        self.qihuo_api = TdxExHq_API()
        self.cflen = 0

    def qihuo_connectSer(self):

        self.qihuo_api.connect('218.80.248.229', 7721)

        qihuoret = self.qihuo_api.connect('218.80.248.229', 7721)
        if qihuoret == False:
            print("期货没有连接。。。")
            return qihuoret
        else:
            print("已连接期货数据服务")
            qihuoret = True
            return qihuoret

    def qihuoK(
        self,
        cflen,
    ):
        data = self.qihuo_api.get_instrument_bars(
            self.cf.category, int(self.cf.cfqihuo[cflen]["marketid"]),
            self.cf.cfqihuo[cflen]['code'], 0,
            self.cf.categorycount)  #7: 扩展行情查询k线数据
        df = pd.DataFrame(
            data,
            columns=[
                'datetime',
                'stockname',
                'open',
                'high',
                'low',
                'close',
                'code',
            ],
        )
        df['stockname'] = self.cf.cfqihuo[cflen]["stockname"]
        df['code'] = self.cf.cfqihuo[cflen]["code"]
        return df
Exemple #15
0
class ExEngine:
    def __init__(self, *args, **kwargs):
        self.api = TdxExHq_API(args, kwargs)

    def connect(self):
        self.api.connect('61.152.107.141', 7727)
        return self

    def __enter__(self):
        return self

    def exit(self):
        self.api.disconnect()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.api.disconnect()

    @lazyval
    def markets(self):
        return self.api.to_df(self.api.get_markets())
Exemple #16
0
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
Exemple #17
0
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
Exemple #18
0
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)))
Exemple #19
0
 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 QA_fetch_get_future_min(code, start, end, frequence='1min', 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()
    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 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
Exemple #22
0
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
Exemple #23
0
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)
Exemple #24
0
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
Exemple #25
0
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
Exemple #26
0
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
Exemple #27
0
 def __exhq_bars(self, code, offset, frequency=9):
     assert self.__ex.qsize() > 0
     api = TdxExHq_API()
     ip, port = self.__ex.get()
     with api.connect(ip, port):
         df = list()
         for _code in code:
             for _start, _count in offset:
                 df.append(
                     api.to_df(
                         api.get_instrument_bars(
                             frequency,
                             self.__exhq_list().xs(_code).market, _code,
                             _start, _count)).assign(code=_code))
         api.disconnect()
         self.__ex.put((ip, port))
         if len(df) < 1:
             return None
         return pandas.concat(df, sort=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)
Exemple #29
0
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)
Exemple #30
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)
Exemple #31
0
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)
def QA_fetch_get_future_transaction(code, start, end, retry=2, 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
    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_info.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
Exemple #33
0
def tdx_ping_future(ip, port=7709, type_='stock'):
    apix = TdxExHq_API()
    __time1 = dt.datetime.now()
    try:
        with apix.connect(ip, port, time_out=0.7):
            res = apix.get_instrument_count()
            if res is not None:
                if res > 40000:
                    return dt.datetime.now() - __time1
                else:
                    #print('️Bad FUTUREIP REPSONSE {}'.format(ip))
                    return dt.timedelta(9, 9, 0)
            else:
                #print('️Bad FUTUREIP REPSONSE {}'.format(ip))
                return dt.timedelta(9, 9, 0)
    #
    except Exception as e:
        pass
        #print('BAD RESPONSE {}'.format(ip))
        return dt.timedelta(9, 9, 0)
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):
                if len(api.get_security_list(0, 1)) > 800:
                    return datetime.datetime.now() - __time1
                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):
                if apix.get_instrument_count() > 10000:
                    return datetime.datetime.now() - __time1
                else:
                    print('️Bad FUTUREIP REPSONSE {}'.format(ip))
                    return datetime.timedelta(9, 9, 0)
    except:
        print('BAD RESPONSE {}'.format(ip))
        return datetime.timedelta(9, 9, 0)
Exemple #35
0
 def ping(self, ip, port=7709):
     """
     ping行情服务器
     :param ip:
     :param port:
     :param type_:
     :return:
     """
     apix = TdxExHq_API()
     __time1 = datetime.now()
     try:
         with apix.connect(ip, port):
             if apix.get_instrument_count() > 10000:
                 _timestamp = datetime.now() - __time1
                 self.write_log("服务器{ip}:{port},耗时:{_timestamp}")
                 return _timestamp
             else:
                 self.write_log("该服务器IP {ip}无响应")
                 return timedelta(9, 9, 0)
     except Exception:
         self.write_error("tdx ping服务器,异常的响应{ip}")
         return timedelta(9, 9, 0)
def QA_fetch_get_future_transaction_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

    code_market = extension_market_info.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)
               
                
                
            ])
            klines.append(kline)

        return klines
        
    
#00000000  01 01 08 6A 01 01 16 00  16 00 FF 23 2F 49 46 4C   ...j.... ...#/IFL 
#00000010  30 00 F0 F4 94 13 07 00  01 00 00 00 00 00 F0 00   0....... ........ 
    
#00000000: 01 01 08 6A 01 01 16 00  16 00 FF 23 4A 4E 56 44  ...j.......#JNVD
#00000010: 41 00 C0 EC A3 13 07 00  01 00 00 00 00 00 C0 03  A...............    

#00000000  01 01 08 6A 01 01 16 00  16 00 FF 23 2F 49 46 31   ...j.... ...#/IF1 
#00000010  37 30 39 00 94 13 07 00  01 00 00 00 00 00 F0 00   709..... ........ 

if __name__ == '__main__':
    import pprint
    from pytdx.exhq import TdxExHq_API
    api = TdxExHq_API()
    with api.connect('61.152.107.141', 7727):
        x = api.to_df(api.get_history_instrument_bars_range(74, "BABA", 20170613,20170620))
        pprint.pprint(x.tail())

        
        
        

        result = []
        for i in range(count):
            (category, market, unused_bytes, code_raw, name_raw, desc_raw) = \
                struct.unpack("<BB3s9s17s9s", body_buf[pos: pos+40])

            code = code_raw.decode("gbk", 'ignore')
            name = name_raw.decode("gbk", 'ignore')
            desc = desc_raw.decode("gbk", 'ignore')

            one = OrderedDict(
                [
                    ("category", category),
                    ("market", market),
                    ("code", code.rstrip("\x00")),
                    ("name", name.rstrip("\x00")),
                    ("desc", desc.rstrip("\x00")),
                ]
            )

            pos += 64
            result.append(one)

        return result

if __name__ == '__main__':
    from pytdx.exhq import TdxExHq_API
    api = TdxExHq_API()
    api.connect('121.14.110.210', 7727)
    ret = api.get_instrument_info(200, 100)
    print(ret)