Skip to content

Commit aaeb16b

Browse files
committed
Fix the run_backtests method to return a list of Backtest instances
1 parent 8fda874 commit aaeb16b

File tree

3 files changed

+453
-33
lines changed

3 files changed

+453
-33
lines changed

examples/backtest_example/run_backtest.ipynb

Lines changed: 434 additions & 19 deletions
Large diffs are not rendered by default.

investing_algorithm_framework/app/app.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
BacktestMarketDataSourceService, BacktestPortfolioService, \
2828
BacktestService
2929
from .app_hook import AppHook
30-
from .reporting import BacktestReport
3130

3231
logger = logging.getLogger("investing_algorithm_framework")
3332
COLOR_RESET = '\033[0m'
@@ -762,7 +761,7 @@ def run_backtest(
762761
date and time.
763762
764763
Returns:
765-
Instance of BacktestReport
764+
Backtest: Instance of Backtest
766765
"""
767766

768767
# Add backtest configuration to the config
@@ -853,16 +852,18 @@ def run_backtest(
853852
def run_backtests(
854853
self,
855854
algorithms=None,
856-
strategies=None,
855+
strategies: List[TradingStrategy] = None,
857856
initial_amount=None,
858857
backtest_date_ranges: List[BacktestDateRange] = None,
858+
snapshot_interval: SnapshotInterval = SnapshotInterval.TRADE_CLOSE,
859+
risk_free_rate: Optional[float] = None,
860+
save=True,
859861
output_directory=None,
860862
checkpoint=False,
861-
862-
) -> List[BacktestReport]:
863+
) -> List[Backtest]:
863864
"""
864-
Run a backtest for a set algorithm. This method should be called when
865-
running a backtest.
865+
Run a set of backtests for the provided algorithms or strategies
866+
with the given date ranges.
866867
867868
Args:
868869
algorithms (List[Algorithm]) (Optional): The algorithms to run
@@ -885,14 +886,15 @@ def run_backtests(
885886
when running backtests for a large number of algorithms
886887
and date ranges where some of the backtests may fail
887888
and you want to re-run only the failed backtests.
888-
save_strategy: bool - Whether to save the strategy as part
889-
of the backtest report. You can only save in-memory strategies
890-
when running multiple backtests. This is because we can't
891-
differentiate between which folders belong to a specific
892-
strategy.
889+
save (bool): Whether to save the backtest report
890+
to the output directory. If True, then the backtest report
891+
will be saved to the output directory.
892+
snapshot_interval (SnapshotInterval): The snapshot interval to
893+
use for the backtest. This is used to determine how often
894+
the portfolio snapshot should be taken during the backtest.
893895
894896
Returns
895-
List of BacktestReport instances
897+
List[Backtest]: List of Backtest instances
896898
"""
897899
logger.info("Initializing backtests")
898900
reports = []
@@ -959,6 +961,9 @@ def run_backtests(
959961
initial_amount=initial_amount,
960962
output_directory=output_directory,
961963
algorithm=algorithm,
964+
save=save,
965+
snapshot_interval=snapshot_interval,
966+
risk_free_rate=risk_free_rate
962967
)
963968
reports.append(report)
964969

tests/scenarios/test_run_backtests_algorithms_param.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_run(self):
3030
Algorithm(strategies=[CrossOverStrategyV1]),
3131
Algorithm(strategies=[CrossOverStrategyV3]),
3232
],
33-
save_strategy=True,
33+
save=True,
3434
)
3535
backtest_report = backtest_reports[0]
3636
self.assertAlmostEqual(

0 commit comments

Comments
 (0)