refactor(pre-alpha): fail closed on historical preview fallbacks

This commit is contained in:
Bu5hm4nn
2026-03-26 11:55:45 +01:00
parent 4eec0127da
commit f38d0a53a9
6 changed files with 85 additions and 47 deletions

View File

@@ -98,7 +98,7 @@ class BacktestPageService:
)
return history[0].close
def run_read_only_scenario(
def validate_preview_inputs(
self,
*,
symbol: str,
@@ -108,7 +108,8 @@ class BacktestPageService:
underlying_units: float,
loan_amount: float,
margin_call_ltv: float,
) -> BacktestPageRunResult:
entry_spot: float | None = None,
) -> float:
normalized_symbol = symbol.strip().upper()
if not normalized_symbol:
raise ValueError("Symbol is required")
@@ -125,9 +126,35 @@ class BacktestPageService:
if not template_slug:
raise ValueError("Template selection is required")
self.template_service.get_template(template_slug)
resolved_entry_spot = (
entry_spot if entry_spot is not None else self.derive_entry_spot(normalized_symbol, start_date, end_date)
)
_validate_initial_collateral(underlying_units, resolved_entry_spot, loan_amount)
return resolved_entry_spot
def run_read_only_scenario(
self,
*,
symbol: str,
start_date: date,
end_date: date,
template_slug: str,
underlying_units: float,
loan_amount: float,
margin_call_ltv: float,
) -> BacktestPageRunResult:
normalized_symbol = symbol.strip().upper()
entry_spot = self.validate_preview_inputs(
symbol=normalized_symbol,
start_date=start_date,
end_date=end_date,
template_slug=template_slug,
underlying_units=underlying_units,
loan_amount=loan_amount,
margin_call_ltv=margin_call_ltv,
)
template = self.template_service.get_template(template_slug)
entry_spot = self.derive_entry_spot(normalized_symbol, start_date, end_date)
_validate_initial_collateral(underlying_units, entry_spot, loan_amount)
initial_portfolio = materialize_backtest_portfolio_state(
symbol=normalized_symbol,
underlying_units=underlying_units,

View File

@@ -114,6 +114,8 @@ class EventComparisonPageService:
loan_amount: float,
margin_call_ltv: float,
) -> BacktestScenario:
if not template_slugs:
raise ValueError("Select at least one strategy template.")
try:
scenario = self.comparison_service.preview_scenario_from_inputs(
preset_slug=preset_slug,
@@ -147,6 +149,8 @@ class EventComparisonPageService:
) -> EventComparisonReport:
if not preset_slug:
raise ValueError("Preset selection is required")
if not template_slugs:
raise ValueError("Select at least one strategy template.")
if underlying_units <= 0:
raise ValueError("Underlying units must be positive")
if loan_amount < 0:
@@ -162,7 +166,7 @@ class EventComparisonPageService:
try:
preview = self.comparison_service.preview_scenario_from_inputs(
preset_slug=preset.slug,
template_slugs=tuple(template_slugs or preset.scenario_overrides.default_template_slugs),
template_slugs=template_slugs,
underlying_units=underlying_units,
loan_amount=loan_amount,
margin_call_ltv=margin_call_ltv,
@@ -180,7 +184,7 @@ class EventComparisonPageService:
_validate_initial_collateral(underlying_units, preview.initial_portfolio.entry_spot, loan_amount)
return self.comparison_service.compare_event_from_inputs(
preset_slug=preset.slug,
template_slugs=tuple(template_slugs or preset.scenario_overrides.default_template_slugs),
template_slugs=template_slugs,
underlying_units=underlying_units,
loan_amount=loan_amount,
margin_call_ltv=margin_call_ltv,