File size: 3,673 Bytes
d4c51f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""Step 2 — fetch history for each candidate and compute metrics + patterns.

    python scripts/02_analyze_wallets.py

Reads  data/candidates.json
Writes data/wallets.csv          (one row per wallet: metrics + pattern features)
       data/ledgers/<wallet>.parquet  (cached raw ledgers for the backtester)
"""
import sys
from pathlib import Path

import pandas as pd

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from pmct.client import PolymarketClient
from pmct import ledger as ledger_mod
from pmct import metrics as metrics_mod
from pmct import patterns as patterns_mod
from pmct import discover as discover_mod
from pmct.util import load_config, cache_path, read_json


def main():
    cfg = load_config()
    acfg = cfg["analyze"]
    candidates = read_json(cache_path(cfg, "candidates.json")) or []
    if not candidates:
        raise SystemExit("No candidates. Run 01_discover_wallets.py first.")
    client = PolymarketClient(sleep_sec=acfg["request_sleep_sec"])

    # authoritative profit/volume from the leaderboard (one pass, reused for all)
    dcfg = cfg["discover"]
    lb_map = discover_mod.leaderboard_profit_map(
        client, dcfg["leaderboard_windows"], dcfg["leaderboard_top_n"])
    print(f"authoritative leaderboard entries: {len(lb_map)}")

    # analyze leaderboard wallets first (authoritative profit = best copy targets),
    # then fill the budget with the rest, so max_wallets isn't wasted on low-hex
    # addresses that happen to sort first.
    candidates.sort(key=lambda w: (0 if w in lb_map else 1, w))
    candidates = candidates[: acfg["max_wallets"]]

    rows = []
    for i, w in enumerate(candidates, 1):
        try:
            raw = list(client.iter_activity(
                w, acfg["activity_page_limit"], acfg["max_pages_per_wallet"]))
            led = ledger_mod.normalize_activity(raw)
            if led.empty:
                continue
            open_df = ledger_mod.attach_open_positions(client.positions(w))
            stats = metrics_mod.compute(w, led, open_df)
            feats = patterns_mod.extract(w, led)
            row = {**stats.to_dict(), **feats.to_dict()}
            row.pop("wallet", None)

            lb = lb_map.get(w, {})
            lb_profit = lb.get("lb_profit")
            row["on_leaderboard"] = bool(lb)
            row["lb_profit"] = lb_profit
            row["lb_volume"] = lb.get("lb_volume")
            # authoritative profit: trust the leaderboard when ranked, else our recon
            row["profit_final"] = lb_profit if lb_profit is not None else row["total_pnl"]
            # recompute rebate share against the AUTHORITATIVE profit so truncated
            # (negative) reconstructions don't falsely flag profitable wallets
            pf = row["profit_final"]
            if pf and pf > 0:
                row["rebate_share"] = round(min(max(row["rebate_pnl"] / pf, 0.0), 1.0), 4)
            elif row["rebate_pnl"] > 0:
                row["rebate_share"] = 1.0   # no net directional profit, only rebates
            row = {"wallet": w, **row}
            rows.append(row)

            led.to_pickle(cache_path(cfg, "ledgers", f"{w}.pkl"))
            print(f"[{i}/{len(candidates)}] {w}  pnl=${stats.total_pnl:>10}  "
                  f"wr={stats.win_rate:.2f}  rep={feats.repeatability:.2f}")
        except Exception as e:  # noqa: BLE001 - keep going through the batch
            print(f"[{i}/{len(candidates)}] {w}  ERROR {e}")

    df = pd.DataFrame(rows)
    out = cache_path(cfg, "wallets.csv")
    df.to_csv(out, index=False)
    print(f"\nAnalyzed {len(df)} wallets -> {out}")


if __name__ == "__main__":
    main()