""" Scoring & conviction (spec §4, extended per user design). Two distinct numbers: • CONFLUENCE (0..100) — is this a valid setup? weighted sum of the 4 layers → tier WATCH / SETUP / SNIPER. • CONVICTION (0..100) — how good is it vs EVERY other live setup? Blends confluence with coin quality (liquidity), reversal strength, trend safety, and data maturity. There is NO cap on active signals — instead every qualifying setup is ranked by conviction and tagged (Elite / Strong / Moderate / Watch), so the user scans the strongest opportunities first. Entry/stop/target levels are ATR-based (spec §4 / §6). """ from __future__ import annotations import math from dataclasses import dataclass, field from config import THRESHOLDS as T from .layers import clamp @dataclass class LayerScores: l1: float l2: float l3: float l4: float @dataclass class Setup: symbol: str direction: str # 'long' | 'short' layers: LayerScores confluence: float # 0..100 tier: str # 'sniper' | 'setup' | 'watch' | 'none' conviction: float = 0.0 # 0..100 conviction_tag: str = "watch" entry: float = 0.0 stop: float = 0.0 target1: float = 0.0 target2: float = 0.0 rr1: float = 0.0 size_mult: float = 0.0 detail: dict = field(default_factory=dict) def confluence_score(ls: LayerScores) -> float: w = T["layer_weights"] s = ls.l1 * w["L1"] + ls.l2 * w["L2"] + ls.l3 * w["L3"] + ls.l4 * w["L4"] return round(clamp(s) * 100, 1) def tier_of(confluence: float) -> str: t = T["tiers"] if confluence >= t["sniper"]: return "sniper" if confluence >= t["setup"]: return "setup" if confluence >= t["watch"]: return "watch" return "none" def _liquidity_factor(turnover_usd: float) -> float: floor, ref = T["liq_turnover_floor_usd"], T["liq_turnover_ref_usd"] if turnover_usd <= floor: return 0.0 return clamp(math.log(turnover_usd / floor) / math.log(ref / floor)) def _trend_safety(direction: str, btc_trend_atr: float, btc_dir: str) -> float: """1.0 = safe. Counter-trend into a strong BTC move is penalised; with-trend or calm BTC = safe. btc_dir is 'up'/'down'/'flat'.""" counter = (direction == "long" and btc_dir == "down") or \ (direction == "short" and btc_dir == "up") if not counter: return 1.0 hostility = clamp(btc_trend_atr / T["btc_trend_atr_ref"]) return clamp(1.0 - hostility) def conviction( setup: Setup, turnover_usd: float, baseline_days: float, btc_trend_atr: float = 0.0, btc_dir: str = "flat", ) -> None: """Compute conviction + tag + size on a Setup in place.""" w = T["conviction_w"] f_conf = setup.confluence / 100.0 f_liq = _liquidity_factor(turnover_usd) f_rev = clamp(0.6 * setup.layers.l1 + 0.4 * setup.layers.l4) f_trend = _trend_safety(setup.direction, btc_trend_atr, btc_dir) f_base = clamp(baseline_days / 7.0) score = (f_conf * w["confluence"] + f_liq * w["liquidity"] + f_rev * w["reversal"] + f_trend * w["trend"] + f_base * w["baseline"]) setup.conviction = round(clamp(score) * 100, 1) setup.conviction_tag = conviction_tag(setup.conviction) setup.size_mult = T["size_by_tag"][setup.conviction_tag] setup.detail.update({ "f_conf": round(f_conf, 3), "f_liq": round(f_liq, 3), "f_rev": round(f_rev, 3), "f_trend": round(f_trend, 3), "f_base": round(f_base, 3), }) def conviction_tag(conv: float) -> str: t = T["conviction_tags"] if conv >= t["elite"]: return "elite" if conv >= t["strong"]: return "strong" if conv >= t["moderate"]: return "moderate" return "watch" def compute_levels(setup: Setup, flush_extreme: float, atr_val: float) -> None: """ATR-based entry/stop/targets (spec §4/§6). flush_extreme = the low (long) or high (short) of the flush candle.""" buf = T["entry_buffer_atr"] * atr_val stop_dist = T["stop_atr_mult"] * atr_val r1, r2 = T["target_r_multiples"] if setup.direction == "long": entry = flush_extreme + buf stop = flush_extreme - stop_dist risk = entry - stop setup.target1 = entry + r1 * risk setup.target2 = entry + r2 * risk else: entry = flush_extreme - buf stop = flush_extreme + stop_dist risk = stop - entry setup.target1 = entry - r1 * risk setup.target2 = entry - r2 * risk setup.entry, setup.stop = entry, stop setup.rr1 = round(r1, 2) def build_setup(symbol: str, direction: str, ls: LayerScores) -> Setup: conf = confluence_score(ls) return Setup(symbol=symbol, direction=direction, layers=ls, confluence=conf, tier=tier_of(conf)) def rank(setups: list[Setup]) -> list[Setup]: """All qualifying setups sorted strongest-first by conviction (no cap).""" return sorted(setups, key=lambda s: s.conviction, reverse=True)