Exemplo n.º 1
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
Exemplo n.º 2
0
Arquivo: go.py Projeto: cjj208/jimc
 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()
Exemplo n.º 3
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)
Exemplo n.º 4
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]))
Exemplo n.º 5
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
Exemplo n.º 6
0
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]))
Exemplo n.º 7
0
    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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 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]))
Exemplo n.º 10
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)
Exemplo n.º 11
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
Exemplo n.º 12
0
    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
Exemplo n.º 13
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
Exemplo n.º 14
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)))
Exemplo n.º 15
0
 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)
Exemplo n.º 16
0
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)))
Exemplo n.º 17
0
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
Exemplo n.º 18
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=',')
Exemplo n.º 19
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
Exemplo n.º 20
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)
Exemplo n.º 21
0
    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
Exemplo n.º 22
0
    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
Exemplo n.º 23
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
Exemplo n.º 24
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
Exemplo n.º 25
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)
Exemplo n.º 26
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)
Exemplo n.º 27
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)
Exemplo n.º 28
0
    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)
Exemplo n.º 29
0
def QA_fetch_get_future_list(ip=best_ip['future']['ip'], port=best_ip['future']['port']):
    '期货代码list'
    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)
Exemplo n.º 30
0
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
Exemplo n.º 31
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
Exemplo n.º 32
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)
Exemplo n.º 33
0
    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_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
Exemplo n.º 34
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
Exemplo n.º 35
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)
Exemplo n.º 36
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)
Exemplo n.º 37
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)
Exemplo n.º 38
0
        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)
Exemplo n.º 39
0
                    ('bid5', b5),
                    ('bid_vol1', bv1),
                    ('bid_vol2', bv2),
                    ('bid_vol3', bv3),
                    ('bid_vol4', bv4),
                    ('bid_vol5', bv5),
                    ('ask1', a1),
                    ('ask2', a2),
                    ('ask3', a3),
                    ('ask4', a4),
                    ('ask5', a5),
                    ('ask_vol1', av1),
                    ('ask_vol2', av2),
                    ('ask_vol3', av3),
                    ('ask_vol4', av4),
                    ('ask_vol5', av5),
                ]
            )
        ]


if __name__ == '__main__':
    from pytdx.exhq import TdxExHq_API

    api = TdxExHq_API()
    with api.connect('61.152.107.141', 7727):
        print(api.to_df(api.get_instrument_quote(47, "IF1709")))



Exemplo n.º 40
0
                else: #512
                    direction = 0
                    nature_name = ''


            result.append(OrderedDict([
                ("date", date),
                ("hour", hour),
                ("minute", minute),
                ("second", second),
                ("price", price),
                ("volume", volume),
                ("zengcang", zengcang),
                ("nature", nature),
                ("nature_mark", nature // 10000),
                ("nature_value", nature % 10000),
                ("nature_name", nature_name),
                ("direction", direction),
            ]))

        return result


if __name__ == "__main__":
    from pytdx.exhq import TdxExHq_API

    api = TdxExHq_API()
    with api.connect('121.14.110.210', 7727):
        print(api.to_df(api.get_transaction_data(47, 'IFL9')))
        # print(api.to_df(api.get_transaction_data(31, "00020")))
               
                
                
            ])
            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())

        
        
        

Exemplo n.º 42
0
                ("low", low),
                ("close", close),
                ("position", position),
                ("trade", trade),
                ("price", price),
                ("year", year),
                ("month", month),
                ("day", day),
                ("hour", hour),
                ("minute", minute),
                ("datetime", "%d-%02d-%02d %02d:%02d" % (year, month, day, hour, minute)),
                ("amount", amount),
            ])

            klines.append(kline)

        return klines



if __name__ == '__main__':
    from pytdx.exhq import TdxExHq_API
    from pytdx.params import TDXParams
    api = TdxExHq_API()
    # cmd = GetInstrumentBars(api)
    # cmd.setParams(4, 7, "10000843", 0, 10)
    # print(cmd.send_pkg)
    with api.connect('61.152.107.141', 7727):
        print(api.to_df(api.get_instrument_bars(TDXParams.KLINE_TYPE_EXHQ_1MIN, 74, 'BABA')).tail())
        print(api.to_df(api.get_instrument_bars(TDXParams.KLINE_TYPE_DAILY, 31, '00001')).tail())
                    direction = 0
                    nature_name = ''

            result.append(OrderedDict([
                ("date", date),
                ("hour", hour),
                ("minute", minute),
                ("price", price),
                ("volume", volume),
                ("zengcang", zengcang),
                ("natrue_name", nature_name),
                ("nature_name", nature_name), #修正了nature_name的拼写错误(natrue), 为了保持兼容性,原有的natrue_name还会保留一段时间
                ("direction", direction),
                ("nature", nature),

            ]))

        return result


if __name__ == '__main__':

    from pytdx.exhq import TdxExHq_API

    api = TdxExHq_API()
    with api.connect('121.14.110.210', 7727):
        # print(api.to_df(api.get_history_transaction_data(4, 'SR61099D', 20171025))[["date","price","volume",'zengcang','nature','t1','t2']])

        print(api.to_df(api.get_history_transaction_data(47, 'IFL0', 20170811)))
        #print(api.to_df(api.get_history_transaction_data(31,  "01918", 20171026))[["date","price","volume",'zengcang','nature']])
        #api.to_df(api.get_history_transaction_data(47, 'IFL0', 20170810)).to_excel('//Users//wy//data//iflo.xlsx')