refactor(BT-001C): share historical fixture provider
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
|
||||
from app.models.backtest import BacktestScenario, EventComparisonRanking, EventComparisonReport
|
||||
from app.services.backtesting.comparison import EventComparisonService
|
||||
from app.services.backtesting.historical_provider import DailyClosePoint, SyntheticHistoricalProvider
|
||||
from app.services.backtesting.fixture_source import bind_fixture_source, build_event_comparison_fixture_source
|
||||
from app.services.backtesting.historical_provider import SyntheticHistoricalProvider
|
||||
from app.services.backtesting.input_normalization import normalize_historical_scenario_inputs
|
||||
from app.services.event_presets import EventPresetService
|
||||
from app.services.strategy_templates import StrategyTemplateService
|
||||
@@ -23,32 +23,7 @@ def _validate_initial_collateral(underlying_units: float, entry_spot: float, loa
|
||||
)
|
||||
|
||||
|
||||
DETERMINISTIC_EVENT_COMPARISON_FIXTURE_HISTORY: tuple[DailyClosePoint, ...] = (
|
||||
DailyClosePoint(date=date(2024, 1, 2), close=100.0),
|
||||
DailyClosePoint(date=date(2024, 1, 3), close=96.0),
|
||||
DailyClosePoint(date=date(2024, 1, 4), close=92.0),
|
||||
DailyClosePoint(date=date(2024, 1, 5), close=88.0),
|
||||
DailyClosePoint(date=date(2024, 1, 8), close=85.0),
|
||||
)
|
||||
|
||||
FIXTURE_HISTORY_START = DETERMINISTIC_EVENT_COMPARISON_FIXTURE_HISTORY[0].date
|
||||
FIXTURE_HISTORY_END = DETERMINISTIC_EVENT_COMPARISON_FIXTURE_HISTORY[-1].date
|
||||
|
||||
|
||||
class EventComparisonFixtureHistoricalPriceSource:
|
||||
def load_daily_closes(self, symbol: str, start_date: date, end_date: date) -> list[DailyClosePoint]:
|
||||
normalized_symbol = symbol.strip().upper()
|
||||
if normalized_symbol != SUPPORTED_EVENT_COMPARISON_SYMBOL:
|
||||
raise ValueError(
|
||||
"BT-003A deterministic fixture data only supports GLD event-comparison presets on this page"
|
||||
)
|
||||
if start_date < FIXTURE_HISTORY_START or end_date > FIXTURE_HISTORY_END:
|
||||
raise ValueError(
|
||||
"BT-003A deterministic fixture data only supports the seeded 2024-01-02 through 2024-01-08 window"
|
||||
)
|
||||
return [
|
||||
point for point in DETERMINISTIC_EVENT_COMPARISON_FIXTURE_HISTORY if start_date <= point.date <= end_date
|
||||
]
|
||||
EventComparisonFixtureHistoricalPriceSource = build_event_comparison_fixture_source
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -102,7 +77,10 @@ class EventComparisonPageService:
|
||||
self.event_preset_service = event_preset_service or EventPresetService()
|
||||
self.template_service = template_service or StrategyTemplateService()
|
||||
if comparison_service is None:
|
||||
provider = SyntheticHistoricalProvider(source=EventComparisonFixtureHistoricalPriceSource())
|
||||
provider = bind_fixture_source(
|
||||
SyntheticHistoricalProvider(),
|
||||
build_event_comparison_fixture_source(),
|
||||
)
|
||||
comparison_service = EventComparisonService(
|
||||
provider=provider,
|
||||
event_preset_service=self.event_preset_service,
|
||||
|
||||
Reference in New Issue
Block a user