File size: 7,071 Bytes
c6262df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
CRE v1.0 β€” Central configuration.

Single source of truth for runtime settings AND version-controlled strategy
thresholds (spec Β§7). Engine code reads from here; never hard-codes a number.
THRESHOLDS is snapshotted into config_versions at boot so every signal is
traceable to the exact parameter set that produced it.

Infra settings come from environment variables (HF Space Secrets/Variables);
strategy thresholds are code-defined and version-bumped deliberately.
"""
from __future__ import annotations
import os

# ── Version ──────────────────────────────────────────────────────────────────
APP_VERSION = "1.0"
CONFIG_VERSION = 3  # bumped: engine thresholds + conviction ranking (no active cap)

# ── Exchange / data source (spec Β§2, Β§12 geo-fallback) ───────────────────────
# OKX is the data source: the only free, geo-reachable exchange that preserves ALL
# four layers β€” funding, OI, klines, liquidations (L1) and the taker buy/sell split
# (L3). Binance/Bybit are geo-blocked on free hosts (451/403) and were removed in v1.0.
EXCHANGE = os.getenv("EXCHANGE", "okx").lower().strip()  # okx (only supported source)

# ── Universe: dynamic whole-market scan with a quality funnel (D5) ────────────
# Mode "dynamic" = scan all USDT perps, keep the liquid ones. Mode "fixed" =
# only the symbols in WATCHLIST. Setting WATCHLIST forces fixed mode.
_WATCHLIST_ENV = os.getenv("WATCHLIST", "").strip()
UNIVERSE_MODE = "fixed" if _WATCHLIST_ENV else os.getenv("UNIVERSE_MODE", "dynamic").lower()
WATCHLIST = [s.strip().upper() for s in _WATCHLIST_ENV.split(",") if s.strip()]

# Funnel parameters (the "remove the junk" filter). Tunable via env.
UNIVERSE_MAX = int(os.getenv("UNIVERSE_MAX", "80"))                       # cap on coins scanned
UNIVERSE_MIN_TURNOVER_USD = float(os.getenv("UNIVERSE_MIN_TURNOVER_USD", "20000000"))  # min 24h USD turnover
UNIVERSE_REFRESH_SEC = int(os.getenv("UNIVERSE_REFRESH_SEC", "3600"))     # rebuild hourly
# Bases excluded outright (stables / wrapped / non-tradeable noise).
UNIVERSE_EXCLUDE_BASES = {
    "USDC", "USDT", "DAI", "TUSD", "FDUSD", "USDD", "USDE", "PYUSD",
    "EUR", "GUSD", "USTC",
}

# ── Per-pair links (D7) ──────────────────────────────────────────────────────
# Where the "TV" link points. You trade on Bybit β†’ default BYBIT. Switch freely.
TV_EXCHANGE = os.getenv("TV_EXCHANGE", "BYBIT").upper().strip()  # BYBIT|BINANCE|MEXC|OKX
CHART_ENABLED = os.getenv("CHART_ENABLED", "1") == "1"           # annotated screenshot on/off

# ── Storage (spec Β§12 ephemeral-storage warning) ─────────────────────────────
DB_PATH = os.getenv("DB_PATH", "./cre.db")  # HF persistent storage β†’ /data/cre.db

# ── Polling cadence ──────────────────────────────────────────────────────────
POLL_INTERVAL_SEC = int(os.getenv("POLL_INTERVAL_SEC", "60"))
HTTP_TIMEOUT_SEC = float(os.getenv("HTTP_TIMEOUT_SEC", "15"))
WS_BACKOFF_START = 1.0
WS_BACKOFF_CAP = 60.0

# ── Telegram alerts (spec Β§10) β€” SECRETS, never hard-code (D8) ────────────────
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "").strip()
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", "").strip()
ALERTS_ENABLED = bool(TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID)

# ── Optional Coinglass enrichment (spec Β§2.2) ────────────────────────────────
COINGLASS_API_KEY = os.getenv("COINGLASS_API_KEY", "").strip()
COINGLASS_ENABLED = bool(COINGLASS_API_KEY)

# ── Liquidation baseline (spec Β§2.1, Β§15.3) ──────────────────────────────────
LIQ_BASELINE_DAYS_REQUIRED = 7  # cold-start: WATCH-only until this many days collected

# ── Strategy thresholds (version-controlled, spec Β§3–§6) ─────────────────────
# Layer/scoring/risk thresholds (Β§3–§6) are appended in Step 2 alongside the
# engine code that consumes them; CONFIG_VERSION bumps then.
THRESHOLDS: dict = {
    "funding_history_limit": 100,    # OKX caps funding history at 100/call
    "oi_history_period": "1H",       # OKX rubik period format
    "oi_history_limit": 200,
    "kline_intervals": ["1m", "15m", "1H"],
    "kline_limit": 300,
    "taker_period": "1H",
    "poll_interval_sec": POLL_INTERVAL_SEC,
    "liq_baseline_days_required": LIQ_BASELINE_DAYS_REQUIRED,
    "universe_max": UNIVERSE_MAX,
    "universe_min_turnover_usd": UNIVERSE_MIN_TURNOVER_USD,

    # ── Engine: layer weights (sum=1.0) β€” confluence score (spec Β§3/Β§4) ──────
    "layer_weights": {"L1": 0.30, "L2": 0.25, "L3": 0.25, "L4": 0.20},
    "liq_flush_cap_mult": 5.0,   # flush / rolling baseline; cap_multΓ— => full L1
    "funding_z_ref": 2.0,        # |funding_z| saturating the funding sub-score
    "oi_drop_ref_pct": 5.0,      # % OI drop giving full OI sub-score
    "funding_sub_w": 0.6, "oi_sub_w": 0.4,
    "taker_delta_z_ref": 2.0,    # |taker delta z| saturating L3
    "struct_w": {"fvg": 0.40, "swing": 0.35, "bos": 0.25},
    "swing_lookback": 3,
    "atr_period": 14,

    # ── Levels (spec Β§4) ─────────────────────────────────────────────────────
    "stop_atr_mult": 1.5,
    "target_r_multiples": [1.0, 2.0],
    "entry_buffer_atr": 0.10,

    # ── Tiers (confluence 0..100) ────────────────────────────────────────────
    "tiers": {"sniper": 75, "setup": 55, "watch": 35},

    # ── Conviction (0..100): ranks ALL qualifying setups (no active cap) ──────
    "conviction_w": {
        "confluence": 0.45, "liquidity": 0.15, "reversal": 0.20,
        "trend": 0.10, "baseline": 0.10,
    },
    "conviction_tags": {"elite": 85, "strong": 70, "moderate": 55},
    "liq_turnover_floor_usd": 2.0e7,
    "liq_turnover_ref_usd": 2.0e9,
    "btc_trend_atr_ref": 2.0,

    # ── Risk (spec Β§6): cooldown kept; hard active/direction caps REMOVED ─────
    "cooldown_hours": 4,
    "size_by_tag": {"elite": 1.0, "strong": 0.7, "moderate": 0.4, "watch": 0.0},
}


def snapshot() -> dict:
    """Full config snapshot persisted to config_versions at boot (spec Β§7)."""
    return {
        "config_version": CONFIG_VERSION,
        "exchange": EXCHANGE,
        "universe_mode": UNIVERSE_MODE,
        "watchlist": WATCHLIST,
        "tv_exchange": TV_EXCHANGE,
        "thresholds": THRESHOLDS,
    }