feat: signal-copier — копитрейдинг бот для @Cryptovolksignalbot
Telegram userbot (Telethon) слушает сигналы от CryptoVolk и автоматически торгует на Bybit Futures. Двухуровневый парсер: regex (быстро) + LLM fallback (z.ai) для нестандартных форматов. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+237
@@ -0,0 +1,237 @@
|
||||
"""Tests for regex + LLM signal parsers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
|
||||
from signal_parser import parse_signal, parse_close, Signal, CloseSignal
|
||||
from llm_parser import LLMParser
|
||||
|
||||
# ── Test data ────────────────────────────────────────────────────────
|
||||
|
||||
SIGNAL_RU = """🟢 LONG #BTCUSDT.P
|
||||
|
||||
📍 Вход: 68314.5
|
||||
⛔️ Стоп: 65923.5 (3.5%)
|
||||
|
||||
🎯 Цель 1: 70363.9 (3%)
|
||||
🎯 Цель 2: 71730.2 (5%)
|
||||
🎯 Цель 3: 72413.4 (6%)
|
||||
🎯 Цель 4: 73096.5 (7%)
|
||||
|
||||
📌 После достижения 3-го тейка рекомендуется поставить стоп в безубыток (Б/У).
|
||||
|
||||
⚙️ Плечо: 20X
|
||||
💰 Риск: до 3% от депозита"""
|
||||
|
||||
SIGNAL_EN = """🟢 LONG #ETHUSDT.P
|
||||
|
||||
Entry: 3450.5
|
||||
Stop: 3350.0 (2.9%)
|
||||
|
||||
Target 1: 3550.0 (2.9%)
|
||||
Target 2: 3650.0 (5.8%)
|
||||
|
||||
Leverage: 15X"""
|
||||
|
||||
SIGNAL_SHORT = """🔴 SHORT #SOLUSDT.P
|
||||
|
||||
📍 Вход: 180.50
|
||||
⛔️ Стоп: 190.00 (5.3%)
|
||||
|
||||
🎯 Цель 1: 170.00 (5.8%)
|
||||
🎯 Цель 2: 160.00 (11.4%)
|
||||
🎯 Цель 3: 150.00 (16.9%)
|
||||
|
||||
⚙️ Плечо: 10X"""
|
||||
|
||||
CLOSE_RU = """🎯 Фиксация прибыли по LONG позиции
|
||||
✅ #BTCUSDT.P
|
||||
📊 Цена: 70880.1"""
|
||||
|
||||
CLOSE_EN = """✅ Close position SHORT #ETHUSDT.P
|
||||
Price: 3200.0"""
|
||||
|
||||
SPAM_MESSAGES = [
|
||||
"🎁 Нажми «+», если хочешь получить мини курс трейдинга в подарок 🎁",
|
||||
"😀 РАСПИСАНИЕ ЭФИРОВ НА СЛЕДУЮЩУЮ НЕДЕЛЮ",
|
||||
"Ну что, получилось разобраться с инструкцией?",
|
||||
"👋 Отлично, супер, что вышло разобраться.",
|
||||
"",
|
||||
]
|
||||
|
||||
# Unusual format that regex won't parse but LLM should
|
||||
SIGNAL_UNUSUAL = """LONG позиция по #XRPUSDT.P
|
||||
Цена входа — 0.62
|
||||
Защитный стоп — 0.58
|
||||
Первая цель — 0.66
|
||||
Вторая цель — 0.70
|
||||
Третья цель — 0.75
|
||||
Кредитное плечо 20"""
|
||||
|
||||
|
||||
# ── Regex tests ──────────────────────────────────────────────────────
|
||||
|
||||
def test_regex():
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
def check(name, result, expected_type, checks=None):
|
||||
nonlocal passed, failed
|
||||
if expected_type is None:
|
||||
if result is None:
|
||||
print(f" ✓ {name}: None")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ {name}: expected None, got {result}")
|
||||
failed += 1
|
||||
return
|
||||
if result is None:
|
||||
print(f" ✗ {name}: expected {expected_type.__name__}, got None")
|
||||
failed += 1
|
||||
return
|
||||
if not isinstance(result, expected_type):
|
||||
print(f" ✗ {name}: wrong type {type(result).__name__}")
|
||||
failed += 1
|
||||
return
|
||||
if checks:
|
||||
for field, expected in checks.items():
|
||||
actual = getattr(result, field, "MISSING")
|
||||
if actual != expected:
|
||||
print(f" ✗ {name}.{field}: {actual} != {expected}")
|
||||
failed += 1
|
||||
return
|
||||
print(f" ✓ {name}: OK")
|
||||
passed += 1
|
||||
|
||||
print("\n=== Regex Parser Tests ===")
|
||||
|
||||
# Signals
|
||||
check("RU LONG", parse_signal(SIGNAL_RU), Signal, {
|
||||
"direction": "long",
|
||||
"symbol": "BTC/USDT:USDT",
|
||||
"entry": 68314.5,
|
||||
"stop": 65923.5,
|
||||
"leverage": 20,
|
||||
})
|
||||
r = parse_signal(SIGNAL_RU)
|
||||
check("RU targets count", r, Signal)
|
||||
if r:
|
||||
assert len(r.targets) == 4, f"Expected 4 targets, got {len(r.targets)}"
|
||||
print(f" ✓ RU targets: {r.targets}")
|
||||
passed += 1
|
||||
|
||||
check("EN LONG", parse_signal(SIGNAL_EN), Signal, {
|
||||
"direction": "long",
|
||||
"symbol": "ETH/USDT:USDT",
|
||||
"entry": 3450.5,
|
||||
"stop": 3350.0,
|
||||
"leverage": 15,
|
||||
})
|
||||
|
||||
check("SHORT", parse_signal(SIGNAL_SHORT), Signal, {
|
||||
"direction": "short",
|
||||
"symbol": "SOL/USDT:USDT",
|
||||
"entry": 180.50,
|
||||
"stop": 190.00,
|
||||
"leverage": 10,
|
||||
})
|
||||
|
||||
# Close signals
|
||||
check("Close RU", parse_close(CLOSE_RU), CloseSignal, {
|
||||
"direction": "long",
|
||||
"symbol": "BTC/USDT:USDT",
|
||||
"price": 70880.1,
|
||||
})
|
||||
|
||||
check("Close EN", parse_close(CLOSE_EN), CloseSignal, {
|
||||
"direction": "short",
|
||||
"symbol": "ETH/USDT:USDT",
|
||||
"price": 3200.0,
|
||||
})
|
||||
|
||||
# Spam rejection
|
||||
for i, spam in enumerate(SPAM_MESSAGES):
|
||||
check(f"Spam #{i+1}", parse_signal(spam), None)
|
||||
check(f"Spam close #{i+1}", parse_close(spam), None)
|
||||
|
||||
# Unusual format (regex should fail)
|
||||
check("Unusual (regex miss)", parse_signal(SIGNAL_UNUSUAL), None)
|
||||
|
||||
print(f"\nRegex: {passed} passed, {failed} failed")
|
||||
return failed
|
||||
|
||||
|
||||
# ── LLM tests ────────────────────────────────────────────────────────
|
||||
|
||||
async def test_llm():
|
||||
llm = LLMParser(
|
||||
base_url="https://api.z.ai/api/anthropic",
|
||||
api_key="4d13fce5f9f247f18e2670ee00a369d7.PWbacOpikzPJ62Qu",
|
||||
model="glm-4.7",
|
||||
)
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
print("\n=== LLM Parser Tests ===")
|
||||
|
||||
# Test 1: Standard RU signal
|
||||
r = await llm.parse(SIGNAL_RU)
|
||||
if isinstance(r, Signal) and r.direction == "long" and r.entry == 68314.5:
|
||||
print(f" ✓ LLM RU signal: {r.direction} {r.symbol} entry={r.entry}")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ LLM RU signal: {r}")
|
||||
failed += 1
|
||||
|
||||
# Test 2: Unusual format (regex can't parse, LLM should)
|
||||
r = await llm.parse(SIGNAL_UNUSUAL)
|
||||
if isinstance(r, Signal) and r.direction == "long" and abs(r.entry - 0.62) < 0.01:
|
||||
print(f" ✓ LLM unusual: {r.direction} {r.symbol} entry={r.entry} targets={r.targets}")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ LLM unusual: {r}")
|
||||
failed += 1
|
||||
|
||||
# Test 3: Close signal
|
||||
r = await llm.parse(CLOSE_RU)
|
||||
if isinstance(r, CloseSignal) and r.direction == "long":
|
||||
print(f" ✓ LLM close: {r.direction} {r.symbol} price={r.price}")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ LLM close: {r}")
|
||||
failed += 1
|
||||
|
||||
# Test 4: Spam rejection
|
||||
r = await llm.parse(SPAM_MESSAGES[0])
|
||||
if r is None:
|
||||
print(f" ✓ LLM spam: rejected")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ LLM spam: should be None, got {r}")
|
||||
failed += 1
|
||||
|
||||
# Test 5: SHORT signal
|
||||
r = await llm.parse(SIGNAL_SHORT)
|
||||
if isinstance(r, Signal) and r.direction == "short":
|
||||
print(f" ✓ LLM short: {r.direction} {r.symbol} stop={r.stop}")
|
||||
passed += 1
|
||||
else:
|
||||
print(f" ✗ LLM short: {r}")
|
||||
failed += 1
|
||||
|
||||
print(f"\nLLM: {passed} passed, {failed} failed")
|
||||
return failed
|
||||
|
||||
|
||||
# ── Main ─────────────────────────────────────────────────────────────
|
||||
|
||||
if __name__ == "__main__":
|
||||
regex_fails = test_regex()
|
||||
llm_fails = asyncio.run(test_llm())
|
||||
total_fails = regex_fails + llm_fails
|
||||
print(f"\n{'='*40}")
|
||||
print(f"Total: {total_fails} failures")
|
||||
sys.exit(1 if total_fails else 0)
|
||||
Reference in New Issue
Block a user