Spaces:
Running
Running
Download scripts/02_analyze_wallets.py from sharpflowapp/web: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/sharpflowapp/web/resolve/ffbe64b08e6ebb9a5ae0285440deb059e2a0730c/scripts/02_analyze_wallets.py
- Command line
-
hf download hf://spaces/sharpflowapp/web@ffbe64b08e6ebb9a5ae0285440deb059e2a0730c/scripts/02_analyze_wallets.py
-
curl -L -o 02_analyze_wallets.py https://huggingface.co/spaces/sharpflowapp/web/resolve/ffbe64b08e6ebb9a5ae0285440deb059e2a0730c/scripts/02_analyze_wallets.py
3.67 kB
| #!/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() | |