Skip to content

Commit 02a4b21

Browse files
committed
Fix backtest report writer
1 parent d0eed34 commit 02a4b21

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

investing_algorithm_framework/services/backtesting/backtest_report_writer_service.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ def write_report_to_csv(
2222
if not os.path.exists(output_directory):
2323
os.makedirs(output_directory)
2424

25-
csv_file_path = os.path.join(
26-
output_directory,
27-
f"report_{report.identifier}_"
28-
f"{report.created_at.strftime(DATETIME_FORMAT)}.csv"
29-
)
25+
if report.identifier is None:
26+
csv_file_path = os.path.join(
27+
output_directory,
28+
f"report_"
29+
f"{report.created_at.strftime(DATETIME_FORMAT)}.csv"
30+
)
31+
else:
32+
csv_file_path = os.path.join(
33+
output_directory,
34+
f"report_{report.identifier}_"
35+
f"{report.created_at.strftime(DATETIME_FORMAT)}.csv"
36+
)
3037
report_dict = report.to_dict()
3138

3239
with open(csv_file_path, 'w', newline='') as csv_file:

investing_algorithm_framework/services/backtesting/backtest_service.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,16 @@ def create_backtest_report(
184184
"""
185185
for portfolio in self._portfolio_repository.get_all():
186186
ids = [strategy.strategy_id for strategy in algorithm.strategies]
187-
identifier = '_'.join(ids)
187+
188+
# Check if strategy_id is None
189+
if None in ids:
190+
# Remove None from ids
191+
ids = [x for x in ids if x is not None]
192+
193+
if len(ids) != 0:
194+
identifier = '_'.join(ids)
195+
else:
196+
identifier = None
188197

189198
backtest_profile = BacktestReport(
190199
identifier=identifier,

tests/app/backtesting/test_backtest_report.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,34 @@ def test_report_csv_creation(self):
6363
)
6464
)
6565
)
66+
67+
def test_report_csv_creation_without_strategy_identifier(self):
68+
app = create_app(
69+
config={"test": "test", RESOURCE_DIRECTORY: self.resource_dir}
70+
)
71+
strategy = TestStrategy()
72+
strategy.strategy_id = None
73+
app.add_strategy(strategy)
74+
app.add_portfolio_configuration(
75+
PortfolioConfiguration(
76+
market="bitvavo",
77+
trading_symbol="EUR",
78+
initial_balance=1000
79+
)
80+
)
81+
report = app.backtest(
82+
start_date=datetime.utcnow() - timedelta(days=1),
83+
end_date=datetime.utcnow(),
84+
)
85+
86+
# Check if the backtest report exists
87+
self.assertTrue(
88+
os.path.isfile(
89+
os.path.join(
90+
self.resource_dir,
91+
"backtest_reports",
92+
f"report"
93+
f"_{report.created_at.strftime(DATETIME_FORMAT)}.csv"
94+
)
95+
)
96+
)

0 commit comments

Comments
 (0)