6605008f49
Traccia lo stop-rate delle fade (MR01/MR02/MR07) PRIMA/DOPO l'attivazione del loss-guard (2026-06-02 14:34 UTC). Verdetto automatico quando il campione DOPO >= 30 trade. Conferma gia' visibile: stop-rate live PRIMA 42% (n=36) == backtest 42.1%. Gira host-side (cron), no rebuild. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
193 lines
6.9 KiB
Python
193 lines
6.9 KiB
Python
"""Report orario PORT06 -> Telegram.
|
|
|
|
Legge lo stato persistito del paper trader a portafoglio (data/portfolio_paper/*/ +
|
|
data/portfolios/PORT06/status.json) e invia su Telegram:
|
|
1) trade CHIUSI: positivi/negativi (netto fee) con breakdown per motivo e PnL;
|
|
2) trade IN CORSO (posizioni aperte);
|
|
3) PnL realizzato totale + equity mark-to-market.
|
|
|
|
Eseguibile standalone (es. da cron orario):
|
|
cd /opt/docker/PythagorasGoal && uv run python scripts/portfolios/hourly_report.py
|
|
|
|
Carica .env da solo (cron non eredita l'env del container). Legge file world-readable
|
|
scritti dal container; non tocca lo stato del trader.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import glob
|
|
from collections import defaultdict
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
PAPER = ROOT / "data" / "portfolio_paper"
|
|
PORT_STATUS = ROOT / "data" / "portfolios" / "PORT06" / "status.json"
|
|
|
|
|
|
def _load_env():
|
|
"""Carica TELEGRAM_* da .env nell'os.environ (cron non li ha)."""
|
|
import os
|
|
envf = ROOT / ".env"
|
|
if not envf.exists():
|
|
return
|
|
for line in envf.read_text().splitlines():
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
k, v = line.split("=", 1)
|
|
os.environ.setdefault(k.strip(), v.strip())
|
|
|
|
|
|
def _short(wid: str) -> str:
|
|
"""SH01_shape_ml__BTC__1h -> SH01/BTC ; PR01_..._ETH_SOL__1h -> PR01/ETH_SOL."""
|
|
parts = wid.split("__")
|
|
code = parts[0].split("_")[0]
|
|
tag = parts[1] if len(parts) > 1 else ""
|
|
return f"{code}/{tag}" if tag else code
|
|
|
|
|
|
# --- monitor loss-guard Hurst: stop-rate fade PRIMA/DOPO l'attivazione (hurst_max=0.55, v1.0.0) ---
|
|
LOSSGUARD_SINCE = "2026-06-02T14:34:30"
|
|
FADE_PREFIXES = ("MR01", "MR02", "MR07") # le 3 fade con hurst_max attivo
|
|
LOSSGUARD_MIN_SAMPLE = 30
|
|
|
|
|
|
def lossguard_section() -> str:
|
|
before = [0, 0] # [closes, stops] prima dell'attivazione
|
|
after = [0, 0] # dopo
|
|
for sp in glob.glob(str(PAPER / "*" / "status.json")):
|
|
wid = Path(sp).parent.name
|
|
if not wid.startswith(FADE_PREFIXES):
|
|
continue
|
|
tp = Path(sp).parent / "trades.jsonl"
|
|
if not tp.exists():
|
|
continue
|
|
for line in tp.read_text().splitlines():
|
|
if not line.strip():
|
|
continue
|
|
ev = json.loads(line)
|
|
if ev.get("event") != "CLOSE":
|
|
continue
|
|
b = after if ev.get("ts", "") >= LOSSGUARD_SINCE else before
|
|
b[0] += 1
|
|
if ev.get("reason") == "stop_loss":
|
|
b[1] += 1
|
|
|
|
def rate(b):
|
|
return b[1] / b[0] * 100 if b[0] else 0.0
|
|
|
|
L = [f"🛡️ <b>Loss-guard Hurst</b> (fade, dal {LOSSGUARD_SINCE[:16].replace('T', ' ')} UTC)"]
|
|
L.append(f" stop-rate PRIMA {rate(before):.0f}% (n={before[0]}) → DOPO {rate(after):.0f}% (n={after[0]})")
|
|
if after[0] >= LOSSGUARD_MIN_SAMPLE:
|
|
delta = rate(before) - rate(after)
|
|
L.append(f" VERDETTO (n≥{LOSSGUARD_MIN_SAMPLE}): {delta:+.0f}pp → "
|
|
f"{'✅ riduce gli stop' if delta > 0 else '⚠️ nessuna riduzione'}")
|
|
else:
|
|
L.append(f" campione DOPO {after[0]}/{LOSSGUARD_MIN_SAMPLE} → verdetto rimandato")
|
|
return "\n".join(L)
|
|
|
|
|
|
def collect():
|
|
closed = [] # (sleeve, reason, net_return, pnl, win)
|
|
open_pos = [] # dict per posizione aperta
|
|
realized = 0.0
|
|
for sp in sorted(glob.glob(str(PAPER / "*" / "status.json"))):
|
|
d = Path(sp).parent
|
|
wid = d.name
|
|
st = json.loads(Path(sp).read_text())
|
|
tp = d / "trades.jsonl"
|
|
if tp.exists():
|
|
for line in tp.read_text().splitlines():
|
|
if not line.strip():
|
|
continue
|
|
ev = json.loads(line)
|
|
if ev.get("event") != "CLOSE":
|
|
continue
|
|
nr = ev.get("net_return", 0.0)
|
|
pnl = ev.get("pnl", 0.0)
|
|
realized += pnl
|
|
closed.append((_short(wid), ev.get("reason", "?"), nr, pnl, nr > 0))
|
|
if st.get("in_position"):
|
|
open_pos.append({
|
|
"sleeve": _short(wid),
|
|
"dir": st.get("direction", 0),
|
|
"entry": st.get("entry_price") or st.get("entry_a"),
|
|
"entry_b": st.get("entry_b"),
|
|
"bars": st.get("bars_held", 0),
|
|
"cap": st.get("capital", 0.0),
|
|
})
|
|
return closed, open_pos, realized
|
|
|
|
|
|
def build_report() -> str:
|
|
closed, open_pos, realized = collect()
|
|
pos = sum(1 for c in closed if c[4])
|
|
neg = len(closed) - pos
|
|
|
|
# breakdown per motivo
|
|
by_reason = defaultdict(lambda: [0, 0, 0.0]) # reason -> [win, loss, pnl]
|
|
for _, reason, _, pnl, win in closed:
|
|
r = by_reason[reason]
|
|
r[0 if win else 1] += 1
|
|
r[2] += pnl
|
|
|
|
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
|
|
try:
|
|
ver = (ROOT / "VERSION").read_text().strip()
|
|
except Exception:
|
|
ver = "?"
|
|
eq = dd = cap = None
|
|
if PORT_STATUS.exists():
|
|
ps = json.loads(PORT_STATUS.read_text())
|
|
eq, cap, dd = ps.get("equity"), ps.get("total_capital"), ps.get("max_dd")
|
|
|
|
L = [f"📊 <b>PORT06 — Report orario</b> <code>v{ver}</code>", now]
|
|
if eq is not None:
|
|
L.append(f"Equity €{eq:.2f} | Cap €{cap:.2f} | maxDD {dd:.3f}%")
|
|
|
|
# 1) CHIUSI
|
|
L.append(f"\n✅ <b>CHIUSI</b>: {pos} positivi / {neg} negativi (netto fee)")
|
|
rows = [f"{'motivo':<12}{'✅':>3}{'❌':>4}{'PnL€':>9}"]
|
|
for reason, (w, l, pnl) in sorted(by_reason.items(), key=lambda x: x[1][2]):
|
|
rows.append(f"{reason:<12}{w:>3}{l:>4}{pnl:>+9.2f}")
|
|
L.append("<pre>" + "\n".join(rows) + "</pre>")
|
|
|
|
# 2) IN CORSO
|
|
L.append(f"🟢 <b>IN CORSO</b>: {len(open_pos)} posizioni")
|
|
if open_pos:
|
|
rows = [f"{'sleeve':<14}{'d':<2}{'barre':>6} {'entry'}"]
|
|
for p in sorted(open_pos, key=lambda x: x["sleeve"]):
|
|
d = "L" if p["dir"] == 1 else "S" if p["dir"] == -1 else "-"
|
|
entry = p["entry"]
|
|
es = f"{entry:.6g}" if isinstance(entry, (int, float)) else str(entry)
|
|
if p["entry_b"]:
|
|
es = f"{entry:.6g}/{p['entry_b']:.6g}" # coppia: 2 gambe
|
|
rows.append(f"{p['sleeve']:<14}{d:<2}{p['bars']:>6} {es}")
|
|
L.append("<pre>" + "\n".join(rows) + "</pre>")
|
|
|
|
# 2b) monitor loss-guard
|
|
L.append(lossguard_section())
|
|
|
|
# 3) TOTALE
|
|
L.append(f"💰 <b>PnL realizzato totale: €{realized:+.2f}</b>")
|
|
if eq is not None:
|
|
unreal = eq - cap
|
|
L.append(f" equity mark-to-market: €{eq:.2f} (non realizz. €{unreal:+.2f})")
|
|
return "\n".join(L)
|
|
|
|
|
|
def main():
|
|
_load_env()
|
|
import sys
|
|
sys.path.insert(0, str(ROOT))
|
|
from src.live.telegram_notifier import send_telegram
|
|
report = build_report()
|
|
print(report)
|
|
ok = send_telegram(report)
|
|
print("\n[telegram]", "inviato" if ok else "NON inviato (token/chat mancanti o errore rete)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|