File size: 1,595 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 | """
Per-pair link + symbol helpers (decision D7).
• display_name('BTC-USDT-SWAP') -> 'BTC'
• pair_label('BTC-USDT-SWAP') -> 'BTC/USDT'
• tv_link(symbol) -> TradingView chart URL on the configured
exchange (TV_EXCHANGE var: BYBIT|BINANCE|MEXC|OKX)
The "Chart" annotated-screenshot link is produced in the UI layer (Step 4) using
chart.py; this module only builds the lightweight text/URL bits.
"""
from __future__ import annotations
import config
# TradingView symbol suffix per exchange. Perps use the '.P' suffix on TV.
_TV_PERP_SUFFIX = {
"BYBIT": ".P",
"BINANCE": ".P",
"MEXC": ".P",
"OKX": ".P",
}
def parts(symbol: str) -> tuple[str, str]:
"""'BTC-USDT-SWAP' -> ('BTC', 'USDT'). Tolerant of plain 'BTCUSDT'."""
if "-" in symbol:
bits = symbol.split("-")
return bits[0], (bits[1] if len(bits) > 1 else "USDT")
if symbol.upper().endswith("USDT"):
return symbol[:-4].upper(), "USDT"
return symbol.upper(), "USDT"
def display_name(symbol: str) -> str:
return parts(symbol)[0]
def pair_label(symbol: str) -> str:
base, quote = parts(symbol)
return f"{base}/{quote}"
def tv_link(symbol: str, tv_exchange: str | None = None) -> str:
"""Build a TradingView URL pointing at the user's chosen exchange."""
ex = (tv_exchange or config.TV_EXCHANGE).upper()
base, quote = parts(symbol)
suffix = _TV_PERP_SUFFIX.get(ex, ".P")
tv_symbol = f"{ex}:{base}{quote}{suffix}"
return f"https://www.tradingview.com/chart/?symbol={tv_symbol}"
|