Skip to content

Commit 0f8f96a

Browse files
committed
Fix snapshot create at attribute setting
1 parent 8cb3521 commit 0f8f96a

10 files changed

+56
-12
lines changed

investing_algorithm_framework/app/app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ def initialize(self):
391391
configuration_service.add_value(APP_MODE, AppMode.WEB.value)
392392
self._initialize_web()
393393

394-
self._initialize_portfolios()
394+
if not Environment.BACKTEST.equals(config[ENVIRONMENT]):
395+
self._initialize_portfolios()
395396

396397
def run(self, payload: dict = None, number_of_iterations: int = None):
397398
"""
@@ -710,7 +711,7 @@ def run_backtest(
710711
save_strategy=False,
711712
) -> BacktestReport:
712713
"""
713-
Run a backtest for an algorithm. An
714+
Run a backtest for an algorithm.
714715
715716
Args:
716717
backtest_date_range: The date range to run the backtest for
@@ -886,7 +887,6 @@ def run_backtests(
886887
)
887888

888889
if report is not None:
889-
890890
print(
891891
f"{COLOR_YELLOW}Backtest already exists "
892892
f"for algorithm {algorithm.name} date "

investing_algorithm_framework/domain/utils/backtesting.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,6 @@ def get_backtest_report(
11501150
# Loop through all files in the directory
11511151
for root, _, files in os.walk(directory):
11521152
for file in files:
1153-
print(file)
11541153

11551154
# Check if the file is a directory
11561155
if os.path.isdir(os.path.join(root, file)):
@@ -1185,8 +1184,6 @@ def get_backtest_report(
11851184
return report
11861185

11871186
elif file.endswith(".json"):
1188-
print("found file")
1189-
print(f"Found backtest report file: {file}")
11901187
# Read the file
11911188
with open(os.path.join(root, file), "r") as json_file:
11921189
# Parse the JSON file

investing_algorithm_framework/services/backtesting/backtest_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def run_backtest(
123123

124124
# Check if the portfolio configuration has an initial balance
125125
self._portfolio_service.create_portfolio_from_configuration(
126-
portfolio_configuration, initial_amount=initial_amount
126+
portfolio_configuration,
127+
initial_amount=initial_amount,
128+
created_at=backtest_date_range.start_date,
127129
)
128130

129131
strategy_profiles = []

investing_algorithm_framework/services/portfolios/backtest_portfolio_service.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime
12
from investing_algorithm_framework.domain import PortfolioConfiguration, \
23
OperationalException
34
from .portfolio_service import PortfolioService
@@ -10,8 +11,10 @@ class BacktestPortfolioService(PortfolioService):
1011
not check if the initial balance is present on the exchange or broker.
1112
"""
1213
def create_portfolio_from_configuration(
13-
self, portfolio_configuration: PortfolioConfiguration,
14-
initial_amount=None
14+
self,
15+
portfolio_configuration: PortfolioConfiguration,
16+
initial_amount=None,
17+
created_at: datetime = None
1518
):
1619
"""
1720
Wil create a portfolio from a portfolio configuration for backtesting.
@@ -20,6 +23,9 @@ def create_portfolio_from_configuration(
2023
portfolio_configuration (PortfolioConfiguration):
2124
Portfolio configuration to create the portfolio from
2225
initial_amount (Decimal): Initial balance for the portfolio
26+
created_at (datetime): The date and time when the portfolio
27+
is created. If not provided, the current date and time
28+
will be used.
2329
2430
Returns:
2531
Portfolio: The created portfolio
@@ -41,6 +47,7 @@ def create_portfolio_from_configuration(
4147
"market": portfolio_configuration.market,
4248
"trading_symbol": portfolio_configuration.trading_symbol,
4349
"unallocated": amount,
44-
"initialized": True
50+
"initialized": True,
51+
"created_at": created_at
4552
}
4653
return self.create(data)

investing_algorithm_framework/services/portfolios/portfolio_service.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def create_snapshot(self, portfolio_id, created_at=None):
110110
)
111111

112112
def create_portfolio_from_configuration(
113-
self, portfolio_configuration, initial_amount=None
113+
self,
114+
portfolio_configuration,
115+
initial_amount=None,
116+
created_at: datetime = None
114117
) -> Portfolio:
115118
"""
116119
Method to create a portfolio based on a portfolio configuration.
@@ -123,6 +126,9 @@ def create_portfolio_from_configuration(
123126
portfolio_configuration (PortfolioConfiguration)
124127
Portfolio configuration to create the portfolio from
125128
initial_amount (Decimal): Initial balance for the portfolio
129+
created_at (datetime): The date and time when the portfolio
130+
is created. If not provided, the current date and time
131+
will be used.
126132
127133
Returns:
128134
Portfolio: Portfolio created from the configuration
@@ -158,6 +164,9 @@ def create_portfolio_from_configuration(
158164
)
159165
data = portfolio.to_dict()
160166

167+
if created_at is not None:
168+
data["created_at"] = created_at
169+
161170
if initial_amount is not None:
162171
data["unallocated"] = initial_amount
163172

tests/scenarios/test_run_backtest_algorithm_param.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ def test_run(self):
5353
end_time = time.time()
5454
elapsed_time = end_time - start_time
5555
print(f"Test completed in {elapsed_time:.2f} seconds")
56+
57+
snapshots = backtest_report.get_snapshots()
58+
# Check that the first two snapshots created at are the same
59+
# as the start date of the backtest
60+
self.assertEqual(
61+
snapshots[0].created_at.replace(tzinfo=timezone.utc),
62+
start_date.replace(tzinfo=timezone.utc)
63+
)

tests/scenarios/test_run_backtest_multiple_strategies_param.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ def test_run(self):
4848
self.assertAlmostEqual(
4949
backtest_report.get_profit_percentage(), 4.5, delta=0.5
5050
)
51+
snapshots = backtest_report.get_snapshots()
52+
# Check that the first two snapshots created at are the same
53+
# as the start date of the backtest
54+
self.assertEqual(
55+
snapshots[0].created_at.replace(tzinfo=timezone.utc),
56+
start_date.replace(tzinfo=timezone.utc)
57+
)

tests/scenarios/test_run_backtest_single_strategy_param.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ def test_run(self):
4646
self.assertAlmostEqual(
4747
backtest_report.get_profit_percentage(), 0.8, delta=0.1
4848
)
49+
snapshots = backtest_report.get_snapshots()
50+
# Check that the first two snapshots created at are the same
51+
# as the start date of the backtest
52+
self.assertEqual(
53+
snapshots[0].created_at.replace(tzinfo=timezone.utc),
54+
start_date.replace(tzinfo=timezone.utc)
55+
)

tests/scenarios/test_run_backtest_strategies_attribute.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ def test_run(self):
4646
self.assertAlmostEqual(
4747
backtest_report.get_profit_percentage(), 0.8, delta=0.1
4848
)
49+
snapshots = backtest_report.get_snapshots()
50+
# Check that the first two snapshots created at are the same
51+
# as the start date of the backtest
52+
self.assertEqual(
53+
snapshots[0].created_at.replace(tzinfo=timezone.utc),
54+
start_date.replace(tzinfo=timezone.utc)
55+
)

tests/scenarios/test_run_backtests_algorithms_param.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ def test_run(self):
6969
)
7070
self.assertAlmostEqual(
7171
backtest_report_two.get_profit_percentage(), 4.5, delta=0.2
72-
)
72+
)

0 commit comments

Comments
 (0)