量化交易之回测篇 - 天勤数据接口(新增指数和主连api)

from tqsdk import TqApi, TqAuth
from public_module.tqz_extern.tools.pandas_operator.pandas_operator import pandas  # noqa

from server_api.tqz_object import BarData, Exchange

TIME_GAP = 8 * 60 * 60 * 1000000000


class TQZTianQinClient:
    """
    天勤接口 每次只能拉取单一合约的数据!
    """

    __tq_symbols = None

    def __init__(self, account: str = "account", pass_word: str = "password"):
        self.api = TqApi(auth=TqAuth(account, pass_word))

        if TQZTianQinClient.__tq_symbols is None:
            TQZTianQinClient.__tq_symbols = self.api.query_quotes(ins_class="FUTURE", expired=False)


    def query_history_bars(self, tq_symbol: str, tq_duration_seconds: int, tq_data_length: int = 8964) -> list:
        assert tq_symbol in TQZTianQinClient.__tq_symbols, f'bad tq_symbol: {tq_symbol}'

        tq_result = self.api.get_kline_serial(symbol=tq_symbol, duration_seconds=tq_duration_seconds, data_length=tq_data_length)
        self.api.close()

        tq_result["datetime"] = pandas.to_datetime(tq_result["datetime"] + TIME_GAP)
        tq_result['datetime'] = tq_result['datetime'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))  # %f是毫秒

        symbol, exchange = TQZTianQinClient.__get_symbol_exchange(tq_symbol=tq_symbol)
        history_bars = []
        if tq_result is not None:
            for ix, row in tq_result.loc[tq_result["id"] >= 0].iterrows():
                history_bars.append(
                    BarData(
                        symbol=symbol,
                        exchange=exchange,
                        interval='any_interval',  # noqa
                        datetime=row["datetime"],
                        open_price=row["open"],
                        high_price=row["high"],
                        low_price=row["low"],
                        close_price=row["close"],
                        volume=row["volume"],
                        open_interest=row.get("open_oi", 0),
                        gateway_name="TQ",
                    )
                )

        return history_bars

    def query_index_history_bars(self, tq_index_symbol: str, tq_duration_seconds: int, tq_data_length: int = 8964) -> list:
        tq_result = self.api.get_kline_serial(symbol=tq_index_symbol, duration_seconds=tq_duration_seconds, data_length=tq_data_length)
        self.api.close()

        tq_result["datetime"] = pandas.to_datetime(tq_result["datetime"] + TIME_GAP)
        tq_result['datetime'] = tq_result['datetime'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))  # %f是毫秒

        symbol, exchange = f'{tq_index_symbol.split(".")[2]}000', Exchange.TQZ_INDEX
        history_bars = []
        if tq_result is not None:
            for ix, row in tq_result.loc[tq_result["id"] >= 0].iterrows():
                history_bars.append(
                    BarData(
                        symbol=symbol,
                        exchange=exchange,
                        interval='any_interval',  # noqa
                        datetime=row["datetime"],
                        open_price=row["open"],
                        high_price=row["high"],
                        low_price=row["low"],
                        close_price=row["close"],
                        volume=row["volume"],
                        open_interest=row.get("open_oi", 0),
                        gateway_name="TQ",
                    )
                )

        return history_bars

    def query_main_history_bars(self, tq_main_symbol: str, tq_duration_seconds: int, tq_data_length: int = 8964) -> list:
        tq_result = self.api.get_kline_serial(symbol=tq_main_symbol, duration_seconds=tq_duration_seconds, data_length=tq_data_length)
        self.api.close()

        tq_result["datetime"] = pandas.to_datetime(tq_result["datetime"] + TIME_GAP)
        tq_result['datetime'] = tq_result['datetime'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))  # %f是毫秒

        symbol, exchange = f'{tq_main_symbol.split(".")[2]}888', Exchange.TQZ_MAIN
        history_bars = []
        if tq_result is not None:
            for ix, row in tq_result.loc[tq_result["id"] >= 0].iterrows():
                history_bars.append(
                    BarData(
                        symbol=symbol,
                        exchange=exchange,
                        interval='any_interval',  # noqa
                        datetime=row["datetime"],
                        open_price=row["open"],
                        high_price=row["high"],
                        low_price=row["low"],
                        close_price=row["close"],
                        volume=row["volume"],
                        open_interest=row.get("open_oi", 0),
                        gateway_name="TQ",
                    )
                )

        return history_bars

    def load_all_tq_symbols(self):
        self.api.close()
        return TQZTianQinClient.__tq_symbols


    # --- private part ---
    @classmethod
    def __get_symbol_exchange(cls, tq_symbol: str) -> (str, Exchange):
        exchange_str, symbol = tq_symbol.split('.')[0], tq_symbol.split('.')[1]

        if exchange_str in [Exchange.SHFE.value]:
            exchange = Exchange.SHFE
        elif exchange_str in [Exchange.INE.value]:
            exchange = Exchange.INE
        elif exchange_str in [Exchange.DCE.value]:
            exchange = Exchange.DCE
        elif exchange_str in [Exchange.CZCE.value]:
            exchange = Exchange.CZCE
        elif exchange_str in [Exchange.CFFEX.value]:
            exchange = Exchange.CFFEX
        else:
            assert False, f'bad exchange_str: {exchange_str}'

        return symbol, exchange


