Skip to content

Commit

Permalink
Check if market data source is not None
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Apr 4, 2024
1 parent fafd7e6 commit a0b380f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
4 changes: 3 additions & 1 deletion investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ def _initialize_app_for_backtest(
backtest_market_data_sources = [
market_data_source.to_backtest_market_data_source()
for market_data_source in market_data_sources
if market_data_source is not None
]

for market_data_source in backtest_market_data_sources:
market_data_source.config = self.config
if market_data_source is not None:
market_data_source.config = self.config

self.container.market_data_source_service.override(
BacktestMarketDataSourceService(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta

from dateutil import parser
import pandas as pd
from tqdm import tqdm

Expand Down Expand Up @@ -68,8 +68,6 @@ def run_backtest(
strategy_profiles = []
portfolios = self._portfolio_repository.get_all()
initial_unallocated = 0
config = self._configuration_service.config
datetime_format = config.get("DATETIME_FORMAT")

for portfolio in portfolios:
initial_unallocated += portfolio.unallocated
Expand Down Expand Up @@ -100,7 +98,7 @@ def run_backtest(
strategy_profile = self.get_strategy_from_strategy_profiles(
strategy_profiles, row['id']
)
index_date = datetime.strptime(str(index), datetime_format)
index_date = parser.parse(str(index))
self.run_backtest_for_profile(
algorithm=algorithm,
strategy=algorithm.get_strategy(strategy_profile.strategy_id),
Expand Down
7 changes: 7 additions & 0 deletions tests/app/backtesting/test_run_backtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def test_run_backtests(self):
initial_balance=1000
)
)
start_date = datetime.utcnow() - timedelta(days=1)
end_date = datetime.utcnow()
app._initialize_app_for_backtest(
backtest_start_date=start_date,
backtest_end_date=end_date,
pending_order_check_interval='2h'
)
reports = app.run_backtests(
algorithms=[algorithm_one, algorithm_two, algorithm_three],
start_date=datetime.utcnow() - timedelta(days=1),
Expand Down
3 changes: 1 addition & 2 deletions tests/resources/stubs/market_service_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ class MarketServiceStub(MarketService):
'EUR': 1000,
}

def __init__(self, config, market_credential_service):
def __init__(self, market_credential_service):
super().__init__(
market_credential_service=market_credential_service,
config=config
)
self._market_credential_service = market_credential_service
self._orders = []
Expand Down
32 changes: 16 additions & 16 deletions tests/services/test_order_backtest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def test_has_executed_buy_order(self):
"Low": 0.24262,
"Close": 0.24262,
"Volume": 0.24262,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -532,7 +532,7 @@ def test_has_executed_buy_order(self):
"Low": 0.24162,
"Close": 0.24162,
"Volume": 0.24162,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -552,7 +552,7 @@ def test_has_executed_buy_order(self):
"Low": 0.24362,
"Close": 0.24362,
"Volume": 0.24362,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -572,15 +572,15 @@ def test_has_executed_buy_order(self):
"Low": 0.24462,
"Close": 0.24462,
"Volume": 0.24462,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24300,
"High": 0.24300,
"Low": 0.24300,
"Close": 0.24300,
"Volume": 0.24300,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -599,23 +599,23 @@ def test_has_executed_buy_order(self):
"Low": 0.24162,
"Close": 0.24162,
"Volume": 0.24162,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24200,
"High": 0.24200,
"Low": 0.24200,
"Close": 0.24200,
"Volume": 0.24200,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24300,
"High": 0.24300,
"Low": 0.24300,
"Close": 0.24300,
"Volume": 0.24300,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand Down Expand Up @@ -688,7 +688,7 @@ def test_has_executed_sell_order(self):
"Low": 0.24262,
"Close": 0.24262,
"Volume": 0.24262,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -707,7 +707,7 @@ def test_has_executed_sell_order(self):
"Low": 0.24362,
"Close": 0.24362,
"Volume": 0.24362,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -727,7 +727,7 @@ def test_has_executed_sell_order(self):
"Low": 0.24162,
"Close": 0.24162,
"Volume": 0.24162,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -747,15 +747,15 @@ def test_has_executed_sell_order(self):
"Low": 0.24162,
"Close": 0.24162,
"Volume": 0.24162,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24000,
"High": 0.24000,
"Low": 0.24000,
"Close": 0.24000,
"Volume": 0.24000,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand All @@ -774,23 +774,23 @@ def test_has_executed_sell_order(self):
"Low": 0.24300,
"Close": 0.24300,
"Volume": 0.24300,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24162,
"High": 0.24162,
"Low": 0.24162,
"Close": 0.24162,
"Volume": 0.24162,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
},
{
"Open": 0.24000,
"High": 0.24000,
"Low": 0.24000,
"Close": 0.24000,
"Volume": 0.24000,
"Timestamp": datetime.now(tz=tzutc())
"Timestamp": datetime.now()
}
]
ohlcv_df = pd.DataFrame(
Expand Down

0 comments on commit a0b380f

Please sign in to comment.