Skip to content

Commit dea7a22

Browse files
committed
Add continue_on_error parameter to backtest methods to allow continuation on errors
1 parent 3e9fcd6 commit dea7a22

File tree

1 file changed

+40
-15
lines changed
  • investing_algorithm_framework/app

1 file changed

+40
-15
lines changed

investing_algorithm_framework/app/app.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ def run_vector_backtests(
801801
show_progress: bool = True,
802802
market: Optional[str] = None,
803803
trading_symbol: Optional[str] = None,
804+
continue_on_error: bool = False,
804805
) -> List[Backtest]:
805806
"""
806807
Run vectorized backtests for a set of strategies. The provided
@@ -848,6 +849,10 @@ def run_vector_backtests(
848849
trading_symbol (str): The trading symbol to use for the backtest.
849850
This is used to create a portfolio configuration if no
850851
portfolio configuration is provided in the strategy.
852+
continue_on_error (bool): Whether to continue running other
853+
backtests if an error occurs in one of the backtests. If set
854+
to True, the backtest will return an empty Backtest instance
855+
in case of an error. If set to False, the error will be raised.
851856
852857
Returns:
853858
List[Backtest]: List of Backtest instances for each strategy
@@ -956,7 +961,8 @@ def run_vector_backtest(
956961
show_data_initialization_progress: bool = True,
957962
initial_amount: float = None,
958963
market: str = None,
959-
trading_symbol: str = None
964+
trading_symbol: str = None,
965+
continue_on_error: bool = False,
960966
) -> Backtest:
961967
"""
962968
Run vectorized backtests for a strategy. The provided
@@ -1008,6 +1014,10 @@ def run_vector_backtest(
10081014
that the portfolio will start with. If not provided,
10091015
the initial amount from the portfolio configuration will
10101016
be used.
1017+
continue_on_error (bool): Whether to continue running other
1018+
backtests if an error occurs in one of the backtests. If set
1019+
to True, the backtest will return an empty Backtest instance
1020+
in case of an error. If set to False, the error will be raised.
10111021
10121022
Returns:
10131023
Backtest: Instance of Backtest
@@ -1040,21 +1050,36 @@ def run_vector_backtest(
10401050

10411051
backtest_service = self.container.backtest_service()
10421052
backtest_service.validate_strategy_for_vector_backtest(strategy)
1043-
run = backtest_service.create_vector_backtest(
1044-
strategy=strategy,
1045-
backtest_date_range=backtest_date_range,
1046-
risk_free_rate=risk_free_rate,
1047-
market=market,
1048-
trading_symbol=trading_symbol,
1049-
initial_amount=initial_amount
1050-
)
1051-
backtest = Backtest(
1052-
backtest_runs=[run],
1053-
risk_free_rate=risk_free_rate,
1054-
backtest_summary=generate_backtest_summary_metrics(
1055-
[run.backtest_metrics]
1053+
1054+
try:
1055+
run = backtest_service.create_vector_backtest(
1056+
strategy=strategy,
1057+
backtest_date_range=backtest_date_range,
1058+
risk_free_rate=risk_free_rate,
1059+
market=market,
1060+
trading_symbol=trading_symbol,
1061+
initial_amount=initial_amount
10561062
)
1057-
)
1063+
backtest = Backtest(
1064+
backtest_runs=[run],
1065+
risk_free_rate=risk_free_rate,
1066+
backtest_summary=generate_backtest_summary_metrics(
1067+
[run.backtest_metrics]
1068+
)
1069+
)
1070+
except Exception as e:
1071+
logger.error(
1072+
f"Error occurred during vector backtest for strategy "
1073+
f"{strategy.name}: {str(e)}"
1074+
)
1075+
if continue_on_error:
1076+
backtest = Backtest(
1077+
backtest_runs=[],
1078+
risk_free_rate=risk_free_rate,
1079+
backtest_summary={}
1080+
)
1081+
else:
1082+
raise e
10581083

10591084
# Add the metadata to the backtest
10601085
if metadata is None:

0 commit comments

Comments
 (0)