33
44from __future__ import division
55from __future__ import print_function
6- from logging import warn
76
87import numpy as np
98import pandas as pd
@@ -130,7 +129,7 @@ def backtest_daily(
130129 benchmark : str = "SH000300" ,
131130 exchange_kwargs : dict = None ,
132131 pos_type : str = "Position" ,
133- ) -> pd . DataFrame :
132+ ):
134133 """initialize the strategy and executor, then executor the backtest of daily frequency
135134
136135 Parameters
@@ -187,13 +186,28 @@ def backtest_daily(
187186 Using Account with a Position
188187 exchange_kwargs : dict
189188 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+
190202 pos_type : str
191203 the type of Position.
192204
193205 Returns
194206 -------
195- analysis_df: pd.DataFrame
196- backtest results
207+ report_normal: pd.DataFrame
208+ backtest report
209+ positions_normal: pd.DataFrame
210+ backtest positions
197211
198212 """
199213 freq = "day"
@@ -205,8 +219,8 @@ def backtest_daily(
205219 executor = _executor .SimulatorExecutor (** executor_config )
206220 _exchange_kwargs = {
207221 "freq" : freq ,
208- "limit_threshold" : 0.095 ,
209- "deal_price" : "close" ,
222+ "limit_threshold" : None ,
223+ "deal_price" : None ,
210224 "open_cost" : 0.0005 ,
211225 "close_cost" : 0.0015 ,
212226 "min_cost" : 5 ,
@@ -227,16 +241,8 @@ def backtest_daily(
227241 analysis_freq = "{0}{1}" .format (* Freq .parse (freq ))
228242
229243 report_normal , positions_normal = portfolio_metric_dict .get (analysis_freq )
230- analysis = dict ()
231- analysis ["excess_return_without_cost" ] = risk_analysis (
232- report_normal ["return" ] - report_normal ["bench" ], freq = analysis_freq
233- )
234- analysis ["excess_return_with_cost" ] = risk_analysis (
235- report_normal ["return" ] - report_normal ["bench" ] - report_normal ["cost" ], freq = analysis_freq
236- )
237244
238- analysis_df = pd .concat (analysis ) # type: pd.DataFrame
239- return analysis_df
245+ return report_normal , positions_normal
240246
241247
242248def long_short_backtest (
@@ -374,12 +380,7 @@ def t_run():
374380 "n_drop" : 5 ,
375381 "signal" : pred ,
376382 }
377- portfolio_metric_dict , indicator_dict = backtest_daily (
378- start_time = "2017-01-01" , end_time = "2020-08-01" , strategy = strategy_config
379- )
380- freq = "day"
381- analysis_freq = "{0}{1}" .format (* Freq .parse (freq ))
382- 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 )
383384 print (report_df .head ())
384385 print (positions .keys ())
385386 print (positions [list (positions .keys ())[0 ]])
0 commit comments