From 430d8bf421dcd8d2511e6456bfc7bbc1aee2c786 Mon Sep 17 00:00:00 2001 From: marcvanduyn Date: Tue, 5 Mar 2024 11:09:52 +0100 Subject: [PATCH] Add backtest report tests --- tests/app/backtesting/test_backtest_report.py | 93 ++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/tests/app/backtesting/test_backtest_report.py b/tests/app/backtesting/test_backtest_report.py index 238b15c8..78eeed3c 100644 --- a/tests/app/backtesting/test_backtest_report.py +++ b/tests/app/backtesting/test_backtest_report.py @@ -17,7 +17,9 @@ def run_strategy(self, algorithm, market_data): class Test(TestBase): - + """ + Collection of tests for backtest report operations + """ def setUp(self) -> None: self.resource_dir = os.path.abspath( os.path.join( @@ -36,6 +38,9 @@ def setUp(self) -> None: ) def test_report_csv_creation(self): + """ + Test if the backtest report is created as a CSV file + """ app = create_app( config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir} ) @@ -65,11 +70,55 @@ def test_report_csv_creation(self): ) def test_report_csv_creation_without_strategy_identifier(self): + """ + Test if the backtest report is created as a CSV file + when the strategy does not have an identifier + """ + app = create_app( + config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir} + ) + strategy = TestStrategy() + strategy.strategy_id = None + app.add_strategy(strategy) + app.add_portfolio_configuration( + PortfolioConfiguration( + market="bitvavo", + trading_symbol="EUR", + initial_balance=1000 + ) + ) + report = app.backtest( + start_date=datetime.utcnow() - timedelta(days=1), + end_date=datetime.utcnow(), + ) + + # Check if the backtest report exists + self.assertTrue( + os.path.isfile( + os.path.join( + self.resource_dir, + "backtest_reports", + f"report" + f"_{report.created_at.strftime(DATETIME_FORMAT)}.csv" + ) + ) + ) + + def test_report_csv_creation_with_multiple_strategies(self): + """ + Test if the backtest report is created as a CSV file + when there are multiple strategies + """ app = create_app( config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir} ) strategy = TestStrategy() strategy.strategy_id = None + + @app.strategy() + def run_strategy(algorithm, market_data): + pass + app.add_strategy(strategy) app.add_portfolio_configuration( PortfolioConfiguration( @@ -78,6 +127,8 @@ def test_report_csv_creation_without_strategy_identifier(self): initial_balance=1000 ) ) + + self.assertEqual(2, len(app.strategies)) report = app.backtest( start_date=datetime.utcnow() - timedelta(days=1), end_date=datetime.utcnow(), @@ -94,3 +145,43 @@ def test_report_csv_creation_without_strategy_identifier(self): ) ) ) + + def test_report_csv_creation_with_multiple_strategies_with_id(self): + """ + Test if the backtest report is created as a CSV file + when there are multiple strategies with identifiers + """ + app = create_app( + config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir} + ) + + @app.strategy() + def run_strategy(algorithm, market_data): + pass + + app.add_strategy(TestStrategy) + app.add_portfolio_configuration( + PortfolioConfiguration( + market="bitvavo", + trading_symbol="EUR", + initial_balance=1000 + ) + ) + + self.assertEqual(2, len(app.strategies)) + report = app.backtest( + start_date=datetime.utcnow() - timedelta(days=1), + end_date=datetime.utcnow(), + ) + + # Check if the backtest report exists + self.assertTrue( + os.path.isfile( + os.path.join( + self.resource_dir, + "backtest_reports", + f"report_{report.identifier}" + f"_{report.created_at.strftime(DATETIME_FORMAT)}.csv" + ) + ) + )