feat: instrument model

This commit is contained in:
OpenSquared
2026-07-03 11:00:23 +02:00
parent 5381b3fb92
commit 8f51e0de7b
3 changed files with 80 additions and 18 deletions

View File

@@ -37,6 +37,7 @@ class VirtualEvent(BaseModel):
class WhatIfBody(BaseModel):
period: str = "1y"
virtual_events: List[VirtualEvent] = []
start_date: Optional[str] = None
class CalibrateBody(BaseModel):
@@ -192,10 +193,26 @@ def get_price_history(
conn.close()
def _ph_period(start_date: Optional[str], fallback_period: str) -> str:
"""Compute the price-history period string needed to cover start_date → today."""
if not start_date:
return fallback_period
try:
from datetime import datetime as _dt
days = (_dt.utcnow().date() - _dt.fromisoformat(start_date[:10]).date()).days + 10
for p, d in [("5d", 7), ("1mo", 35), ("3mo", 95), ("6mo", 190), ("1y", 370), ("2y", 740)]:
if days <= d:
return p
return "2y"
except Exception:
return fallback_period
@router.get("/{instrument}/timeline")
def get_instrument_timeline(
instrument: str,
period: str = Query("1y", description="5d|1mo|3mo|6mo|1y|2y"),
period: str = Query("1y", description="5d|1mo|3mo|6mo|1y|2y"),
start_date: Optional[str] = Query(None, description="Date de début ISO (override period)"),
) -> List[Dict[str, Any]]:
"""Simulation jour par jour de tous les nœuds du modèle sur la période."""
from services.database import get_conn
@@ -203,9 +220,8 @@ def get_instrument_timeline(
from services.price_history import get_price_history
conn = get_conn()
try:
# Pré-peupler le cache prix pour que l'auto-anchor ait les données disponibles
get_price_history(conn, instrument.upper(), period)
data = simulate_timeline(conn, instrument.upper(), period)
get_price_history(conn, instrument.upper(), _ph_period(start_date, period))
data = simulate_timeline(conn, instrument.upper(), period, start_date=start_date)
if not data:
raise HTTPException(status_code=404, detail=f"Modèle introuvable pour {instrument.upper()}")
return data
@@ -224,10 +240,12 @@ def timeline_whatif(
from services.price_history import get_price_history
conn = get_conn()
try:
# Garantir que le cache prix est disponible pour l'auto-anchor
get_price_history(conn, instrument.upper(), body.period)
get_price_history(conn, instrument.upper(), _ph_period(body.start_date, body.period))
ve_list = [ve.dict() for ve in body.virtual_events]
data = simulate_timeline(conn, instrument.upper(), body.period, virtual_events=ve_list)
data = simulate_timeline(
conn, instrument.upper(), body.period,
virtual_events=ve_list, start_date=body.start_date
)
if not data:
raise HTTPException(status_code=404, detail=f"Modèle introuvable pour {instrument.upper()}")
return data