量化交易之回测篇 - 资金管理(刷新std_avg_tr_ticks.json)

import os

from public_module.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator
from public_module.tqz_extern.tools.position_operator.position_operator import TQZJsonOperator
from server_api.api.tqz_tianqin_api import TQZTianQinDataManager

from math import floor

class TQZFundManage:

    __future_contracts_setting = TQZJsonOperator.tqz_load_jsonfile(
        jsonfile=TQZFilePathOperator.grandfather_path(
            source_path=__file__
        ) + f'/back_tester_config/future/future_contracts_setting.json'
    )
    __avg_tr_path = TQZFilePathOperator.father_path(source_path=__file__) + f'/result_data/avg_tr.json'
    __pos_result_path = TQZFilePathOperator.father_path(source_path=__file__) + f'/result_data/pos_result.json'

    @classmethod
    def __csv_content(cls):
        return TQZTianQinDataManager.load_main_history_bars_from_csv(
            tq_m_symbols=[
                "KQ.m@SHFE.cu",
                "KQ.m@SHFE.rb",
                "KQ.m@CFFEX.IF",
                "KQ.m@CFFEX.IC",
                "KQ.m@CFFEX.T",
                "KQ.m@SHFE.au",
                "KQ.m@DCE.i",
                "KQ.m@DCE.m",
                "KQ.m@CZCE.CF",
                "KQ.m@DCE.y",
                "KQ.m@CZCE.TA",
                "KQ.m@SHFE.ag",
                "KQ.m@DCE.p",
                "KQ.m@SHFE.hc",
                "KQ.m@SHFE.ru",
                "KQ.m@DCE.j",
                "KQ.m@CZCE.MA",
                "KQ.m@CZCE.SR",
                "KQ.m@DCE.c",
                "KQ.m@SHFE.al",
                "KQ.m@SHFE.ni",
                "KQ.m@CZCE.OI",
                "KQ.m@CZCE.FG",
                "KQ.m@CZCE.SA",
                "KQ.m@INE.sc",
                "KQ.m@DCE.v",
                "KQ.m@CZCE.AP",
                "KQ.m@DCE.pp",
                "KQ.m@DCE.jm",
                "KQ.m@DCE.l",
                "KQ.m@CZCE.RM",
                "KQ.m@SHFE.bu",
                "KQ.m@DCE.eg",
                "KQ.m@SHFE.zn",
                "KQ.m@DCE.lh",
                "KQ.m@SHFE.sp",
                "KQ.m@CZCE.ZC",
                "KQ.m@SHFE.fu",
                "KQ.m@SHFE.sn",
                "KQ.m@CZCE.SF",
                "KQ.m@DCE.a",
                "KQ.m@DCE.jd",
                "KQ.m@CZCE.SM",
                "KQ.m@SHFE.ss",
                "KQ.m@DCE.eb",
                "KQ.m@DCE.pg",
                "KQ.m@CZCE.PF",
                "KQ.m@CZCE.UR",
                "KQ.m@SHFE.pb",
                "KQ.m@DCE.cs"
            ]
        )

    @classmethod
    def refresh_avg_tr_json(cls):
        avg_tr_map = {}
        for tq_m_symbol, bars in cls.__csv_content().items():
            trs = []
            for bar in bars:
                hl_diff = bar.high_price - bar.low_price
                trs.append(hl_diff)

            trs.sort()

            begin_index = floor(len(trs) * 0.25)
            end_index = len(trs) - begin_index
            avg_tr = sum(trs[begin_index:end_index]) / len(trs[begin_index:end_index])

            tq_sym = tq_m_symbol.split('@')[1]
            min_tick_price_flow = cls.__future_contracts_setting[tq_sym]["min_tick_price_flow"]
            contract_multiple = cls.__future_contracts_setting[tq_sym]["contract_multiple"]

            avg_tr_map[tq_sym] = {
                "avg_tr": avg_tr,
                "min_tick_price_flow": min_tick_price_flow,
                "contract_multiple": contract_multiple,
                "avg_tr_ticks": int(avg_tr / min_tick_price_flow),
                "std_avg_tr_ticks": cls.__get_std_avg_tr_ticks(avg_tr_ticks=int(avg_tr / min_tick_price_flow)),
            }

        TQZJsonOperator.tqz_write_jsonfile(content=avg_tr_map, target_jsonfile=cls.__avg_tr_path)


    # --- private part ---
    @classmethod
    def __get_std_avg_tr_ticks(cls, avg_tr_ticks: int, base_value: int = 10) -> int:
        assert avg_tr_ticks >= 0, f'avg_tr_ticks value is error.({avg_tr_ticks})'

        count = floor(avg_tr_ticks / base_value)
        if avg_tr_ticks % base_value is 0:  # 以 10 为 基准单位
            return count * base_value
        else:
            return (count + 1) * base_value


if __name__ == '__main__':
    TQZFundManage.refresh_avg_tr_json()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值