Skip to content

Commit

Permalink
Add backtest report tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Mar 5, 2024
1 parent 987bb84 commit 430d8bf
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion tests/app/backtesting/test_backtest_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}
)
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
Expand All @@ -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"
)
)
)

0 comments on commit 430d8bf

Please sign in to comment.