031b71bf54
T1 (TP di SKH01 come limit resting on-book) NON e' stato implementato, per misura.
Leggendo il codice di produzione: TP01 e SKH01 tradano lo stesso strumento con una
sola posizione netta Deribit, quindi un ordine on-book al livello di SKH chiuderebbe
anche quota TP01. Misurati i due ostacoli: (A) segno compatibile nel 97% dei trade
che escono in TP = risolvibile; (B) divergenza modello/live apparentemente bloccante.
Ma (B) non esiste: resample_5m NON scarta il bin 230m in corso (21 barre 5m su 46
nell'ultimo bin) e _skyhook_positions ci itera dentro -> il live rileva gia' SL/TP
intra-barra, entro ~1h dal tocco. I docstring che dicevano "usa solo barre chiuse" e
"latenza fino alla chiusura della barra 230m" erano FALSI e hanno guidato tre analisi
(02/07, 24/07, T1). Corretti sul posto.
Conseguenza: la lente `hourly` sottostima il path live di +0.081 Sharpe FULL di book
(23/23 offset, banda appaiata); il live vero sta sopra il canonical sul FULL, e il fix
richiesto sarebbe un declassamento (+0.054 vs +0.081) in cambio di ordini parziali sul
netto in un percorso con soldi veri.
Cablato invece il problema vero trovato per strada: fresh_5m fallisce in SILENZIO
(fallback al certificato, rigenerato 1x/giorno) -> la latenza d'uscita di SKH01 passa
da ~1h a ~1 giorno senza segnalazione, e nessun controllo esistente scatta (il gate di
staleness guarda il feed di TP01). Stesso schema del feed-freeze del 14/07.
* livefeed.feed_age_minutes: pura, eta' dalla CHIUSURA della barra, None = non
misurata, clamp sugli skew d'orologio
* book_report espone skh_feed_age_min (max fra gli asset)
* book_execute stampa lo stato e allerta su Telegram sopra skh_feed_max_age_min (30m)
Scelta dichiarata: ALLERTA, NON blocca. Bloccare fermerebbe anche TP01 (nettato sullo
stesso strumento) per un guasto di rete; forzare SKH flat chiuderebbe posizioni buone
su un glitch.
Follow-up dichiarato: la stessa barra parziale tocca gli INGRESSI (ent[n-1] da breakout
non confermato). Disaccordo in 1 bin su 112, ma con 3 entry nel campione la taglia non
e' stimabile. E' un cambio di strategia, non di strumentazione -> misura dedicata prima.
Strategia, pesi e cadenza del cron INVARIATI. Nessun ordine inviato. Suite 266 verdi.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
222 lines
11 KiB
Python
222 lines
11 KiB
Python
"""BOOK DERIBIT-ONLY LIVE EXECUTE — TP01 + SKH01 NETTATI in software su un solo conto Deribit mainnet.
|
|
|
|
Porta il conto reale al target NETTO per asset (vedi src/live/book.py): per ogni asset combina la
|
|
frazione long-flat di TP01 (peso 0.75) e il segno L/S di SKH01 (peso 0.25), e manda UN ordine con
|
|
segno (long/short/flip) per raggiungerlo. Poi assicura un disaster-SL on-book sulla posizione NETTA.
|
|
|
|
DOPPIO GATE DI SICUREZZA (entrambi necessari per inviare ordini reali):
|
|
1. config/live.json -> "execution_enabled": true (master switch, default false)
|
|
2. flag CLI --execute
|
|
Senza entrambi e' un DRY-RUN (stampa il piano, NON invia). Reconciliation dopo ogni ordine; log in
|
|
data/live/book_executions.jsonl.
|
|
|
|
⚠️ CADENZA: SKH01 decide su griglia 230m -> questo script va lanciato ogni ~230 minuti con la feed
|
|
fresca all'ultima barra chiusa (NON il cron giornaliero, che mancherebbe gli ingressi). Gli exit di
|
|
SKH sono SOFTWARE (latenza fino a fine barra 230m); solo il disaster-SL (-30%) e' on-book.
|
|
|
|
uv run python scripts/live/book_execute.py # DRY-RUN (piano, nessun ordine)
|
|
uv run python scripts/live/book_execute.py --execute # esegue SOLO se execution_enabled=true
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from src.live.book import book_report
|
|
from src.live.execution import DeribitTrader
|
|
from src.live.notifier import notify
|
|
|
|
CONFIG = PROJECT_ROOT / "config" / "live.json"
|
|
LOG_DIR = PROJECT_ROOT / "data" / "live"
|
|
LOG = LOG_DIR / "book_executions.jsonl"
|
|
|
|
|
|
def load_config() -> dict:
|
|
cfg = json.loads(CONFIG.read_text()) if CONFIG.exists() else {}
|
|
cfg.setdefault("execution_enabled", False)
|
|
cfg.setdefault("max_notional_per_asset_usd", 300.0)
|
|
cfg.setdefault("min_order_usd", 5.0)
|
|
cfg.setdefault("disaster_sl_pct", 0.30)
|
|
cfg.setdefault("max_data_age_days", 2.0)
|
|
cfg.setdefault("skh_feed_max_age_min", 30.0)
|
|
return cfg
|
|
|
|
|
|
def _data_age_days(last_data) -> float | None:
|
|
"""Eta' in giorni dell'ultima barra del feed certificato. None se non interpretabile
|
|
(trattata come stantia: meglio non operare che operare su una data che non so leggere)."""
|
|
if last_data in (None, ""):
|
|
return None
|
|
try:
|
|
ts = pd.Timestamp(last_data)
|
|
ts = ts.tz_localize("UTC") if ts.tz is None else ts.tz_convert("UTC")
|
|
return float((pd.Timestamp.now(tz="UTC") - ts).total_seconds() / 86400.0)
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def log_event(rec: dict):
|
|
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
|
with open(LOG, "a") as f:
|
|
f.write(json.dumps(rec) + "\n")
|
|
|
|
|
|
def _run():
|
|
cfg = load_config()
|
|
want_execute = "--execute" in sys.argv[1:]
|
|
enabled = bool(cfg["execution_enabled"])
|
|
do_execute = want_execute and enabled
|
|
min_order = float(cfg["min_order_usd"])
|
|
sl_pct = float(cfg["disaster_sl_pct"])
|
|
|
|
r = book_report(live_feed=True) # target NETTO + conto/posizioni reali (feed SKH fresco)
|
|
equity = r["equity"]
|
|
|
|
print("=" * 88)
|
|
print(" BOOK DERIBIT LIVE EXECUTE — TP01(0.75)+SKH01(0.25) NETTATI — Deribit mainnet (USDC linear)")
|
|
print("=" * 88)
|
|
mode = ("ESECUZIONE REALE" if do_execute else
|
|
("ARMATO ma manca --execute" if enabled else "DRY-RUN (execution_enabled=false)"))
|
|
print(f" modo : {mode}")
|
|
print(f" gate : execution_enabled={enabled} | --execute={want_execute}")
|
|
print(f" conto reale : ${r['real_equity']:,.2f}" if r["real_equity"] else f" conto: {r['eq_basis']}")
|
|
print(f" sizing base : ${equity:,.2f} | cap/asset ${r['cap_per_asset']:.0f} | min ${min_order:.0f} | disaster-SL -{sl_pct*100:.0f}%")
|
|
print(f" ultima barra : {r['last_data']}\n")
|
|
|
|
if r.get("skh_error"): # SKH feed fallito -> book.py ha forzato flat IN SILENZIO
|
|
print(f" ⚠️ SKH FEED ERRORE (SKH forzato flat!): {r['skh_error']}")
|
|
notify("⚠️ BOOK — SKH feed fallito (sleeve forzato flat)", {"error": r["skh_error"]})
|
|
|
|
# --- FRESCHEZZA del feed 5m usato per il segnale SKH (2026-07-26) --------------------------
|
|
# `fresh_5m` ricade sul feed certificato IN SILENZIO se il fetch pubblico Deribit fallisce, e
|
|
# il certificato lo ricostruisce il cron una volta al giorno. Non solleva, non logga: senza
|
|
# questo controllo la latenza d'uscita di SKH01 passa da ~1h a ~1 GIORNO senza che nulla lo
|
|
# dica. E' proprio la latenza d'uscita dove vive la qualita' del path live (misura 2026-07-26).
|
|
# SCELTA DICHIARATA: allerta, NON blocca. Bloccare fermerebbe anche il ribilancio di TP01
|
|
# (sono nettati sullo stesso strumento) per un guasto di rete; e forzare SKH flat chiuderebbe
|
|
# posizioni buone su un glitch. La decisione resta all'operatore.
|
|
skh_age = r.get("skh_feed_age_min")
|
|
max_skh_age = float(cfg.get("skh_feed_max_age_min", 30.0))
|
|
if skh_age is None:
|
|
print(" ⚠️ freschezza feed SKH NON MISURATA (segnale su feed certificato?)")
|
|
elif skh_age > max_skh_age:
|
|
print(f" ⚠️ FEED SKH STANTIO: ultima barra 5m di {skh_age:.0f} min fa "
|
|
f"(soglia {max_skh_age:.0f}) -> le uscite SKH sono in ritardo, NON blocco.")
|
|
if do_execute:
|
|
notify("⚠️ BOOK — feed SKH stantio (uscite in ritardo)",
|
|
{"eta_min": round(skh_age), "soglia_min": round(max_skh_age),
|
|
"effetto": "SL/TP di SKH01 rilevati in ritardo",
|
|
"nota": "fresh_5m e' ricaduto sul feed certificato (fetch pubblico KO)"})
|
|
else:
|
|
print(f" feed SKH : fresco ({skh_age:.0f} min)")
|
|
|
|
if not r["online"]:
|
|
print(" conto non leggibile (offline) -> stop, non eseguo a cieco.")
|
|
if do_execute:
|
|
notify("⚠️ BOOK LIVE — conto offline", {"nota": "salto l'esecuzione, non opero a cieco"})
|
|
return
|
|
|
|
if r.get("pos_error"): # ONLINE ma posizione IGNOTA (read fallita -> assunta flat)
|
|
print(f" 🛑 POSIZIONE NON LEGGIBILE -> NON eseguo a cieco: {r['pos_error']}")
|
|
if do_execute:
|
|
notify("🛑 BOOK LIVE — posizione non leggibile", {"error": r["pos_error"],
|
|
"nota": "salto l'esecuzione, non opero a cieco"})
|
|
return
|
|
|
|
stale_days = _data_age_days(r.get("last_data"))
|
|
# .get col default: un chiamante che passa una config senza la chiave deve ricadere sulla
|
|
# soglia sicura, non sollevare KeyError dentro il percorso d'esecuzione con soldi veri.
|
|
max_age = float(cfg.get("max_data_age_days", 2.0))
|
|
if stale_days is None or stale_days > max_age:
|
|
# FEED STANTIO -> non eseguo. Il 2026-07-14 il book ha comprato ETH con l'ultima barra
|
|
# ferma al 07-08 (feed congelato 6 giorni, diario 2026-07-15-feed-freeze): il conto era
|
|
# online e la posizione leggibile, quindi i due gate esistenti NON scattavano. Il segnale
|
|
# TP01 viene dal feed su disco: se e' vecchio, si opera alla cieca su dati morti.
|
|
# Il disaster-SL on-book resta la rete di sicurezza su eventuali posizioni aperte.
|
|
eta = "ignota" if stale_days is None else f"{stale_days:.0f}g"
|
|
print(f" 🛑 FEED STANTIO (ultima barra {r.get('last_data')}, eta' {eta} > {max_age:.0f}g)"
|
|
" -> NON eseguo su dati morti.")
|
|
print(" Sbloccare con: uv run python scripts/analysis/rebuild_history.py --asset BTC ETH")
|
|
if do_execute:
|
|
notify("🛑 BOOK LIVE — FEED STANTIO, esecuzione saltata",
|
|
{"ultima_barra": str(r.get("last_data")), "eta": eta,
|
|
"soglia": f"{max_age:.0f}g",
|
|
"azione": "rebuild_history.py --asset BTC ETH"})
|
|
return
|
|
|
|
if r.get("eq_fallback"): # equity reale non leggibile -> sizing su paper_cap
|
|
print(f" ⚠️ EQUITY FALLBACK (sizing su paper_cap, NON blocco): {r['eq_fallback']}")
|
|
if do_execute: # solo diagnostica: l'hard-cap $/asset limita il downside
|
|
notify("⚠️ BOOK LIVE — equity fallback (sizing su paper_cap)", {"nota": r["eq_fallback"]})
|
|
|
|
trader = DeribitTrader() if do_execute else None
|
|
actions = []
|
|
for a in r["assets"]:
|
|
asset, inst = a["asset"], a["instrument"]
|
|
net, cur, mark = a["net_target"], a["position_usd"], a["mark"]
|
|
sk = a["skh_state"]
|
|
sk_txt = "flat" if sk == "flat" else f"{sk['dir']}@{sk.get('entry')}"
|
|
order = a["order"]
|
|
if order is None:
|
|
act = "HOLD (a target)"
|
|
elif order.get("is_close"):
|
|
act = f"CLOSE ${cur:,.0f}"
|
|
elif order.get("needs_flip"):
|
|
act = f"FLIP -> ${net:,.0f}"
|
|
else:
|
|
act = f"{order['side'].upper()} ${order['delta']:+,.0f}"
|
|
print(f" {asset:<3} TP {a['tp_frac']:+.3f} · SKH {a['skh_sign']:+d}({sk_txt}) -> net ${net:+,.0f} "
|
|
f"| pos ${cur:+,.0f} -> {act}")
|
|
|
|
if do_execute and order is not None:
|
|
fills = trader.rebalance_signed(inst, net, mark, min_usd=min_order)
|
|
newpos = trader.position_usd(inst)
|
|
for f in fills:
|
|
print(f" -> {f.side.upper()} {f.filled:.4f} @ ${f.price or 0:,.1f} fee {f.fee_usdc:.5f} "
|
|
f"({'OK' if f.verified else 'NON VERIFICATO: ' + f.notes})")
|
|
log_event(dict(ts_utc=str(pd.Timestamp(r['last_data'])), asset=asset, action=act,
|
|
side=f.side, filled=f.filled, price=f.price, fee=f.fee_usdc,
|
|
verified=f.verified, notes=f.notes, net_target=net, pos_after=newpos,
|
|
tp_frac=a["tp_frac"], skh_sign=a["skh_sign"]))
|
|
det = dict(asset=asset, side=f.side, amount=round(f.filled, 4), price=round(f.price or 0, 1),
|
|
fee=round(f.fee_usdc, 5), net=round(net, 0), pos_after=round(newpos, 0))
|
|
notify(f"✅ BOOK {act}" if f.verified else "⚠️ BOOK ORDINE NON VERIFICATO",
|
|
det if f.verified else {**det, "notes": f.notes})
|
|
print(f" reconcile: pos ${newpos:,.0f}")
|
|
if do_execute:
|
|
ds = trader.ensure_disaster_sl(inst, sl_pct) # bracket su posizione NETTA (adatta long/short)
|
|
print(f" disaster-SL: {ds.get('state')}" + (f" @ ${ds['stop']:,.1f}" if ds.get("stop") else ""))
|
|
if ds.get("state") == "placed":
|
|
notify("🛡️ BOOK disaster-SL piazzato", {"asset": asset, "stop": round(ds.get("stop") or 0, 1),
|
|
"amount": round(ds.get("amount") or 0, 4)})
|
|
elif ds.get("state") == "place-failed":
|
|
notify("⚠️ BOOK disaster-SL FALLITO", {"asset": asset, "notes": ds.get("notes")})
|
|
actions.append(act)
|
|
|
|
print()
|
|
if not do_execute:
|
|
print(" => DRY-RUN: nessun ordine inviato." +
|
|
("" if enabled else " Per armare: config/live.json execution_enabled=true + --execute."))
|
|
elif all(x.startswith("HOLD") for x in actions):
|
|
print(" => Nessuna azione: conto gia' al target netto del book.")
|
|
else:
|
|
print(" => Esecuzione completata (vedi data/live/book_executions.jsonl).")
|
|
|
|
|
|
def main():
|
|
try:
|
|
_run()
|
|
except Exception as e:
|
|
notify("🛑 BOOK LIVE — ERRORE", {"error": f"{type(e).__name__}: {e}"})
|
|
raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|