c655604131
PairsExecutionClient (compone ExecutionClient): open_pair (2 market long A/short B, verifica per-gamba, LEG-RISK unwind se una sola filla), close_pair (2 reduce-only via close_amount, MAI close_position), register_contract (fetch spec USDC). Spec LTC/ADA/SOL aggiunti. PairsWorker: ledger reale shadow a 2 gambe resume-safe (_real_open_pair/ _real_close_pair, PnL per gamba dir A=+d/B=-d, doppio arrotondamento riportato). Runner: pairs_executor gated su execution.pairs_enabled (false di default). Validazione: test 92/92 (open/close, leg-risk unwind, resume) + smoke testnet end-to-end (open 2 gambe verificate, close reduce-only, PnL reale -0.039 vs sim -0.036, conto flat). Smoke ora aborta se ci sono posizioni di produzione + usa solo close_amount. NB incidente testnet documentato (diario): pulizia manuale con close_position ha flattato le quote shadow dei fade sul conto condiviso -> auto-riconciliazione al prossimo close. Lezione: mai close_position su strumenti condivisi. pairs_enabled resta FALSE: accendere con finestra a conto flat + osservazione. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
"""Notifiche Telegram per il paper trader."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import urllib.request
|
|
import urllib.parse
|
|
import json
|
|
|
|
from src.version import APP_VERSION
|
|
|
|
BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
|
|
CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID", "")
|
|
|
|
NOTIFY_EVENTS = {
|
|
"SIGNAL", "OPENED", "CLOSED", "OPEN_FAILED", "CLOSE_FAILED",
|
|
"ERROR", "STARTUP", "SHUTDOWN", "TRAINING_FAILED",
|
|
# esecuzione REALE (shadow su Deribit testnet)
|
|
"REAL_EXEC_LIVE", # primo ordine reale verificato di un worker (conferma "e' vivo")
|
|
"REAL_OPEN_FAIL", # un'apertura reale NON si e' verificata (problema da guardare)
|
|
"STALE_FEED", # feed flat/fermo da >= N barre 1h (worker ciechi: il prossimo
|
|
# prezzo reale puo' gappare, come ETH 2026-06-05 1655->1600)
|
|
"PANEL_SHORT", # TSM01/ROT02: panel inner-join troncato sotto il lookback
|
|
# richiesto -> tick() salterebbe in SILENZIO (worker inerte)
|
|
"FEED_OUTAGE", # N poll consecutivi falliti/degradati nel runner: exit non
|
|
# valutati, posizioni reali protette solo dal disaster-SL
|
|
"REAL_DIVERGENCE", # |slippage| sim/reale anomalo a open/close (es. spike print
|
|
# testnet: sim entra su un prezzo fantasma, il reale sul book)
|
|
"REAL_DSL_CANCEL_FAIL", # cancel del disaster-SL fallita dopo retry: possibile
|
|
# stop ORFANO sul book -> verificare a mano
|
|
"REAL_CLOSE_FAILED", # chiusura reale a 2 gambe (pairs) non verificata su una gamba
|
|
}
|
|
|
|
|
|
def send_telegram(text: str) -> bool:
|
|
if not BOT_TOKEN or not CHAT_ID:
|
|
return False
|
|
try:
|
|
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
|
|
data = urllib.parse.urlencode({"chat_id": CHAT_ID, "text": text, "parse_mode": "HTML"}).encode()
|
|
urllib.request.urlopen(url, data, timeout=10)
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def notify_event(event: str, data: dict | None = None):
|
|
if event not in NOTIFY_EVENTS:
|
|
return
|
|
lines = [f"📊 <b>{event}</b> <code>v{APP_VERSION}</code>"]
|
|
if data:
|
|
for k, v in data.items():
|
|
if k in ("signal",):
|
|
continue
|
|
lines.append(f" {k}: {v}")
|
|
send_telegram("\n".join(lines))
|