Files

121 lines
4.1 KiB
Python

#!/usr/bin/env python3
import json
from collections import defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parent
CONFIG = ROOT / "config.json"
TRADES = ROOT / "data" / "trades.jsonl"
MIN_CLOSED = 3
MIN_PNL = 0.25
MIN_WIN_RATE = 0.66
PRUNE_CLOSED = 2
PRUNE_PNL = -0.20
def load_events():
if not TRADES.exists():
return []
out = []
for line in TRADES.read_text().splitlines():
if not line.strip():
continue
try:
out.append(json.loads(line))
except Exception:
pass
return out
def shadow_stats(events):
"""Return fee-adjusted shadow performance by symbol.
Older sell events only included sell fees in realized_pnl_usd. Newer sell
events include buy_fee_usd. For old closed round trips, subtract the matching
buy fees once so watch promotion/pruning is based on real net edge.
"""
stats = defaultdict(lambda: {"closed": 0, "wins": 0, "losses": 0, "pnl": 0.0})
pending_buy_fees = defaultdict(list)
for ev in events:
sym = ev.get("symbol")
if not sym:
continue
if ev.get("type") == "shadow_buy":
pending_buy_fees[sym].append(float(ev.get("fee_usd", 0.0)))
continue
if ev.get("type") != "shadow_sell":
continue
buy_fee = float(ev.get("buy_fee_usd", 0.0))
if "buy_fee_usd" not in ev and pending_buy_fees[sym]:
buy_fee = pending_buy_fees[sym].pop(0)
pnl = float(ev.get("realized_pnl_usd", 0.0)) - buy_fee
d = stats[sym]
d["closed"] += 1
d["pnl"] += pnl
if pnl >= 0:
d["wins"] += 1
else:
d["losses"] += 1
return stats
def main():
cfg = json.loads(CONFIG.read_text())
watch = list(dict.fromkeys(cfg.get("watch_only_symbols", [])))
trade = list(dict.fromkeys(cfg.get("symbols", [])))
stats = shadow_stats(load_events())
promotable = set(cfg.get("promotable_symbols") or (trade + watch))
promoted = []
rejected = []
pruned = []
ignored = []
for sym in list(watch):
d = stats[sym]
win_rate = (d["wins"] / d["closed"]) if d["closed"] else 0.0
if d["closed"] >= MIN_CLOSED and d["pnl"] >= MIN_PNL and win_rate >= MIN_WIN_RATE:
if sym in promotable:
watch.remove(sym)
if sym not in trade:
trade.append(sym)
promoted.append((sym, d))
else:
ignored.append((sym, d))
elif d["closed"] >= PRUNE_CLOSED and d["pnl"] <= PRUNE_PNL:
watch.remove(sym)
pruned.append((sym, d))
elif d["closed"] >= MIN_CLOSED and d["pnl"] < 0:
rejected.append((sym, d))
if promoted or pruned:
cfg["symbols"] = trade
cfg["watch_only_symbols"] = watch
CONFIG.write_text(json.dumps(cfg, indent=2) + "\n")
print("Promotion review")
print(f"Active tradeable: {len(trade)} | watch-only: {len(watch)}")
if promoted:
print("Promoted to tradeable:")
for sym, d in promoted:
print(f"- {sym}: net shadow pnl=${d['pnl']:.2f}, wins={d['wins']}, losses={d['losses']}, closed={d['closed']}")
else:
print("No watch-only coin has enough winning shadow evidence yet.")
if ignored:
print("Ignored winners outside promotable allow-list:")
for sym, d in ignored[:10]:
print(f"- {sym}: net shadow pnl=${d['pnl']:.2f}, wins={d['wins']}, losses={d['losses']}, closed={d['closed']}")
if rejected:
print("Worst rejected watch-only losers:")
for sym, d in sorted(rejected, key=lambda item: item[1]["pnl"])[:10]:
print(f"- {sym}: net shadow pnl=${d['pnl']:.2f}, wins={d['wins']}, losses={d['losses']}, closed={d['closed']}")
if pruned:
print("Pruned from watch-only for losing shadow evidence:")
for sym, d in pruned[:10]:
print(f"- {sym}: net shadow pnl=${d['pnl']:.2f}, wins={d['wins']}, losses={d['losses']}, closed={d['closed']}")
if watch:
print("Watch-only sample: " + ", ".join(watch[:20]) + (" ..." if len(watch) > 20 else ""))
if __name__ == "__main__":
main()