forked from RL-MLDM/alphagen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktest.py
154 lines (129 loc) · 5.13 KB
/
backtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from typing import Optional, TypeVar, Callable, Optional
import os
import pickle
import warnings
import pandas as pd
from qlib.backtest import backtest, executor as exec
from qlib.contrib.evaluate import risk_analysis
from qlib.contrib.report.analysis_position import report_graph
from alphagen.data.expression import *
from alphagen_qlib.stock_data import StockData
from alphagen_generic.features import *
from alphagen_qlib.strategy import TopKSwapNStrategy
_T = TypeVar("_T")
def _create_parents(path: str) -> None:
dir = os.path.dirname(path)
if dir != "":
os.makedirs(dir, exist_ok=True)
def write_all_text(path: str, text: str) -> None:
_create_parents(path)
with open(path, "w") as f:
f.write(text)
def dump_pickle(path: str,
factory: Callable[[], _T],
invalidate_cache: bool = False) -> Optional[_T]:
if invalidate_cache or not os.path.exists(path):
_create_parents(path)
obj = factory()
with open(path, "wb") as f:
pickle.dump(obj, f)
return obj
class BacktestResult(dict):
sharpe: float
annual_return: float
max_drawdown: float
information_ratio: float
annual_excess_return: float
excess_max_drawdown: float
class QlibBacktest:
def __init__(
self,
benchmark: str = "SH000300",
top_k: int = 30,
n_drop: Optional[int] = None,
deal: str = "close",
open_cost: float = 0.0015,
close_cost: float = 0.0015,
min_cost: float = 5,
):
self._benchmark = benchmark
self._top_k = top_k
self._n_drop = n_drop if n_drop is not None else top_k
self._deal_price = deal
self._open_cost = open_cost
self._close_cost = close_cost
self._min_cost = min_cost
def run(
self,
prediction: pd.Series,
output_prefix: Optional[str] = None,
return_report: bool = False
) -> BacktestResult:
prediction = prediction.sort_index()
index: pd.MultiIndex = prediction.index.remove_unused_levels() # type: ignore
dates = index.levels[0]
def backtest_impl(last: int = -1):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
strategy=TopKSwapNStrategy(
K=self._top_k,
n_swap=self._n_drop,
signal=prediction,
min_hold_days=1,
only_tradable=True,
)
executor=exec.SimulatorExecutor(
time_per_step="day",
generate_portfolio_metrics=True
)
return backtest(
strategy=strategy,
executor=executor,
start_time=dates[0],
end_time=dates[last],
account=100_000_000,
benchmark=self._benchmark,
exchange_kwargs={
"limit_threshold": 0.095,
"deal_price": self._deal_price,
"open_cost": self._open_cost,
"close_cost": self._close_cost,
"min_cost": self._min_cost,
}
)[0]
try:
portfolio_metric = backtest_impl()
except IndexError:
print("Cannot backtest till the last day, trying again with one less day")
portfolio_metric = backtest_impl(-2)
report, _ = portfolio_metric["1day"] # type: ignore
result = self._analyze_report(report)
graph = report_graph(report, show_notebook=False)[0]
if output_prefix is not None:
dump_pickle(output_prefix + "-report.pkl", lambda: report, True)
dump_pickle(output_prefix + "-graph.pkl", lambda: graph, True)
write_all_text(output_prefix + "-result.json", result)
print(report)
print(result)
return report if return_report else result
def _analyze_report(self, report: pd.DataFrame) -> BacktestResult:
excess = risk_analysis(report["return"] - report["bench"] - report["cost"])["risk"]
returns = risk_analysis(report["return"] - report["cost"])["risk"]
def loc(series: pd.Series, field: str) -> float:
return series.loc[field] # type: ignore
return BacktestResult(
sharpe=loc(returns, "information_ratio"),
annual_return=loc(returns, "annualized_return"),
max_drawdown=loc(returns, "max_drawdown"),
information_ratio=loc(excess, "information_ratio"),
annual_excess_return=loc(excess, "annualized_return"),
excess_max_drawdown=loc(excess, "max_drawdown"),
)
if __name__ == "__main__":
qlib_backtest = QlibBacktest()
data = StockData(instrument='csi300',
start_time='2020-01-01',
end_time='2021-12-31')
expr = Mul(EMA(Sub(Delta(Mul(Log(open_),Constant(-30.0)),50),Constant(-0.01)),40),Mul(Div(Abs(EMA(low,50)),close),Constant(0.01)))
data_df = data.make_dataframe(expr.evaluate(data))
qlib_backtest.run(data_df)