feat: backtest

This commit is contained in:
OpenSquared
2026-07-30 10:53:47 +02:00
parent 9a2ffb1c6a
commit 15528e0c98
4 changed files with 216 additions and 58 deletions

View File

@@ -145,3 +145,27 @@ def build_legs(
return []
return _first_by_name(list(tmpl.diagonal_spread(near_expiry, far_expiry, spot)), "Diagonal Spread") or []
return []
_NOMINAL_SPOT = 100.0
_NOMINAL_NEAR_DAYS = 90
_NOMINAL_FAR_DAYS = 180
def default_legs_pct(strategy_key: str, strike_offset_pct: float = 0.05) -> List[Dict[str, Any]]:
"""A preset's legs expressed relative to spot (strike_pct = strike/spot, e.g. 1.05 =
5% OTM call) instead of the absolute strikes build_legs() returns — this is what
seeds the frontend's editable leg editor when a preset is picked. Computed once at a
nominal spot=100, not per simulated date (routers/backtest.py's /run instead takes
the user-edited legs directly and reapplies strike_pct * spot at each entry date)."""
near = synthetic_expiry("near", _NOMINAL_NEAR_DAYS, _NOMINAL_SPOT)
far = synthetic_expiry("far", _NOMINAL_FAR_DAYS, _NOMINAL_SPOT)
legs = build_legs(strategy_key, _NOMINAL_SPOT, strike_offset_pct, near, far)
return [
{
"option_type": leg["option_type"], "position": leg["position"], "quantity": leg["quantity"],
"strike_pct": round(leg["strike"] / _NOMINAL_SPOT, 4),
"expiry": "near" if leg["days_to_expiry"] == _NOMINAL_NEAR_DAYS else "far",
}
for leg in legs
]