Telegram userbot (Telethon) слушает сигналы от CryptoVolk и автоматически торгует на Bybit Futures. Двухуровневый парсер: regex (быстро) + LLM fallback (z.ai) для нестандартных форматов. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
138 lines
3.6 KiB
Python
138 lines
3.6 KiB
Python
"""Parse CryptoVolk signal bot messages.
|
|
|
|
Handles Russian, English, and emoji-only formats.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class Signal:
|
|
direction: str # "long" | "short"
|
|
symbol: str # ccxt: BTC/USDT:USDT
|
|
raw_symbol: str # original: BTCUSDT
|
|
entry: float
|
|
stop: float
|
|
targets: list[float]
|
|
leverage: int
|
|
|
|
|
|
@dataclass
|
|
class CloseSignal:
|
|
direction: str # original position direction
|
|
symbol: str
|
|
price: float
|
|
|
|
|
|
def _convert_symbol(raw: str) -> str:
|
|
"""BTCUSDT.P -> BTC/USDT:USDT"""
|
|
s = raw.lstrip("#").replace(".P", "")
|
|
for quote in ("USDT", "USDC"):
|
|
if s.endswith(quote):
|
|
base = s[: -len(quote)]
|
|
return f"{base}/{quote}:{quote}"
|
|
return f"{s}/USDT:USDT"
|
|
|
|
|
|
def _find_number(text: str, patterns: list[str]) -> float | None:
|
|
"""Try multiple regex patterns, return first match as float."""
|
|
for pat in patterns:
|
|
m = re.search(pat, text, re.IGNORECASE)
|
|
if m:
|
|
return float(m.group(1))
|
|
return None
|
|
|
|
|
|
def parse_signal(text: str) -> Signal | None:
|
|
"""Parse new entry signal."""
|
|
# Direction: emoji + keyword
|
|
upper = text.upper()
|
|
if ("LONG" in upper) and ("\U0001f7e2" in text or "LONG" in upper):
|
|
direction = "long"
|
|
elif ("SHORT" in upper) and ("\U0001f534" in text or "SHORT" in upper):
|
|
direction = "short"
|
|
else:
|
|
return None
|
|
|
|
# Symbol: #BTCUSDT.P
|
|
m = re.search(r"#(\w+\.P)", text)
|
|
if not m:
|
|
return None
|
|
raw_sym = m.group(1)
|
|
symbol = _convert_symbol(raw_sym)
|
|
|
|
# Entry price: Вход / Entry / 📍 Вход
|
|
entry = _find_number(text, [
|
|
r"Вход[:\s]*([\d.]+)",
|
|
r"Entry[:\s]*([\d.]+)",
|
|
r"📍\s*(?:Вход|Entry)?[:\s]*([\d.]+)",
|
|
])
|
|
if entry is None:
|
|
return None
|
|
|
|
# Stop-loss: Стоп / Stop / SL / ⛔ Стоп
|
|
stop = _find_number(text, [
|
|
r"Стоп[:\s]*([\d.]+)",
|
|
r"Stop[:\s]*([\d.]+)",
|
|
r"SL[:\s]*([\d.]+)",
|
|
r"⛔️?\s*(?:Стоп|Stop|SL)?[:\s]*([\d.]+)",
|
|
])
|
|
if stop is None:
|
|
return None
|
|
|
|
# Targets: Цель / Target / TP / 🎯 Цель
|
|
targets = []
|
|
for pat in [
|
|
r"Цель\s*\d*[:\s]*([\d.]+)",
|
|
r"Target\s*\d*[:\s]*([\d.]+)",
|
|
r"TP\s*\d*[:\s]*([\d.]+)",
|
|
r"🎯\s*(?:Цель|Target|TP)?\s*\d*[:\s]*([\d.]+)",
|
|
]:
|
|
found = [float(x) for x in re.findall(pat, text, re.IGNORECASE)]
|
|
if found:
|
|
targets = found
|
|
break
|
|
if not targets:
|
|
return None
|
|
|
|
# Leverage: Плечо / Leverage / X
|
|
lev = _find_number(text, [
|
|
r"Плечо[:\s]*(\d+)",
|
|
r"Leverage[:\s]*(\d+)",
|
|
r"(\d+)\s*[Xx](?:\s|$)",
|
|
])
|
|
leverage = int(lev) if lev else 20
|
|
|
|
return Signal(direction, symbol, raw_sym, entry, stop, targets, leverage)
|
|
|
|
|
|
def parse_close(text: str) -> CloseSignal | None:
|
|
"""Parse profit-taking / close recommendation."""
|
|
upper = text.upper()
|
|
is_close = any(kw in text for kw in [
|
|
"Фиксация прибыли", "Take profit", "Close position",
|
|
]) or ("✅" in text and "#" in text)
|
|
|
|
if not is_close:
|
|
return None
|
|
|
|
direction = "long" if "LONG" in upper else "short" if "SHORT" in upper else None
|
|
if direction is None:
|
|
return None
|
|
|
|
m = re.search(r"#(\w+\.P)", text)
|
|
if not m:
|
|
return None
|
|
symbol = _convert_symbol(m.group(1))
|
|
|
|
price = _find_number(text, [
|
|
r"Цена[:\s]*([\d.]+)",
|
|
r"Price[:\s]*([\d.]+)",
|
|
r"📊\s*[:\s]*([\d.]+)",
|
|
])
|
|
|
|
return CloseSignal(direction, symbol, price or 0.0)
|