File size: 2,693 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
"""Filter the analyzed wallets down to a copy-worthy shortlist.

Two gates, matching the user's two rules:
  A. long-term consistent profit  -> pnl/roi/win-rate/drawdown/consistency + span
  B. repeatable, replicable pattern -> repeatability score from patterns.py

Wallets must clear BOTH gates. Survivors are ranked by a composite score so the
strongest, most copyable traders float to the top.
"""
from __future__ import annotations

import pandas as pd


def _zscore(s: pd.Series) -> pd.Series:
    if s.std(ddof=0) == 0:
        return pd.Series(0.0, index=s.index)
    return (s - s.mean()) / s.std(ddof=0)


def screen(df: pd.DataFrame, cfg: dict) -> pd.DataFrame:
    """`df` is the merged metrics+patterns table (one row per wallet)."""
    if df.empty:
        return df
    df = df.copy()

    # authoritative profit: leaderboard figure when the wallet is ranked, else
    # our reconstructed total_pnl. Added by 02_analyze; fall back if absent.
    if "profit_final" not in df.columns:
        df["profit_final"] = df["total_pnl"]
    df["profit_final"] = df["profit_final"].fillna(df["total_pnl"])
    rebate_share = df.get("rebate_share", pd.Series(0.0, index=df.index)).fillna(0.0)

    # Gate A — long-term consistent profit
    gate_profit = (
        (df["profit_final"] >= cfg["min_total_pnl_usd"]) &
        (df["roi"] >= cfg["min_roi"]) &
        (df["win_rate"] >= cfg["min_win_rate"]) &
        (df["n_resolved_markets"] >= cfg["min_resolved_markets"]) &
        (df["active_days"] >= cfg["min_active_days"]) &
        (df["max_drawdown_pct"] <= cfg["max_drawdown_pct"]) &
        (df["monthly_profit_ratio"] >= cfg["min_monthly_profit_ratio"])
    )
    # Gate B — repeatable pattern
    gate_pattern = df["repeatability"] >= cfg["min_repeatability"]
    # Gate C — copyable: reject market-maker / rebate-driven profit
    gate_copyable = rebate_share <= cfg.get("max_rebate_share", 0.35)

    kept = df[gate_profit & gate_pattern & gate_copyable].copy()
    if kept.empty:
        return kept

    # composite rank: profit quality + consistency + copyability, penalize
    # drawdown and rebate-dependence
    kept["score"] = (
        1.2 * _zscore(kept["roi"]) +
        1.0 * _zscore(kept["win_rate"]) +
        0.8 * _zscore(kept["monthly_profit_ratio"]) +
        1.0 * _zscore(kept["repeatability"]) +
        0.6 * _zscore(kept["profit_final"].clip(upper=kept["profit_final"].quantile(0.95))) -
        0.8 * _zscore(kept["max_drawdown_pct"]) -
        0.5 * _zscore(kept["rebate_share"].fillna(0.0))
    )
    kept = kept.sort_values("score", ascending=False)
    top_n = cfg.get("top_n_shortlist", 25)
    return kept.head(top_n).reset_index(drop=True)