量化交易之vnpy篇 - 主力合约换月 - 引擎调用私有api部分

    # --- private part ---
    def __change_main_contracts(self):
        """
        Change main contracts data
        """

        if self.__today_main_contracts_has_change is False:
            self.__today_main_contracts_has_change = True

            paths_setting_content = TQZJsonOperator.tqz_load_jsonfile(
                jsonfile=TQZMainContractsChangeFilePath.paths_setting_jsonfile()
            )

            [self.__reset_main_contracts(
                strategy_type=strategy_type,
                data_paths=data_paths
            ) for strategy_type, data_paths in paths_setting_content.items()]


    @staticmethod
    def __get_main_contracts_dictionary():
        """
        Change main_contracts format from dataframe to dictionary and return dictionary
        """

        main_contracts_dataframe = pandas.read_excel(
            io=TQZMainContractsChangeFilePath.main_contracts_excel(),
            sheet_name=TQZMainContractsSheetType.CURRENT_FUTURE_MAIN_CONTRACT.value
        )
        main_contracts_dataframe.dropna(
            subset=[TQZMainContractsColumnType.ENTRY_PRICE.value],
            inplace=True
        )

        main_contracts_dictionary = {}
        for index in main_contracts_dataframe.index:
            main_contract = main_contracts_dataframe.iloc[index][TQZMainContractsColumnType.MAIN_CONTRACT.value]
            entry_price = main_contracts_dataframe.iloc[index][TQZMainContractsColumnType.ENTRY_PRICE.value]
            sym = re.match(r"^[a-zA-Z]{1,3}", main_contract.split(".")[0]).group()

            main_contracts_dictionary[sym] = {
                TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value: main_contract,
                TQZMainContractsKeyType.ENTRY_PRICE_KEY.value: float(entry_price)
            }

        return main_contracts_dictionary

    def __load_main_contracts_from_tq(self, tq_id, tq_password, ins_class="CONT"):
        """
        Load data from TianQin(default data is main contracts)
        """

        if self.__today_main_contracts_has_load is False:
            self.__today_main_contracts_has_load = True

            TQZTqClient(
                account_name=tq_id,
                account_password=tq_password
            ).load_main_contracts(
                ins_class=ins_class
            )


    def __reset_main_contracts(self, strategy_type, data_paths):

        if TQZMainContractsStrategyPathType.CTA_STRATEGY_DATA_PATH.value == strategy_type:

            [self.__reset_cta_strategy_data_main_contracts(
                cta_data_path=cta_data_path
            ) for cta_data_path in data_paths.values()]

        elif TQZMainContractsStrategyPathType.CTA_STRATEGY_SETTING_PATH.value == strategy_type:

            [self.__reset_cta_strategy_setting_main_contracts(
                cta_setting_path=cta_setting_path
            ) for cta_setting_path in data_paths.values()]

        elif TQZMainContractsStrategyPathType.PORTFOLIO_STRATEGY_DATA_PATH.value == strategy_type:

            [self.__reset_portfolio_strategy_data_main_contracts(
                portfolio_strategy_data_path=portfolio_strategy_data_path
            ) for portfolio_strategy_data_path in data_paths.values()]

        elif TQZMainContractsStrategyPathType.PORTFOLIO_STRATEGY_SETTING_PATH.value == strategy_type:

            [self.__reset_portfolio_strategy_setting_main_contracts(
                portfolio_strategy_setting_path=portfolio_strategy_setting_path
            ) for portfolio_strategy_setting_path in data_paths.values()]

    def __reset_cta_strategy_data_main_contracts(self, cta_data_path):
        """
        Reset main contracts of cta strategy data.
        """

        main_contracts_dictionary = self.__get_main_contracts_dictionary()

        cta_strategy_data_before = TQZJsonOperator.tqz_load_jsonfile(jsonfile=cta_data_path)
        cta_strategy_data_after = {}

        for vt_symbol_strategy, vt_symbol_strategy_content in cta_strategy_data_before.items():
            sym = TQZSymbolOperator.get_sym(vt_symbol=vt_symbol_strategy)

            if sym in main_contracts_dictionary.keys():
                current_main_contract = main_contracts_dictionary[sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                strategy = vt_symbol_strategy.split(".")[2]
                new_vt_symbol_strategy = f'{current_main_contract}.{strategy}'
                vt_symbol_strategy_content[TQZMainContractsKeyType.ENTRYPRICE_KEY.value] = main_contracts_dictionary[sym][TQZMainContractsKeyType.ENTRY_PRICE_KEY.value]

                cta_strategy_data_after[new_vt_symbol_strategy] = vt_symbol_strategy_content
            else:
                cta_strategy_data_after[vt_symbol_strategy] = vt_symbol_strategy_content

        TQZJsonOperator.tqz_write_jsonfile(content=cta_strategy_data_after, target_jsonfile=cta_data_path)

    def __reset_cta_strategy_setting_main_contracts(self, cta_setting_path):
        """
        Reset main contracts of cta strategy setting.
        """

        main_contracts_dictionary = self.__get_main_contracts_dictionary()

        cta_strategy_setting_before = TQZJsonOperator.tqz_load_jsonfile(jsonfile=cta_setting_path)
        cta_strategy_setting_after = {}

        for vt_symbol_strategy, vt_symbol_strategy_content in cta_strategy_setting_before.items():
            sym = TQZSymbolOperator.get_sym(vt_symbol=vt_symbol_strategy)

            if sym in main_contracts_dictionary.keys():
                current_main_contract = main_contracts_dictionary[sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                strategy = vt_symbol_strategy.split(".")[2]
                new_vt_symbol_strategy = f'{current_main_contract}.{strategy}'

                vt_symbol_strategy_content[TQZMainContractsKeyType.VT_SYMBOL_KEY.value] = current_main_contract

                cta_strategy_setting_after[new_vt_symbol_strategy] = vt_symbol_strategy_content
            else:
                cta_strategy_setting_after[vt_symbol_strategy] = vt_symbol_strategy_content

        TQZJsonOperator.tqz_write_jsonfile(content=cta_strategy_setting_after, target_jsonfile=cta_setting_path)

    def __reset_portfolio_strategy_data_main_contracts(self, portfolio_strategy_data_path):
        """
        Reset main contracts of portfolio strategy data.
        """

        main_contracts_dictionary = self.__get_main_contracts_dictionary()

        portfolio_strategy_data_before = TQZJsonOperator.tqz_load_jsonfile(jsonfile=portfolio_strategy_data_path)
        portfolio_strategy_data_after = {}

        for sym_pair, sym_pair_content in portfolio_strategy_data_before.items():
            portfolio_strategy_data_after[sym_pair] = {}

            for inside_key, inside_data in sym_pair_content.items():
                portfolio_strategy_data_after[sym_pair][inside_key] = {}

                if TQZMainContractsKeyType.POS_KEY.value == inside_key:
                    for inside_vt_symbol, inside_vt_symbol_pos in inside_data.items():
                        inside_sym = TQZSymbolOperator.get_sym(vt_symbol=inside_vt_symbol)

                        if inside_sym in main_contracts_dictionary.keys():
                            current_main_contract = main_contracts_dictionary[inside_sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                            portfolio_strategy_data_after[sym_pair][inside_key][
                                current_main_contract] = inside_vt_symbol_pos
                        else:
                            portfolio_strategy_data_after[sym_pair][inside_key][inside_vt_symbol] = inside_vt_symbol_pos

                elif TQZMainContractsKeyType.TARGET_POSITION_KEY.value == inside_key:
                    for inside_vt_symbol, inside_vt_symbol_pos in inside_data.items():
                        inside_sym = TQZSymbolOperator.get_sym(vt_symbol=inside_vt_symbol)

                        if inside_sym in main_contracts_dictionary.keys():
                            current_main_contract = main_contracts_dictionary[inside_sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                            portfolio_strategy_data_after[sym_pair][inside_key][
                                current_main_contract] = inside_vt_symbol_pos
                        else:
                            portfolio_strategy_data_after[sym_pair][inside_key][inside_vt_symbol] = inside_vt_symbol_pos
                elif TQZMainContractsKeyType.ER_FACTOR_KEY.value == inside_key:
                    for inside_vt_symbol, inside_vt_symbol_pos in inside_data.items():
                        inside_sym = TQZSymbolOperator.get_sym(vt_symbol=inside_vt_symbol)

                        if inside_sym in main_contracts_dictionary.keys():
                            current_main_contract = main_contracts_dictionary[inside_sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                            portfolio_strategy_data_after[sym_pair][inside_key][
                                current_main_contract] = inside_vt_symbol_pos
                        else:
                            portfolio_strategy_data_after[sym_pair][inside_key][inside_vt_symbol] = inside_vt_symbol_pos
                else:
                    portfolio_strategy_data_after[sym_pair][inside_key] = inside_data

        TQZJsonOperator.tqz_write_jsonfile(content=portfolio_strategy_data_after, target_jsonfile=portfolio_strategy_data_path)

    def __reset_portfolio_strategy_setting_main_contracts(self, portfolio_strategy_setting_path):
        """
        Reset main contracts of portfolio strategy setting
        """

        main_contracts_dictionary = self.__get_main_contracts_dictionary()

        portfolio_strategy_setting_before = TQZJsonOperator.tqz_load_jsonfile(jsonfile=portfolio_strategy_setting_path)
        portfolio_strategy_setting_after = {}

        for sym_pair, sym_pair_content in portfolio_strategy_setting_before.items():
            portfolio_strategy_setting_after[sym_pair] = {}

            for inside_key, inside_data in sym_pair_content.items():
                portfolio_strategy_setting_after[sym_pair][inside_key] = {}

                if TQZMainContractsKeyType.VT_SYMBOLS_KEY.value == inside_key:
                    temp_inside_data = []
                    for vt_symbol in inside_data:
                        temp_sym = TQZSymbolOperator.get_sym(vt_symbol=vt_symbol)

                        if temp_sym in main_contracts_dictionary.keys():
                            current_main_contract = main_contracts_dictionary[temp_sym][TQZMainContractsKeyType.MAIN_CONTRACT_KEY.value]
                            temp_inside_data.append(current_main_contract)
                        else:
                            temp_inside_data.append(vt_symbol)

                    portfolio_strategy_setting_after[sym_pair][inside_key] = temp_inside_data
                else:
                    portfolio_strategy_setting_after[sym_pair][inside_key] = inside_data

        TQZJsonOperator.tqz_write_jsonfile(content=portfolio_strategy_setting_after, target_jsonfile=portfolio_strategy_setting_path)

    def __init_today(self):
        """
        Init today data when new day is coming or restart program.
        """

        if self.__current_day is not time.localtime().tm_wday:
            self.__current_day = time.localtime().tm_wday
            self.__today_main_contracts_has_load = False
            self.__today_main_contracts_has_change = False

    @staticmethod
    def __is_refresh_time(now_time, interval_time):
        """
        Judge current time need callback or not
        """

        if now_time % interval_time is 0:
            should_refresh = True
        else:
            should_refresh = False

        return should_refresh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值