refactor(pre-alpha): fail closed on historical fixture bounds

This commit is contained in:
Bu5hm4nn
2026-03-26 12:04:42 +01:00
parent f38d0a53a9
commit 68275c4d18
5 changed files with 82 additions and 17 deletions

View File

@@ -121,7 +121,9 @@ class EventComparisonService:
provider_ref: ProviderRef | None = None,
history: list[DailyClosePoint] | None = None,
) -> BacktestScenario:
selected_template_slugs = tuple(template_slugs or preset.scenario_overrides.default_template_slugs)
selected_template_slugs = (
tuple(preset.scenario_overrides.default_template_slugs) if template_slugs is None else tuple(template_slugs)
)
if not selected_template_slugs:
raise ValueError("Event comparison requires at least one template slug")

View File

@@ -10,11 +10,7 @@ from app.models.backtest import (
ProviderRef,
TemplateRef,
)
from app.services.backtesting.historical_provider import (
DailyClosePoint,
HistoricalPriceSource,
YFinanceHistoricalPriceSource,
)
from app.services.backtesting.historical_provider import DailyClosePoint
from app.services.backtesting.service import BacktestService
from app.services.strategy_templates import StrategyTemplateService
@@ -40,19 +36,18 @@ DETERMINISTIC_UI_FIXTURE_HISTORY: tuple[DailyClosePoint, ...] = (
)
class FixtureFallbackHistoricalPriceSource:
def __init__(self, fallback: HistoricalPriceSource | None = None) -> None:
self.fallback = fallback or YFinanceHistoricalPriceSource()
class DeterministicBacktestFixtureSource:
def load_daily_closes(self, symbol: str, start_date: date, end_date: date) -> list[DailyClosePoint]:
normalized_symbol = symbol.strip().upper()
if (
normalized_symbol == SUPPORTED_BACKTEST_PAGE_SYMBOL
and start_date == date(2024, 1, 2)
and end_date == date(2024, 1, 8)
normalized_symbol != SUPPORTED_BACKTEST_PAGE_SYMBOL
or start_date != date(2024, 1, 2)
or end_date != date(2024, 1, 8)
):
return list(DETERMINISTIC_UI_FIXTURE_HISTORY)
return self.fallback.load_daily_closes(normalized_symbol, start_date, end_date)
raise ValueError(
"BT-001A deterministic fixture data only supports GLD on the seeded 2024-01-02 through 2024-01-08 window"
)
return list(DETERMINISTIC_UI_FIXTURE_HISTORY)
@dataclass(frozen=True)
@@ -75,7 +70,7 @@ class BacktestPageService:
)
if backtest_service is None:
provider = self.backtest_service.provider
provider.source = FixtureFallbackHistoricalPriceSource(provider.source)
provider.source = DeterministicBacktestFixtureSource()
def template_options(self, symbol: str = "GLD") -> list[dict[str, str | int]]:
return [