feat: saxo history

This commit is contained in:
OpenSquared
2026-07-19 08:49:57 +02:00
parent d4fb15ca9a
commit e7247d4c4c
5 changed files with 131 additions and 33 deletions

View File

@@ -6219,6 +6219,49 @@ def get_latest_saxo_snapshot_rows(symbol: str) -> List[Dict[str, Any]]:
return [dict(r) for r in rows]
def get_snapshot_rows_asof(symbol: Optional[str] = None, as_of: Optional[str] = None) -> List[Dict[str, Any]]:
"""One row per (symbol, expiry_date, strike, option_type) — the freshest capture at or
before `as_of` (an ISO datetime string), or simply the freshest capture overall when
as_of is omitted. Powers the Saxo History page's default 'current chain' view and its
'replay a past moment' filter — the full granular history stays in the table underneath
for later backtest use, this just picks one row per contract."""
conn = get_conn()
where_symbol = "AND symbol = ?" if symbol else ""
where_asof = "AND created_at <= ?" if as_of else ""
params: List[Any] = []
if symbol:
params.append(symbol)
if as_of:
params.append(as_of)
rows = conn.execute(f"""
SELECT s.* FROM saxo_option_snapshots s
JOIN (
SELECT symbol, expiry_date, strike, option_type, MAX(created_at) AS max_created
FROM saxo_option_snapshots
WHERE 1=1 {where_symbol} {where_asof}
GROUP BY symbol, expiry_date, strike, option_type
) latest
ON s.symbol = latest.symbol AND s.expiry_date = latest.expiry_date
AND s.strike = latest.strike AND s.option_type = latest.option_type
AND s.created_at = latest.max_created
ORDER BY s.symbol, s.expiry_date, s.strike
""", params).fetchall()
conn.close()
return [dict(r) for r in rows]
def delete_saxo_snapshots(symbol: str) -> int:
"""Wipes the full accumulated history for one symbol (all expiries/strikes/dates) —
the raw history table is otherwise never pruned, so this is the user-triggered escape
hatch for a ticker they no longer want tracked."""
conn = get_conn()
cur = conn.execute("DELETE FROM saxo_option_snapshots WHERE symbol = ?", (symbol,))
deleted = cur.rowcount
conn.commit()
conn.close()
return deleted
def upsert_saxo_catalog_rows(rows: List[Dict[str, Any]]):
if not rows:
return