def get_cex_status(): cex_pos = "/opt/dyeverse/crypto_agency/cex_bot/data/positions.json" if not os.path.exists(cex_pos): return {"active": False} try: with open(cex_pos) as f: d = json.load(f) pos = [{"exchange": p["exchange"], "symbol": p["symbol"], "side": p["side"], "qty": round(p["qty"], 8), "qty_usd": round(p.get("qty_usd", 0), 2), "avg_entry": round(p["avg_entry"], 4), "realized_pnl": round(p["realized_pnl"], 4), "fills": p["fills"]} for p in d.get("positions", [])] tot = d.get("total_realized_pnl", 0) ks, pnl, intents, execs = False, tot, 0, 0 try: import subprocess r = subprocess.run(["pm2", "logs", "sovereign-cex", "--lines", "50", "--nostream", "--no-color"], capture_output=True, text=True, timeout=2) if "KILL_SWITCH_BLOCK" in r.stdout: ks = True for ln in r.stdout.split("\\n"): if "session_pnl=" in ln: try: pnl = float(ln.split("session_pnl=")[1].split()[0].replace("$", "")) except: pass if "intents=" in ln: try: intents = int(ln.split("intents=")[1].split()[0]) except: pass if "executed=" in ln: try: execs = int(ln.split("executed=")[1].split()[0]) except: pass except: pass return {"active": True, "mode": "paper", "positions": pos, "total_realized_pnl": round(tot, 4), "session_pnl": round(pnl, 4), "kill_switch": ks, "position_count": len(pos), "total_intents": intents, "total_executed": execs} except Exception as e: return {"active": False, "error": str(e)}