class TQZTianQinDataManager:

    __load_history_bars_map: dict = None
    __load_history_index_bars_map: dict = None
    __load_history_main_bars_map: dict = None

    @classmethod
    def load_history_bars_map(cls, tq_symbols: list, tq_duration_seconds: int, tq_data_length: int = 8964) -> dict:
        """
        Load history bars of multi symbols from tianqin, and only load once time before back tester.
        :param tq_symbols: list of multi symbols
        :param tq_duration_seconds: just tq_duration_seconds
        :param tq_data_length: data length
        :return: history bars map
        """

        load_history_bars_map = {}
        if cls.__load_history_bars_map is None:
            for tq_symbol in tq_symbols:
                if tq_symbol not in load_history_bars_map.keys():
                    load_history_bars_map[tq_symbol] = TQZTianQinClient().query_history_bars(
                        tq_symbol=tq_symbol,
                        tq_duration_seconds=tq_duration_seconds,
                        tq_data_length=tq_data_length
                    )

            cls.__load_history_bars_map = load_history_bars_map

        return cls.__load_history_bars_map

    @classmethod
    def load_history_index_bars_map(cls, tq_index_symbols: list, tq_duration_seconds: int, tq_data_length: int = 8964) -> dict:
        """
        Load history bars of multi index symbols from tianqin, and only load once time before back tester.
        :param tq_index_symbols: list of multi symbols
        :param tq_duration_seconds: just tq_duration_seconds
        :param tq_data_length: data length
        :return: history index bars map
        """

        load_history_index_bars_map = {}
        if cls.__load_history_index_bars_map is None:
            for tq_index_symbol in tq_index_symbols:
                if tq_index_symbol not in load_history_index_bars_map.keys():
                    load_history_index_bars_map[tq_index_symbol] = TQZTianQinClient().query_index_history_bars(
                        tq_index_symbol=tq_index_symbol,
                        tq_duration_seconds=tq_duration_seconds,
                        tq_data_length=tq_data_length
                    )

            cls.__load_history_index_bars_map = load_history_index_bars_map

        return cls.__load_history_index_bars_map

    @classmethod
    def load_history_main_bars_map(cls, tq_main_symbols: list, tq_duration_seconds: int, tq_data_length: int = 8964) -> dict:
        """
        Load history bars of multi main symbols from tianqin, and only load once time before back tester.
        :param tq_main_symbols: list of multi symbols
        :param tq_duration_seconds: just tq_duration_seconds
        :param tq_data_length: data length
        :return: history main bars map
        """

        load_history_main_bars_map = {}
        if cls.__load_history_main_bars_map is None:
            for tq_main_symbols in tq_main_symbols:
                if tq_main_symbols not in load_history_main_bars_map.keys():
                    load_history_main_bars_map[tq_main_symbols] = TQZTianQinClient().query_main_history_bars(
                        tq_main_symbol=tq_main_symbols,
                        tq_duration_seconds=tq_duration_seconds,
                        tq_data_length=tq_data_length
                    )

            cls.__load_history_main_bars_map = load_history_main_bars_map

        return cls.__load_history_main_bars_map


if __name__ == '__main__':
    """ contract data
    content = TQZTianQinDataManager.load_history_bars_map(tq_symbols=["CZCE.SM206", "SHFE.rb2205"], tq_duration_seconds=60 * 60)
    print("content: " + str(content))
    """

    """ index data
    content = TQZTianQinDataManager.load_history_index_bars_map(tq_index_symbols=["KQ.i@SHFE.bu"], tq_duration_seconds=60 * 60, tq_data_length=3)
    print("content: " + str(content))
    """


    """ main data
    """
    content = TQZTianQinDataManager.load_history_main_bars_map(tq_main_symbols=["KQ.m@SHFE.bu"], tq_duration_seconds=60 * 60, tq_data_length=10)

    for content_list in content.values():
        for bar_data in content_list:
            print("bar_data: " + str(bar_data))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值