Skip to content

Commit 430d8bf

Browse files
committed
Add backtest report tests
1 parent 987bb84 commit 430d8bf

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

tests/app/backtesting/test_backtest_report.py

+92-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def run_strategy(self, algorithm, market_data):
1717

1818

1919
class Test(TestBase):
20-
20+
"""
21+
Collection of tests for backtest report operations
22+
"""
2123
def setUp(self) -> None:
2224
self.resource_dir = os.path.abspath(
2325
os.path.join(
@@ -36,6 +38,9 @@ def setUp(self) -> None:
3638
)
3739

3840
def test_report_csv_creation(self):
41+
"""
42+
Test if the backtest report is created as a CSV file
43+
"""
3944
app = create_app(
4045
config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir}
4146
)
@@ -65,11 +70,55 @@ def test_report_csv_creation(self):
6570
)
6671

6772
def test_report_csv_creation_without_strategy_identifier(self):
73+
"""
74+
Test if the backtest report is created as a CSV file
75+
when the strategy does not have an identifier
76+
"""
77+
app = create_app(
78+
config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir}
79+
)
80+
strategy = TestStrategy()
81+
strategy.strategy_id = None
82+
app.add_strategy(strategy)
83+
app.add_portfolio_configuration(
84+
PortfolioConfiguration(
85+
market="bitvavo",
86+
trading_symbol="EUR",
87+
initial_balance=1000
88+
)
89+
)
90+
report = app.backtest(
91+
start_date=datetime.utcnow() - timedelta(days=1),
92+
end_date=datetime.utcnow(),
93+
)
94+
95+
# Check if the backtest report exists
96+
self.assertTrue(
97+
os.path.isfile(
98+
os.path.join(
99+
self.resource_dir,
100+
"backtest_reports",
101+
f"report"
102+
f"_{report.created_at.strftime(DATETIME_FORMAT)}.csv"
103+
)
104+
)
105+
)
106+
107+
def test_report_csv_creation_with_multiple_strategies(self):
108+
"""
109+
Test if the backtest report is created as a CSV file
110+
when there are multiple strategies
111+
"""
68112
app = create_app(
69113
config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir}
70114
)
71115
strategy = TestStrategy()
72116
strategy.strategy_id = None
117+
118+
@app.strategy()
119+
def run_strategy(algorithm, market_data):
120+
pass
121+
73122
app.add_strategy(strategy)
74123
app.add_portfolio_configuration(
75124
PortfolioConfiguration(
@@ -78,6 +127,8 @@ def test_report_csv_creation_without_strategy_identifier(self):
78127
initial_balance=1000
79128
)
80129
)
130+
131+
self.assertEqual(2, len(app.strategies))
81132
report = app.backtest(
82133
start_date=datetime.utcnow() - timedelta(days=1),
83134
end_date=datetime.utcnow(),
@@ -94,3 +145,43 @@ def test_report_csv_creation_without_strategy_identifier(self):
94145
)
95146
)
96147
)
148+
149+
def test_report_csv_creation_with_multiple_strategies_with_id(self):
150+
"""
151+
Test if the backtest report is created as a CSV file
152+
when there are multiple strategies with identifiers
153+
"""
154+
app = create_app(
155+
config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir}
156+
)
157+
158+
@app.strategy()
159+
def run_strategy(algorithm, market_data):
160+
pass
161+
162+
app.add_strategy(TestStrategy)
163+
app.add_portfolio_configuration(
164+
PortfolioConfiguration(
165+
market="bitvavo",
166+
trading_symbol="EUR",
167+
initial_balance=1000
168+
)
169+
)
170+
171+
self.assertEqual(2, len(app.strategies))
172+
report = app.backtest(
173+
start_date=datetime.utcnow() - timedelta(days=1),
174+
end_date=datetime.utcnow(),
175+
)
176+
177+
# Check if the backtest report exists
178+
self.assertTrue(
179+
os.path.isfile(
180+
os.path.join(
181+
self.resource_dir,
182+
"backtest_reports",
183+
f"report_{report.identifier}"
184+
f"_{report.created_at.strftime(DATETIME_FORMAT)}.csv"
185+
)
186+
)
187+
)

0 commit comments

Comments
 (0)