Download exchange/base.py from abyXsmart/Confluence_Reversal_Engine: direct link, hf CLI and curl.
- Browser
- Download file 4.68 kB
-
https://huggingface.co/spaces/abyXsmart/Confluence_Reversal_Engine/resolve/main/exchange/base.py
- Command line
-
hf download hf://spaces/abyXsmart/Confluence_Reversal_Engine/exchange/base.py
-
curl -L -o base.py https://huggingface.co/spaces/abyXsmart/Confluence_Reversal_Engine/resolve/main/exchange/base.py
4.68 kB
| """ | |
| ExchangeClient interface (spec Β§12.1: "Build this abstraction from day one"). | |
| OKX is the one implemented backend (the only free, geo-reachable source that | |
| exposes all four layers). The ABC is kept deliberately: it documents the data | |
| contract and lets a second source be added later without touching the engine. | |
| Data is returned in normalized shapes regardless of upstream JSON quirks. | |
| The seven core methods are abstract (any backend must implement them). The | |
| universe/taker helpers are optional (default NotImplementedError) so a future | |
| minimal backend can be added without implementing the full surface β the active | |
| source (OKX) implements everything. | |
| """ | |
| from __future__ import annotations | |
| from abc import ABC, abstractmethod | |
| from dataclasses import dataclass | |
| from typing import AsyncIterator | |
| class LiqEvent: | |
| """One liquidation order, normalized across exchanges. | |
| liquidated_side: 'long' -> a long position was force-closed (a forced SELL) | |
| 'short' -> a short position was force-closed (a forced BUY) | |
| notional: price * qty in USD(T). ts_ms: event time, unix milliseconds. | |
| """ | |
| symbol: str | |
| liquidated_side: str # 'long' | 'short' | |
| price: float | |
| qty: float | |
| notional: float | |
| ts_ms: int | |
| class Ticker: | |
| """A perp ticker row used by the universe funnel (spec Β§2.4, D5).""" | |
| symbol: str # exchange-native instrument id (e.g. 'BTC-USDT-SWAP') | |
| last: float # last price | |
| turnover_usd: float # 24h turnover in USD (liquidity ranking key) | |
| class ExchangeClient(ABC): | |
| """Async interface every exchange backend must implement.""" | |
| name: str = "base" | |
| # ββ REST: market data (core, required) ββββββββββββββββββββββββββββββββββ | |
| async def funding_rate(self, symbol: str) -> float: | |
| """Current funding rate (fraction, e.g. 0.0001 = 0.01%).""" | |
| async def funding_history(self, symbol: str, limit: int) -> list[float]: | |
| """Historical funding rates (oldestβnewest) for rolling z-score.""" | |
| async def open_interest(self, symbol: str) -> float: | |
| """Current open interest in USD value.""" | |
| async def oi_history(self, symbol: str, period: str, limit: int) -> list[float]: | |
| """Historical OI (USD) oldestβnewest for percentile computation.""" | |
| async def klines(self, symbol: str, interval: str, limit: int) -> list[dict]: | |
| """OHLCV bars oldestβnewest. Each: {ts, open, high, low, close, volume, | |
| taker_buy_volume}.""" | |
| async def mark_price(self, symbol: str) -> float: | |
| """Current mark price.""" | |
| # ββ REST: universe & order-flow (optional β used by the active source) βββ | |
| async def tickers_universe(self) -> list[Ticker]: | |
| """All USDT-perp tickers for the dynamic-universe funnel (spec Β§2.4/D5).""" | |
| raise NotImplementedError(f"{self.name} has no tickers_universe()") | |
| async def open_interest_map(self) -> dict[str, float]: | |
| """Batch {symbol: oi_base} for all perps (one call) to avoid N requests.""" | |
| raise NotImplementedError(f"{self.name} has no open_interest_map()") | |
| async def taker_volume(self, symbol: str, period: str, limit: int) -> list[dict]: | |
| """Taker buy/sell volume bars (oldestβnewest) for L3. Each: {ts, buy, sell}.""" | |
| raise NotImplementedError(f"{self.name} has no taker_volume()") | |
| # ββ WebSocket: liquidations (spec Β§2.1) βββββββββββββββββββββββββββββββββ | |
| def liquidation_stream(self) -> AsyncIterator[LiqEvent]: | |
| """Async generator yielding normalized LiqEvents for ALL symbols. | |
| Implementations must auto-reconnect with exponential backoff.""" | |
| async def aclose(self) -> None: | |
| """Release HTTP/WS resources.""" | |
| def get_client(exchange: str, watchlist: list[str], timeout: float): | |
| """Factory: return the configured ExchangeClient. | |
| Only OKX is implemented in v1.0 β Binance/Bybit are geo-blocked on free hosts | |
| (verified: HTTP 451/403) and were removed. Branch here to add a new source. | |
| """ | |
| exchange = (exchange or "okx").lower().strip() | |
| if exchange == "okx": | |
| from .okx import OKXClient | |
| return OKXClient(watchlist=watchlist, timeout=timeout) | |
| raise ValueError( | |
| f"EXCHANGE={exchange!r} is not supported in v1.0 (only 'okx'). " | |
| f"Binance/Bybit are geo-blocked on free hosts." | |
| ) | |