33
44from __future__ import division
55from __future__ import print_function
6- from logging import warn
76
87import numpy as np
98import 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
202248def 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