-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_engine.py
More file actions
433 lines (353 loc) · 14.4 KB
/
pattern_engine.py
File metadata and controls
433 lines (353 loc) · 14.4 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# ============================================================
# pattern_engine.py — ON-CHAIN PATTERN RECOGNITION
# Rescores borderline tokens (60–79) using 5 pattern checks
# If patterns push score to 80+ — auto buys
# ============================================================
import asyncio
import aiohttp
import requests
from datetime import datetime, timedelta
from collections import defaultdict
from config import HELIUS_API_KEY
# ── Pattern thresholds ────────────────────────────────────
LIQ_SPIKE_RATIO = 2.0 # Liquidity doubles = spike
CLUSTER_COUNT = 5 # 5+ unique wallets in 60 secs
CLUSTER_WINDOW_SECS = 60
VOL_ACCEL_RATIO = 3.0 # Buy vol grows 3x faster than sells
SMART_CONVERGENCE_MIN = 2 # 2+ smart wallets = convergence
STEALTH_VOL_USD = 5000 # $5k+ volume with <5% price move
STEALTH_PRICE_MAX_PCT = 5.0
# ── Pattern bonus scores ──────────────────────────────────
PATTERN_SCORES = {
"liquidity_spike": 15,
"wallet_clustering": 20,
"volume_acceleration": 15,
"smart_money_convergence": 25,
"stealth_accumulation": 20,
}
# ── Per-token state for pattern tracking ──────────────────
token_state = defaultdict(lambda: {
"liquidity_history": [], # [(datetime, usd)]
"first_seen": datetime.utcnow()
})
# ── Results cache — avoid re-checking same token ──────────
pattern_cache = {} # { address: { patterns, bonus, total, detected_at } }
# ── Check interval ────────────────────────────────────────
CHECK_INTERVAL = 30 # seconds
# ============================================================
# MAIN ENTRY
# ============================================================
async def start_pattern_engine():
print("[PATTERN] Starting on-chain pattern recognition engine...")
while True:
try:
await run_pattern_checks()
except Exception as e:
print(f"[PATTERN] Loop error: {e}")
await asyncio.sleep(CHECK_INTERVAL)
# ============================================================
# MAIN CHECK LOOP
# ============================================================
async def run_pattern_checks():
"""
Checks borderline tokens (scored 60–79 in last 20 mins)
for on-chain patterns that could push them over 80.
"""
borderline = get_borderline_tokens()
if not borderline:
return
print(f"[PATTERN] Checking {len(borderline)} borderline tokens...")
for token in borderline:
token_address = token.get("token_address", "")
token_name = token.get("token_name", "Unknown")
base_score = token.get("score", 0)
if not token_address or len(token_address) < 30:
continue
# Skip if recently checked
if token_address in pattern_cache:
cached = pattern_cache[token_address]
age_secs = (datetime.utcnow() - cached["detected_at"]).total_seconds()
if age_secs < 120: # Don't recheck within 2 minutes
continue
try:
patterns, bonus = await analyse_all_patterns(token_address)
if not patterns:
continue
total = min(base_score + bonus, 100)
pattern_cache[token_address] = {
"patterns": patterns,
"bonus": bonus,
"total_score": total,
"detected_at": datetime.utcnow()
}
print(
f"[PATTERN] 🎯 {token_name}: "
f"{len(patterns)} patterns | +{bonus} | Total: {total}/100"
)
if total >= 80:
await trigger_pattern_buy(
token_address, token_name,
base_score, bonus, patterns, total
)
except Exception as e:
print(f"[PATTERN] Check error {token_address[:16]}: {e}")
clean_caches()
# ============================================================
# PATTERN ANALYSIS
# ============================================================
async def analyse_all_patterns(token_address):
"""
Runs all 5 pattern checks.
Returns (list_of_pattern_names, total_bonus_score)
"""
patterns = []
bonus = 0
# Single DexScreener fetch — reused by all checks
dex = await fetch_dex_data(token_address)
if not dex:
return [], 0
# Pattern 1 — Liquidity Spike
found, detail = check_liquidity_spike(token_address, dex)
if found:
patterns.append(f"💧 Liquidity Spike ({detail})")
bonus += PATTERN_SCORES["liquidity_spike"]
# Pattern 2 — Wallet Clustering
found, detail = await check_wallet_clustering(token_address)
if found:
patterns.append(f"👥 Wallet Cluster ({detail})")
bonus += PATTERN_SCORES["wallet_clustering"]
# Pattern 3 — Volume Acceleration
found, detail = check_volume_acceleration(dex)
if found:
patterns.append(f"📈 Volume Acceleration ({detail})")
bonus += PATTERN_SCORES["volume_acceleration"]
# Pattern 4 — Smart Money Convergence
found, detail = check_smart_money_convergence(token_address)
if found:
patterns.append(f"🧠 Smart Money Convergence ({detail})")
bonus += PATTERN_SCORES["smart_money_convergence"]
# Pattern 5 — Stealth Accumulation
found, detail = check_stealth_accumulation(dex)
if found:
patterns.append(f"🕵️ Stealth Accumulation ({detail})")
bonus += PATTERN_SCORES["stealth_accumulation"]
return patterns, bonus
def check_liquidity_spike(token_address, dex):
"""Liquidity doubled in last 2 minutes"""
try:
current = dex.get("liquidity", {}).get("usd", 0)
if current <= 0:
return False, ""
now = datetime.utcnow()
history = token_state[token_address]["liquidity_history"]
history.append((now, current))
# Keep last 10 minutes only
history[:] = [(t, l) for t, l in history
if now - t < timedelta(minutes=10)]
# Compare to 2 minutes ago
old = [(t, l) for t, l in history
if now - t >= timedelta(minutes=2)]
if not old:
return False, ""
old_liq = old[0][1]
if old_liq <= 0:
return False, ""
ratio = current / old_liq
if ratio >= LIQ_SPIKE_RATIO:
return True, f"{ratio:.1f}x in 2mins"
return False, ""
except Exception as e:
print(f"[PATTERN] Liquidity spike error: {e}")
return False, ""
async def check_wallet_clustering(token_address):
"""5+ unique wallets buying in 60 seconds"""
try:
url = (
f"https://api.helius.xyz/v0/addresses/{token_address}"
f"/transactions?api-key={HELIUS_API_KEY}&limit=50"
)
async with aiohttp.ClientSession() as s:
async with s.get(url, timeout=aiohttp.ClientTimeout(total=8)) as r:
if r.status != 200:
return False, ""
txs = await r.json()
if not isinstance(txs, list):
return False, ""
now = datetime.utcnow()
cutoff = now - timedelta(seconds=CLUSTER_WINDOW_SECS)
buyers = set()
for tx in txs:
ts = tx.get("timestamp", 0)
if not ts:
continue
if datetime.utcfromtimestamp(ts) < cutoff:
continue
fee_payer = tx.get("feePayer", "")
if not fee_payer:
continue
for transfer in tx.get("tokenTransfers", []):
if (transfer.get("mint") == token_address and
float(transfer.get("tokenAmount", 0) or 0) > 0 and
transfer.get("toUserAccount") == fee_payer):
buyers.add(fee_payer)
break
count = len(buyers)
if count >= CLUSTER_COUNT:
return True, f"{count} wallets/60s"
return False, ""
except Exception as e:
print(f"[PATTERN] Wallet cluster error: {e}")
return False, ""
def check_volume_acceleration(dex):
"""Buy volume growing 3x faster than sells (5min vs 1hr rate)"""
try:
txns = dex.get("txns", {})
m5_buys = txns.get("m5", {}).get("buys", 0)
m5_sells = txns.get("m5", {}).get("sells", 1)
h1_buys = txns.get("h1", {}).get("buys", 0)
h1_sells = txns.get("h1", {}).get("sells", 1)
if not h1_buys or not h1_sells:
return False, ""
recent_ratio = m5_buys / max(m5_sells, 1)
overall_ratio = h1_buys / max(h1_sells, 1)
if overall_ratio <= 0:
return False, ""
accel = recent_ratio / overall_ratio
if accel >= VOL_ACCEL_RATIO:
return True, f"{accel:.1f}x acceleration"
return False, ""
except Exception as e:
print(f"[PATTERN] Volume accel error: {e}")
return False, ""
def check_smart_money_convergence(token_address):
"""2+ smart wallets bought this token independently"""
try:
from database import supabase
if not supabase:
return False, ""
result = supabase.table("trades") \
.select("source") \
.eq("token_address", token_address) \
.eq("action", "BUY") \
.execute()
if not result.data:
return False, ""
copy_buys = sum(
1 for t in result.data
if t.get("source") == "COPY_TRADE"
)
if copy_buys >= SMART_CONVERGENCE_MIN:
return True, f"{copy_buys} smart wallets"
return False, ""
except Exception as e:
print(f"[PATTERN] Smart convergence error: {e}")
return False, ""
def check_stealth_accumulation(dex):
"""Large buy volume with tiny price impact — smart money loading up quietly"""
try:
vol_m5 = float(dex.get("volume", {}).get("m5", 0) or 0)
chg_m5 = abs(float(dex.get("priceChange", {}).get("m5", 0) or 0))
if vol_m5 > STEALTH_VOL_USD and chg_m5 < STEALTH_PRICE_MAX_PCT:
return True, f"${vol_m5:,.0f} vol, {chg_m5:.1f}% move"
return False, ""
except Exception as e:
print(f"[PATTERN] Stealth check error: {e}")
return False, ""
# ============================================================
# TRIGGER BUY
# ============================================================
async def trigger_pattern_buy(token_address, token_name,
base_score, bonus, patterns, total_score):
try:
from database import token_already_seen, mark_token_seen
from filters import run_hard_filters
from telegram_bot import send_alert, alert_new_token_detected
if token_already_seen(token_address):
return
# Re-run filters — state may have changed
passed, reason = run_hard_filters(token_address)
if not passed:
mark_token_seen(token_address, token_name, score=0, decision="FILTERED")
return
# Get ticker
ticker = "???"
try:
url = f"https://api.dexscreener.com/latest/dex/tokens/{token_address}"
resp = requests.get(url, timeout=5)
data = resp.json()
pairs = data.get("pairs", [])
if pairs:
ticker = pairs[0].get("baseToken", {}).get("symbol", "???")
except Exception:
pass
mark_token_seen(token_address, token_name, score=total_score, decision="BOUGHT")
pattern_text = "\n".join(f" • {p}" for p in patterns)
breakdown = {
"base_score": f"{base_score}/100",
"pattern_bonus": f"+{bonus}",
"patterns_found": pattern_text
}
await alert_new_token_detected(
ticker, token_name, token_address,
total_score, breakdown, "PATTERN_ENGINE"
)
from executor import execute_buy
await execute_buy(
token_address=token_address,
token_name=token_name,
ticker=ticker,
score=total_score,
source="PATTERN_ENGINE"
)
except Exception as e:
print(f"[PATTERN] trigger_pattern_buy error: {e}")
# ============================================================
# HELPERS
# ============================================================
async def fetch_dex_data(token_address):
try:
url = f"https://api.dexscreener.com/latest/dex/tokens/{token_address}"
async with aiohttp.ClientSession() as s:
async with s.get(url, timeout=aiohttp.ClientTimeout(total=6)) as r:
if r.status != 200:
return None
data = await r.json()
pairs = data.get("pairs", [])
if not pairs:
return None
return max(pairs, key=lambda x: x.get("liquidity", {}).get("usd", 0))
except Exception as e:
print(f"[PATTERN] fetch_dex_data error: {e}")
return None
def get_borderline_tokens():
"""Tokens scored 60–79 in last 20 minutes — candidates for pattern confirmation"""
try:
from database import supabase
if not supabase:
return []
cutoff = (datetime.utcnow() - timedelta(minutes=20)).isoformat()
result = supabase.table("tokens_seen") \
.select("token_address, token_name, score") \
.eq("decision", "SKIPPED") \
.gte("score", 60) \
.lte("score", 79) \
.gte("seen_at", cutoff) \
.execute()
return result.data if result.data else []
except Exception as e:
print(f"[PATTERN] get_borderline_tokens error: {e}")
return []
def clean_caches():
"""Removes stale cache entries"""
now = datetime.utcnow()
cutoff = now - timedelta(hours=1)
# Clean pattern cache
stale = [a for a, d in pattern_cache.items()
if d.get("detected_at", now) < cutoff]
for a in stale:
del pattern_cache[a]
# Clean token state
stale = [a for a, d in token_state.items()
if now - d.get("first_seen", now) > timedelta(hours=2)]
for a in stale:
del token_state[a]