Improve backtest lazy loading and test automation
This commit is contained in:
@@ -29,6 +29,7 @@ from app.services.backtesting.service import BacktestService
|
||||
from app.services.strategy_templates import StrategyTemplateService
|
||||
|
||||
SUPPORTED_BACKTEST_PAGE_SYMBOLS = ("GLD", "GC", "XAU")
|
||||
SUPPORTED_DATABENTO_BACKTEST_PAGE_SYMBOLS = ("GLD", "XAU")
|
||||
|
||||
|
||||
def _validate_initial_collateral(underlying_units: float, entry_spot: float, loan_amount: float) -> None:
|
||||
@@ -134,6 +135,15 @@ class BacktestPageService:
|
||||
self._yfinance_provider = YFinanceHistoricalPriceSource()
|
||||
return self._yfinance_provider
|
||||
|
||||
@staticmethod
|
||||
def validate_data_source_support(symbol: str, data_source: str) -> None:
|
||||
normalized_symbol = symbol.strip().upper()
|
||||
if data_source == "databento" and normalized_symbol not in SUPPORTED_DATABENTO_BACKTEST_PAGE_SYMBOLS:
|
||||
raise ValueError(
|
||||
"Databento backtests currently support GLD and XAU only. "
|
||||
"GC futures remain unavailable on the backtest page until contract mapping is wired."
|
||||
)
|
||||
|
||||
def get_historical_prices(
|
||||
self, symbol: str, start_date: date, end_date: date, data_source: str
|
||||
) -> list[DailyClosePoint]:
|
||||
@@ -148,6 +158,7 @@ class BacktestPageService:
|
||||
Returns:
|
||||
List of daily close points sorted by date
|
||||
"""
|
||||
self.validate_data_source_support(symbol, data_source)
|
||||
if data_source == "databento":
|
||||
return self._get_databento_provider().load_daily_closes(symbol, start_date, end_date)
|
||||
elif data_source == "yfinance":
|
||||
@@ -171,6 +182,8 @@ class BacktestPageService:
|
||||
if data_source != "databento":
|
||||
return 0.0
|
||||
|
||||
self.validate_data_source_support(symbol, data_source)
|
||||
|
||||
try:
|
||||
provider = self._get_databento_provider()
|
||||
return provider.get_cost_estimate(symbol, start_date, end_date)
|
||||
@@ -213,6 +226,8 @@ class BacktestPageService:
|
||||
if data_source != "databento":
|
||||
return None, None
|
||||
|
||||
self.validate_data_source_support(symbol, data_source)
|
||||
|
||||
try:
|
||||
provider = self._get_databento_provider()
|
||||
return provider.get_available_range(symbol)
|
||||
@@ -258,6 +273,7 @@ class BacktestPageService:
|
||||
raise ValueError("Symbol is required")
|
||||
if normalized_symbol not in SUPPORTED_BACKTEST_PAGE_SYMBOLS:
|
||||
raise ValueError(f"Backtests support symbols: {', '.join(SUPPORTED_BACKTEST_PAGE_SYMBOLS)}")
|
||||
self.validate_data_source_support(normalized_symbol, data_source)
|
||||
if start_date > end_date:
|
||||
raise ValueError("Start date must be on or before end date")
|
||||
normalized_inputs = normalize_historical_scenario_inputs(
|
||||
|
||||
Reference in New Issue
Block a user