Skip to content

Commit 4dace84

Browse files
committed
modify backtest_daily's docstring
1 parent b288550 commit 4dace84

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

docs/component/strategy.rst

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Usage & Example
102102
qlib.init(provider_uri=<qlib data dir>)
103103
104104
CSI300_BENCH = "SH000300"
105-
FREQ = "day"
106105
STRATEGY_CONFIG = {
107106
"topk": 50,
108107
"n_drop": 5,
@@ -112,12 +111,9 @@ Usage & Example
112111
113112
114113
strategy_obj = TopkDropoutStrategy(**STRATEGY_CONFIG)
115-
portfolio_metric_dict, indicator_dict = backtest_daily(
116-
start_time="2017-01-01", end_time="2020-08-01", strategy=strategy_obj, executor=executor_obj
114+
report_normal, positions_normal = backtest_daily(
115+
start_time="2017-01-01", end_time="2020-08-01", strategy=strategy_obj
117116
)
118-
analysis_freq = "{0}{1}".format(*Freq.parse(FREQ))
119-
120-
report_normal, positions_normal = portfolio_metric_dict.get(analysis_freq)
121117
analysis = dict()
122118
analysis["excess_return_without_cost"] = risk_analysis(
123119
report_normal["return"] - report_normal["bench"], freq=analysis_freq
@@ -127,15 +123,7 @@ Usage & Example
127123
)
128124
129125
analysis_df = pd.concat(analysis) # type: pd.DataFrame
130-
# log metrics
131-
analysis_dict = flatten_dict(analysis_df["risk"].unstack().T.to_dict())
132-
# print out results
133-
pprint(f"The following are analysis results of benchmark return({analysis_freq}).")
134-
pprint(risk_analysis(report_normal["bench"], freq=analysis_freq))
135-
pprint(f"The following are analysis results of the excess return without cost({analysis_freq}).")
136-
pprint(analysis["excess_return_without_cost"])
137-
pprint(f"The following are analysis results of the excess return with cost({analysis_freq}).")
138-
pprint(analysis["excess_return_with_cost"])
126+
pprint(analysis_df)
139127
140128
141129

qlib/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ def set_conf_from_C(self, config_c):
244244
_default_region_config = {
245245
REG_CN: {
246246
"trade_unit": 100,
247-
"limit_threshold": 0.099,
248-
"deal_price": "vwap",
247+
"limit_threshold": 0.095,
248+
"deal_price": "close",
249249
},
250250
REG_US: {
251251
"trade_unit": 1,

qlib/contrib/evaluate.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from __future__ import division
55
from __future__ import print_function
6-
from logging import warn
76

87
import numpy as np
98
import pandas as pd
@@ -144,6 +143,37 @@ def backtest_daily(
144143
E.g. Executor[day](Executor[1min]), setting `end_time == 20XX0301` will include all the minutes on 20XX0301
145144
strategy : Union[str, dict, BaseStrategy]
146145
for initializing outermost portfolio strategy. Please refer to the docs of init_instance_by_config for more information.
146+
147+
E.g.
148+
149+
.. code-block:: python
150+
# dict
151+
strategy = {
152+
"class": "TopkDropoutStrategy",
153+
"module_path": "qlib.contrib.strategy.signal_strategy",
154+
"kwargs": {
155+
"signal": (model, dataset),
156+
"topk": 50,
157+
"n_drop": 5,
158+
},
159+
}
160+
# BaseStrategy
161+
pred_score = pd.read_pickle("score.pkl")["score"]
162+
STRATEGY_CONFIG = {
163+
"topk": 50,
164+
"n_drop": 5,
165+
"signal": pred_score,
166+
}
167+
strategy = TopkDropoutStrategy(**STRATEGY_CONFIG)
168+
# str example.
169+
# 1) specify a pickle object
170+
# - path like 'file:///<path to pickle file>/obj.pkl'
171+
# 2) specify a class name
172+
# - "ClassName": getattr(module, "ClassName")() will be used.
173+
# 3) specify module path with class name
174+
# - "a.b.c.ClassName" getattr(<a.b.c.module>, "ClassName")() will be used.
175+
176+
147177
executor : Union[str, dict, BaseExecutor]
148178
for initializing the outermost executor.
149179
benchmark: str
@@ -156,16 +186,28 @@ def backtest_daily(
156186
Using Account with a Position
157187
exchange_kwargs : dict
158188
the kwargs for initializing Exchange
189+
E.g.
190+
191+
.. code-block:: python
192+
193+
exchange_kwargs = {
194+
"freq": freq,
195+
"limit_threshold": None, # limit_threshold is None, using C.limit_threshold
196+
"deal_price": None, # deal_price is None, using C.deal_price
197+
"open_cost": 0.0005,
198+
"close_cost": 0.0015,
199+
"min_cost": 5,
200+
}
201+
159202
pos_type : str
160203
the type of Position.
161204
162205
Returns
163206
-------
164-
portfolio_metrics_dict: Dict[PortfolioMetrics]
165-
it records the trading portfolio_metrics information
166-
indicator_dict: Dict[Indicator]
167-
it computes the trading indicator
168-
It is organized in a dict format
207+
report_normal: pd.DataFrame
208+
backtest report
209+
positions_normal: pd.DataFrame
210+
backtest positions
169211
170212
"""
171213
freq = "day"
@@ -177,8 +219,8 @@ def backtest_daily(
177219
executor = _executor.SimulatorExecutor(**executor_config)
178220
_exchange_kwargs = {
179221
"freq": freq,
180-
"limit_threshold": 0.095,
181-
"deal_price": "close",
222+
"limit_threshold": None,
223+
"deal_price": None,
182224
"open_cost": 0.0005,
183225
"close_cost": 0.0015,
184226
"min_cost": 5,
@@ -196,7 +238,11 @@ def backtest_daily(
196238
exchange_kwargs=_exchange_kwargs,
197239
pos_type=pos_type,
198240
)
199-
return portfolio_metric_dict, indicator_dict
241+
analysis_freq = "{0}{1}".format(*Freq.parse(freq))
242+
243+
report_normal, positions_normal = portfolio_metric_dict.get(analysis_freq)
244+
245+
return report_normal, positions_normal
200246

201247

202248
def long_short_backtest(
@@ -334,12 +380,7 @@ def t_run():
334380
"n_drop": 5,
335381
"signal": pred,
336382
}
337-
portfolio_metric_dict, indicator_dict = backtest_daily(
338-
start_time="2017-01-01", end_time="2020-08-01", strategy=strategy_config
339-
)
340-
freq = "day"
341-
analysis_freq = "{0}{1}".format(*Freq.parse(freq))
342-
report_df, positions = portfolio_metric_dict.get(analysis_freq)
383+
report_df, positions = backtest_daily(start_time="2017-01-01", end_time="2020-08-01", strategy=strategy_config)
343384
print(report_df.head())
344385
print(positions.keys())
345386
print(positions[list(positions.keys())[0]])

0 commit comments

Comments
 (0)