|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import copy |
| 6 | +import pickle |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from typing import Optional, Tuple, Union |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import pandas as pd |
| 13 | +import torch |
| 14 | +from joblib import Parallel, delayed |
| 15 | + |
| 16 | +from qlib.backtest import collect_data_loop, get_strategy_executor |
| 17 | +from qlib.backtest.decision import TradeRangeByTime |
| 18 | +from qlib.backtest.executor import BaseExecutor, NestedExecutor, SimulatorExecutor |
| 19 | +from qlib.backtest.high_performance_ds import BaseOrderIndicator |
| 20 | +from qlib.rl.contrib.naive_config_parser import get_backtest_config_fromfile |
| 21 | +from qlib.rl.contrib.utils import read_order_file |
| 22 | +from qlib.rl.data.integration import init_qlib |
| 23 | +from qlib.rl.utils.env_wrapper import CollectDataEnvWrapper |
| 24 | + |
| 25 | + |
| 26 | +def _get_multi_level_executor_config( |
| 27 | + strategy_config: dict, |
| 28 | + cash_limit: float = None, |
| 29 | + generate_report: bool = False, |
| 30 | +) -> dict: |
| 31 | + executor_config = { |
| 32 | + "class": "SimulatorExecutor", |
| 33 | + "module_path": "qlib.backtest.executor", |
| 34 | + "kwargs": { |
| 35 | + "time_per_step": "1min", |
| 36 | + "verbose": False, |
| 37 | + "trade_type": SimulatorExecutor.TT_PARAL if cash_limit is not None else SimulatorExecutor.TT_SERIAL, |
| 38 | + "generate_report": generate_report, |
| 39 | + "track_data": True, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + freqs = list(strategy_config.keys()) |
| 44 | + freqs.sort(key=lambda x: pd.Timedelta(x)) |
| 45 | + for freq in freqs: |
| 46 | + executor_config = { |
| 47 | + "class": "NestedExecutor", |
| 48 | + "module_path": "qlib.backtest.executor", |
| 49 | + "kwargs": { |
| 50 | + "time_per_step": freq, |
| 51 | + "inner_strategy": strategy_config[freq], |
| 52 | + "inner_executor": executor_config, |
| 53 | + "track_data": True, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + return executor_config |
| 58 | + |
| 59 | + |
| 60 | +def _set_env_for_all_strategy(executor: BaseExecutor) -> None: |
| 61 | + if isinstance(executor, NestedExecutor): |
| 62 | + if hasattr(executor.inner_strategy, "set_env"): |
| 63 | + env = CollectDataEnvWrapper() |
| 64 | + env.reset() |
| 65 | + executor.inner_strategy.set_env(env) |
| 66 | + _set_env_for_all_strategy(executor.inner_executor) |
| 67 | + |
| 68 | + |
| 69 | +def _convert_indicator_to_dataframe(indicator: dict) -> Optional[pd.DataFrame]: |
| 70 | + record_list = [] |
| 71 | + for time, value_dict in indicator.items(): |
| 72 | + if isinstance(value_dict, BaseOrderIndicator): |
| 73 | + # HACK: for qlib v0.8 |
| 74 | + value_dict = value_dict.to_series() |
| 75 | + try: |
| 76 | + value_dict = {k: v for k, v in value_dict.items()} |
| 77 | + if value_dict["ffr"].empty: |
| 78 | + continue |
| 79 | + except Exception: |
| 80 | + value_dict = {k: v for k, v in value_dict.items() if k != "pa"} |
| 81 | + value_dict = pd.DataFrame(value_dict) |
| 82 | + value_dict["datetime"] = time |
| 83 | + record_list.append(value_dict) |
| 84 | + |
| 85 | + if not record_list: |
| 86 | + return None |
| 87 | + |
| 88 | + records: pd.DataFrame = pd.concat(record_list, 0).reset_index().rename(columns={"index": "instrument"}) |
| 89 | + records = records.set_index(["instrument", "datetime"]) |
| 90 | + return records |
| 91 | + |
| 92 | + |
| 93 | +def _generate_report(decisions: list, report_dict: dict) -> dict: |
| 94 | + report = {} |
| 95 | + decision_details = pd.concat([d.details for d in decisions if hasattr(d, "details")]) |
| 96 | + for key in ["1minute", "5minute", "30minute", "1day"]: |
| 97 | + if key not in report_dict["indicator"]: |
| 98 | + continue |
| 99 | + report[key] = report_dict["indicator"][key] |
| 100 | + report[key + "_obj"] = _convert_indicator_to_dataframe( |
| 101 | + report_dict["indicator"][key + "_obj"].order_indicator_his |
| 102 | + ) |
| 103 | + cur_details = decision_details[decision_details.freq == key.rstrip("ute")].set_index(["instrument", "datetime"]) |
| 104 | + if len(cur_details) > 0: |
| 105 | + cur_details.pop("freq") |
| 106 | + report[key + "_obj"] = report[key + "_obj"].join(cur_details, how="outer") |
| 107 | + if "1minute" in report_dict["report"]: |
| 108 | + report["simulator"] = report_dict["report"]["1minute"][0] |
| 109 | + return report |
| 110 | + |
| 111 | + |
| 112 | +def single( |
| 113 | + backtest_config: dict, |
| 114 | + orders: pd.DataFrame, |
| 115 | + split: str = "stock", |
| 116 | + cash_limit: float = None, |
| 117 | + generate_report: bool = False, |
| 118 | +) -> Union[Tuple[pd.DataFrame, dict], pd.DataFrame]: |
| 119 | + if split == "stock": |
| 120 | + stock_id = orders.iloc[0].instrument |
| 121 | + init_qlib(backtest_config["qlib"], part=stock_id) |
| 122 | + else: |
| 123 | + day = orders.iloc[0].datetime |
| 124 | + init_qlib(backtest_config["qlib"], part=day) |
| 125 | + |
| 126 | + trade_start_time = orders["datetime"].min() |
| 127 | + trade_end_time = orders["datetime"].max() |
| 128 | + stocks = orders.instrument.unique().tolist() |
| 129 | + |
| 130 | + top_strategy_config = { |
| 131 | + "class": "FileOrderStrategy", |
| 132 | + "module_path": "qlib.contrib.strategy.rule_strategy", |
| 133 | + "kwargs": { |
| 134 | + "file": orders, |
| 135 | + "trade_range": TradeRangeByTime( |
| 136 | + pd.Timestamp(backtest_config["start_time"]).time(), |
| 137 | + pd.Timestamp(backtest_config["end_time"]).time(), |
| 138 | + ), |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + top_executor_config = _get_multi_level_executor_config( |
| 143 | + strategy_config=backtest_config["strategies"], |
| 144 | + cash_limit=cash_limit, |
| 145 | + generate_report=generate_report, |
| 146 | + ) |
| 147 | + |
| 148 | + tmp_backtest_config = copy.deepcopy(backtest_config["exchange"]) |
| 149 | + tmp_backtest_config.update( |
| 150 | + { |
| 151 | + "codes": stocks, |
| 152 | + "freq": "1min", |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + strategy, executor = get_strategy_executor( |
| 157 | + start_time=pd.Timestamp(trade_start_time), |
| 158 | + end_time=pd.Timestamp(trade_end_time) + pd.DateOffset(1), |
| 159 | + strategy=top_strategy_config, |
| 160 | + executor=top_executor_config, |
| 161 | + benchmark=None, |
| 162 | + account=cash_limit if cash_limit is not None else int(1e12), |
| 163 | + exchange_kwargs=tmp_backtest_config, |
| 164 | + pos_type="Position" if cash_limit is not None else "InfPosition", |
| 165 | + ) |
| 166 | + _set_env_for_all_strategy(executor=executor) |
| 167 | + |
| 168 | + report_dict: dict = {} |
| 169 | + decisions = list(collect_data_loop(trade_start_time, trade_end_time, strategy, executor, report_dict)) |
| 170 | + |
| 171 | + records = _convert_indicator_to_dataframe(report_dict["indicator"]["1day_obj"].order_indicator_his) |
| 172 | + assert records is None or not np.isnan(records["ffr"]).any() |
| 173 | + |
| 174 | + if generate_report: |
| 175 | + report = _generate_report(decisions, report_dict) |
| 176 | + if split == "stock": |
| 177 | + stock_id = orders.iloc[0].instrument |
| 178 | + report = {stock_id: report} |
| 179 | + else: |
| 180 | + day = orders.iloc[0].datetime |
| 181 | + report = {day: report} |
| 182 | + return records, report |
| 183 | + else: |
| 184 | + return records |
| 185 | + |
| 186 | + |
| 187 | +def backtest(backtest_config: dict) -> pd.DataFrame: |
| 188 | + order_df = read_order_file(backtest_config["order_file"]) |
| 189 | + |
| 190 | + cash_limit = backtest_config["exchange"].pop("cash_limit") |
| 191 | + generate_report = backtest_config["exchange"].pop("generate_report") |
| 192 | + |
| 193 | + stock_pool = order_df["instrument"].unique().tolist() |
| 194 | + stock_pool.sort() |
| 195 | + |
| 196 | + mp_config = {"n_jobs": backtest_config["concurrency"], "verbose": 10, "backend": "multiprocessing"} |
| 197 | + torch.set_num_threads(1) # https://github.com/pytorch/pytorch/issues/17199 |
| 198 | + res = Parallel(**mp_config)( |
| 199 | + delayed(single)( |
| 200 | + backtest_config=backtest_config, |
| 201 | + orders=order_df[order_df["instrument"] == stock].copy(), |
| 202 | + split="stock", |
| 203 | + cash_limit=cash_limit, |
| 204 | + generate_report=generate_report, |
| 205 | + ) |
| 206 | + for stock in stock_pool |
| 207 | + ) |
| 208 | + |
| 209 | + output_path = Path(backtest_config["output_dir"]) |
| 210 | + if generate_report: |
| 211 | + with (output_path / "report.pkl").open("wb") as f: |
| 212 | + report = {} |
| 213 | + for r in res: |
| 214 | + report.update(r[1]) |
| 215 | + pickle.dump(report, f) |
| 216 | + res = pd.concat([r[0] for r in res], 0) |
| 217 | + else: |
| 218 | + res = pd.concat(res) |
| 219 | + |
| 220 | + res.to_csv(output_path / "summary.csv") |
| 221 | + return res |
| 222 | + |
| 223 | + |
| 224 | +if __name__ == "__main__": |
| 225 | + import warnings |
| 226 | + |
| 227 | + warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 228 | + warnings.filterwarnings("ignore", category=RuntimeWarning) |
| 229 | + |
| 230 | + path = sys.argv[1] |
| 231 | + backtest(get_backtest_config_fromfile(path)) |
0 commit comments