-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenInterestBot.py
More file actions
787 lines (620 loc) · 24 KB
/
OpenInterestBot.py
File metadata and controls
787 lines (620 loc) · 24 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import os
import html
import logging
import asyncio
from typing import Dict, Any, List, Set
import requests
import pandas as pd
import ccxt
from telegram import (
Update,
KeyboardButton,
ReplyKeyboardMarkup,
)
from telegram.ext import (
Application,
ContextTypes,
CommandHandler,
MessageHandler,
filters,
)
from telegram.error import BadRequest, TimedOut
TG_TOKEN = os.getenv("TG_TOKEN", "")
PROXY_HOST = os.getenv("PROXY_HOST", "")
PROXY_PORT = os.getenv("PROXY_PORT", "")
PROXY_USER = os.getenv("PROXY_USER", "")
PROXY_PASS = os.getenv("PROXY_PASS", "")
if PROXY_HOST and PROXY_PORT:
if PROXY_USER and PROXY_PASS:
PROXY_URL = f"http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}"
else:
PROXY_URL = f"http://{PROXY_HOST}:{PROXY_PORT}"
else:
PROXY_URL = None
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=getattr(logging, LOG_LEVEL, logging.INFO),
format="%(asctime)s | %(levelname)-7s | %(message)s",
)
LOG = logging.getLogger("binance-oi-bot")
EXCHANGE = ccxt.binance({
"enableRateLimit": True,
"options": {"defaultType": "future"},
})
N_DAYS = 30
OI_DAYS = 30
OI_GROWTH_PCT = 50
PRICE_MAX_PCT_UP = 50
MIN_DAILY_VOL_USD = 5_000_000
MAX_MARKETS = 200
SUBSCRIBERS: Set[int] = set()
SEEN_HITS: Set[str] = set()
LAST_AUTOSCAN_STATUS_MSG: Dict[int, int] = {}
BINANCE_OI_URL = "https://fapi.binance.com/futures/data/openInterestHist"
SORT_MODE = "oi_contracts"
SORT_MODE_LABELS = {
"oi_contracts": "by OI growth in contracts",
"oi_usd": "by OI growth in USD",
"price": "by price growth",
"volume": "by average daily volume",
}
def chunk_html(text: str, limit: int = 3500) -> List[str]:
parts: List[str] = []
buf = ""
for line in text.splitlines(keepends=True):
if len(buf) + len(line) > limit:
parts.append(buf)
buf = line
else:
buf += line
if buf:
parts.append(buf)
return parts
def fetch_ohlc(symbol: str, limit: int = 200) -> pd.DataFrame:
try:
ohlc = EXCHANGE.fetch_ohlcv(symbol, timeframe="1d", limit=limit)
return pd.DataFrame(
ohlc,
columns=["time", "open", "high", "low", "close", "volume"],
)
except Exception as e:
LOG.warning(f"OHLC fetch error {symbol}: {e}")
return pd.DataFrame()
def fetch_binance_oi_history(symbol: str, days: int) -> List[Dict[str, float]]:
days = min(days, OI_DAYS)
params = {
"symbol": symbol.replace("/", ""),
"period": "1d",
"limit": min(days + 2, 500),
}
try:
r = requests.get(BINANCE_OI_URL, params=params, timeout=10)
r.raise_for_status()
data = r.json()
except Exception as e:
LOG.warning(f"OI request error for {symbol}: {e}")
return []
out: List[Dict[str, float]] = []
for row in data:
try:
out.append({
"ts": int(row["timestamp"]),
"oi": float(row["sumOpenInterest"]),
})
except Exception:
pass
if not out:
return []
out.sort(key=lambda x: x["ts"])
if len(out) > days:
out = out[-days:]
return out
def get_top_usdt_futures(limit: int = MAX_MARKETS) -> List[str]:
try:
r = requests.get("https://fapi.binance.com/fapi/v1/ticker/24hr", timeout=10)
r.raise_for_status()
data = r.json()
except Exception as e:
LOG.warning(f"Error get_top_usdt_futures: {e}")
return []
items: List[tuple[str, float]] = []
for x in data:
sym = x.get("symbol", "")
if sym.endswith("USDT"):
try:
qv = float(x.get("quoteVolume", 0))
except Exception:
qv = 0.0
items.append((sym, qv))
items.sort(key=lambda x: x[1], reverse=True)
return [f"{s.replace('USDT', '')}/USDT" for s, _ in items[:limit]]
def linkify(symbol: str) -> str:
s = symbol.replace("/", "")
binance_url = f"https://www.binance.com/en/futures/{s}"
tv_url = f"https://www.tradingview.com/chart/?symbol=BINANCE:{s}"
return (
f"<b>{html.escape(symbol)}</b> "
f"(<a href='{binance_url}'>Binance Futures</a> | "
f"<a href='{tv_url}'>TradingView</a>)"
)
def format_symbol_block(
h: Dict[str, Any],
oi_days: int,
include_header: bool = False,
) -> str:
lnk = linkify(h["symbol"])
header = ""
if include_header:
header = f"📊 <b>Analysis for {html.escape(h['symbol'])}</b>\n\n"
line = (
f"{header}"
f"• {lnk}\n"
f" OI (contracts): <b>{h['oi_change_pct']:.1f}%</b> "
f"({h['oi_first']:.0f} → {h['oi_last']:.0f}) over {oi_days} days\n"
f" Price: <b>{h['price_change_pct']:.1f}%</b> over {N_DAYS} days\n"
f" Volume: <b>{h['avg_daily_vol'] / 1_000_000:.2f}M</b> USD (avg daily)\n"
)
if h.get("oi_usd_change_pct") is not None:
line += (
f" OI (≈ USD): <b>{h['oi_usd_change_pct']:.1f}%</b> "
f"(using asset price)\n"
)
return line
def analyze_symbol(symbol: str) -> Dict[str, Any]:
df = fetch_ohlc(symbol, limit=N_DAYS + 20)
if df.empty:
return {"hit": False, "reason": "candles"}
last = df.tail(N_DAYS).copy()
if len(last) < N_DAYS:
return {"hit": False, "reason": "few_days"}
usd_vol = (last["volume"] * last["close"]).astype("float64")
avg_daily_vol = float(usd_vol.mean())
if avg_daily_vol < MIN_DAILY_VOL_USD:
return {"hit": False, "reason": "volume"}
try:
price_first = float(last["close"].iloc[0])
price_last = float(last["close"].iloc[-1])
if price_first <= 0:
return {"hit": False, "reason": "bad_close"}
price_change_pct = (price_last - price_first) / price_first * 100
except Exception:
return {"hit": False, "reason": "price_calc"}
if price_change_pct > PRICE_MAX_PCT_UP:
return {"hit": False, "reason": "price_up_too_much"}
oi_days = min(N_DAYS, OI_DAYS)
oi = fetch_binance_oi_history(symbol, oi_days)
if len(oi) < 2:
return {"hit": False, "reason": "oi_short"}
oi_first = oi[0]["oi"]
oi_last = oi[-1]["oi"]
if oi_first <= 0:
return {"hit": False, "reason": "oi_zero"}
oi_change_pct = (oi_last - oi_first) / oi_first * 100
if oi_change_pct < OI_GROWTH_PCT:
return {"hit": False, "reason": "oi_growth"}
oi_usd_change_pct = None
try:
times = last["time"].astype("int64").tolist()
def nearest_price(ts_ms: int) -> float:
idx = min(range(len(times)), key=lambda i: abs(times[i] - ts_ms))
return float(last["close"].iloc[idx])
p0 = nearest_price(oi[0]["ts"])
p1 = nearest_price(oi[-1]["ts"])
oi_usd_first = oi_first * p0
oi_usd_last = oi_last * p1
if oi_usd_first > 0:
oi_usd_change_pct = (oi_usd_last - oi_usd_first) / oi_usd_first * 100
except Exception:
pass
return {
"hit": True,
"symbol": symbol,
"avg_daily_vol": avg_daily_vol,
"oi_first": oi_first,
"oi_last": oi_last,
"oi_change_pct": oi_change_pct,
"price_first": price_first,
"price_last": price_last,
"price_change_pct": price_change_pct,
"oi_usd_change_pct": oi_usd_change_pct,
}
def analyze_symbol_raw(symbol: str) -> Dict[str, Any]:
df = fetch_ohlc(symbol, limit=N_DAYS + 20)
if df.empty:
return {"ok": False, "reason": "candles", "symbol": symbol}
last = df.tail(N_DAYS).copy()
if len(last) < 2:
return {"ok": False, "reason": "few_days", "symbol": symbol}
usd_vol = (last["volume"] * last["close"]).astype("float64")
avg_daily_vol = float(usd_vol.mean())
try:
price_first = float(last["close"].iloc[0])
price_last = float(last["close"].iloc[-1])
if price_first <= 0:
return {"ok": False, "reason": "bad_close", "symbol": symbol}
price_change_pct = (price_last - price_first) / price_first * 100
except Exception as e:
LOG.warning(f"[RAW] Price change calc error {symbol}: {e}")
return {"ok": False, "reason": "price_calc", "symbol": symbol}
oi_days = min(N_DAYS, OI_DAYS)
oi = fetch_binance_oi_history(symbol, oi_days)
if len(oi) < 2:
return {"ok": False, "reason": "oi_short", "symbol": symbol}
oi_first = oi[0]["oi"]
oi_last = oi[-1]["oi"]
if oi_first <= 0:
return {"ok": False, "reason": "oi_zero", "symbol": symbol}
oi_change_pct = (oi_last - oi_first) / oi_first * 100
oi_usd_change_pct = None
try:
times = last["time"].astype("int64").tolist()
def nearest_price(ts_ms: int) -> float:
idx = min(range(len(times)), key=lambda i: abs(times[i] - ts_ms))
return float(last["close"].iloc[idx])
price_for_oi_first = nearest_price(oi[0]["ts"])
price_for_oi_last = nearest_price(oi[-1]["ts"])
oi_usd_first = oi_first * price_for_oi_first
oi_usd_last = oi_last * price_for_oi_last
if oi_usd_first > 0:
oi_usd_change_pct = (oi_usd_last - oi_usd_first) / oi_usd_first * 100
except Exception as e:
LOG.warning(f"[RAW] OI USD change calc error {symbol}: {e}")
oi_usd_change_pct = None
return {
"ok": True,
"symbol": symbol,
"avg_daily_vol": avg_daily_vol,
"oi_first": oi_first,
"oi_last": oi_last,
"oi_change_pct": oi_change_pct,
"price_first": price_first,
"price_last": price_last,
"price_change_pct": price_change_pct,
"oi_usd_change_pct": oi_usd_change_pct,
}
async def collect_hits(progress_cb=None) -> List[Dict[str, Any]]:
syms = get_top_usdt_futures(MAX_MARKETS)
if progress_cb:
await progress_cb(f"⏳ Scanning {len(syms)} futures…")
loop = asyncio.get_running_loop()
tasks = [loop.run_in_executor(None, analyze_symbol, s) for s in syms]
hits: List[Dict[str, Any]] = []
processed = 0
for coro in asyncio.as_completed(tasks):
res = await coro
processed += 1
if res.get("hit"):
hits.append(res)
if progress_cb and processed % 10 == 0:
await progress_cb(
f"⏳ Processed {processed}/{len(syms)} — hits: {len(hits)}"
)
return hits
def sort_hits(hits: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
def sort_key(h: Dict[str, Any]):
if SORT_MODE == "oi_contracts":
return (h["oi_change_pct"], h["avg_daily_vol"])
elif SORT_MODE == "oi_usd":
v = h.get("oi_usd_change_pct")
if v is None:
v = -1e12
return (v, h["avg_daily_vol"])
elif SORT_MODE == "price":
return (h["price_change_pct"], h["avg_daily_vol"])
elif SORT_MODE == "volume":
return (h["avg_daily_vol"], h["oi_change_pct"])
else:
return (h["oi_change_pct"], h["avg_daily_vol"])
return sorted(hits, key=sort_key, reverse=True)
def build_text_from_hits(hits: List[Dict[str, Any]]) -> str:
oi_days = min(N_DAYS, OI_DAYS)
sort_label = SORT_MODE_LABELS.get(SORT_MODE, "by OI growth in contracts")
header = (
"📊 <b>Binance OI Scanner</b>\n\n"
f"Filters (global scan):\n"
f"• OI (contracts) growth ≥ {OI_GROWTH_PCT}% over last {oi_days} days\n"
f"• Price growth ≤ {PRICE_MAX_PCT_UP}% over last {N_DAYS} days\n"
f"• Average daily volume ≥ {MIN_DAILY_VOL_USD / 1_000_000:.1f}M USD\n"
f"• Sorting: <b>{html.escape(sort_label)}</b>\n\n"
)
if not hits:
return header + "No matches found."
hits_sorted = sort_hits(hits)
out: List[str] = [header]
for h in hits_sorted:
out.append(format_symbol_block(h, oi_days))
return "\n".join(out)
async def build_report(progress_cb):
hits = await collect_hits(progress_cb)
return build_text_from_hits(hits)
def keyboard(chat_id: int | None = None) -> ReplyKeyboardMarkup:
if chat_id is not None and chat_id in SUBSCRIBERS:
autoscan_label = "🟢 Autoscan"
else:
autoscan_label = "🔴 Autoscan"
return ReplyKeyboardMarkup(
[
[KeyboardButton("Scan")],
[KeyboardButton(autoscan_label)],
[KeyboardButton("Sort OI contracts"), KeyboardButton("Sort OI USD")],
[KeyboardButton("Sort price"), KeyboardButton("Sort volume")],
],
resize_keyboard=True,
)
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
sort_label = SORT_MODE_LABELS.get(SORT_MODE, "by OI growth in contracts")
chat_id = update.effective_chat.id
await update.message.reply_text(
"Bot is ready.\n"
"• Buttons: global scan / autoscan (single ON/OFF toggle) / sorting.\n"
"• Send a ticker (ETH, BTC, DOT, etc.) to get raw OI & price data.\n"
f"Current sorting: {sort_label}.",
reply_markup=keyboard(chat_id),
disable_web_page_preview=True,
)
async def cmd_scan(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = await update.message.reply_text(
"⏳ Running scan…", disable_web_page_preview=True
)
async def progress(t: str):
try:
await msg.edit_text(t, parse_mode="HTML", disable_web_page_preview=True)
except BadRequest:
pass
except Exception:
pass
text = await build_report(progress)
for chunk in chunk_html(text):
try:
await update.message.reply_html(chunk, disable_web_page_preview=True)
except TimedOut:
LOG.warning("Timed out while sending scan result chunk, retrying once...")
try:
await update.message.reply_html(chunk, disable_web_page_preview=True)
except TimedOut:
LOG.error(
"Timed out again while sending scan result chunk, "
"giving up on this chunk."
)
break
except BadRequest as e:
LOG.warning(f"BadRequest while sending scan result chunk: {e}")
continue
except Exception as e:
LOG.error(f"Unexpected error while sending scan result chunk: {e}")
continue
async def toggle_autoscan(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
if chat_id in SUBSCRIBERS:
SUBSCRIBERS.discard(chat_id)
state_text = "🔴 Autoscan: OFF"
else:
SUBSCRIBERS.add(chat_id)
state_text = "🟢 Autoscan: ON (H1)"
await update.message.reply_text(
state_text,
reply_markup=keyboard(chat_id),
disable_web_page_preview=True,
)
async def start_autoscan(update: Update, context: ContextTypes.DEFAULT_TYPE):
return await toggle_autoscan(update, context)
async def stop_autoscan(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
SUBSCRIBERS.discard(chat_id)
await update.message.reply_text(
"🔴 Autoscan: OFF",
reply_markup=keyboard(chat_id),
disable_web_page_preview=True,
)
async def cmd_oireset(update: Update, context: ContextTypes.DEFAULT_TYPE):
SEEN_HITS.clear()
await update.message.reply_text(
"♻️ OI signal memory cleared. New signals will be sent again.",
disable_web_page_preview=True,
)
async def _delete_prev_status_if_any(
context: ContextTypes.DEFAULT_TYPE,
chat_id: int,
):
msg_id = LAST_AUTOSCAN_STATUS_MSG.get(chat_id)
if not msg_id:
return
try:
await context.bot.delete_message(chat_id=chat_id, message_id=msg_id)
except BadRequest as e:
LOG.info(f"Cannot delete previous autoscan status msg in {chat_id}: {e}")
except Exception as e:
LOG.info(f"Error deleting previous autoscan status msg in {chat_id}: {e}")
finally:
LAST_AUTOSCAN_STATUS_MSG.pop(chat_id, None)
async def periodic_scan(context: ContextTypes.DEFAULT_TYPE):
if not SUBSCRIBERS:
return
hits = await collect_hits(progress_cb=None)
oi_days = min(N_DAYS, OI_DAYS)
new_hits = [h for h in hits if h["symbol"] not in SEEN_HITS]
if new_hits:
for h in new_hits:
SEEN_HITS.add(h["symbol"])
new_hits_sorted = sort_hits(new_hits)
lines: List[str] = []
for h in new_hits_sorted:
lines.append(format_symbol_block(h, oi_days))
body = "\n".join(lines)
text = (
"🟢 Autoscan (H1) — NEW signals\n"
"━━━━━━━━━━━━━━━━━━━━\n\n"
+ body
)
for chat_id in list(SUBSCRIBERS):
await _delete_prev_status_if_any(context, chat_id)
for chunk in chunk_html(text):
try:
await context.bot.send_message(
chat_id=chat_id,
text=chunk,
parse_mode="HTML",
disable_web_page_preview=True,
)
except TimedOut:
LOG.warning(
f"Timed out while sending autoscan chunk to {chat_id}, "
f"retrying once..."
)
try:
await context.bot.send_message(
chat_id=chat_id,
text=chunk,
parse_mode="HTML",
disable_web_page_preview=True,
)
except TimedOut:
LOG.error(
f"Timed out again while sending autoscan chunk to {chat_id}, "
f"skipping this chunk."
)
break
except BadRequest as e:
LOG.warning(
f"BadRequest while sending autoscan chunk to {chat_id}: {e}"
)
break
except Exception as e:
LOG.error(
f"Unexpected error while sending autoscan chunk to {chat_id}: {e}"
)
break
return
status_text = (
"🟢 Autoscan (H1) — running, no new signals\n"
"━━━━━━━━━━━━━━━━━━━━"
)
for chat_id in list(SUBSCRIBERS):
await _delete_prev_status_if_any(context, chat_id)
try:
m = await context.bot.send_message(
chat_id=chat_id,
text=status_text,
parse_mode="HTML",
disable_web_page_preview=True,
)
LAST_AUTOSCAN_STATUS_MSG[chat_id] = m.message_id
except TimedOut:
LOG.warning(
f"Timed out while sending autoscan status to {chat_id}, retrying once..."
)
try:
m = await context.bot.send_message(
chat_id=chat_id,
text=status_text,
parse_mode="HTML",
disable_web_page_preview=True,
)
LAST_AUTOSCAN_STATUS_MSG[chat_id] = m.message_id
except Exception as e:
LOG.error(f"Failed to send autoscan status to {chat_id}: {e}")
except Exception as e:
LOG.error(f"Unexpected error while sending autoscan status to {chat_id}: {e}")
def normalize_symbol_text(user_text: str) -> str:
t = user_text.strip().upper().replace(" ", "")
if "/" in t:
return t
if t.endswith("USDT"):
base = t[:-4]
return f"{base}/USDT"
return f"{t}/USDT"
async def handle_manual_symbol(update: Update, context: ContextTypes.DEFAULT_TYPE):
raw = update.message.text.strip()
symbol = normalize_symbol_text(raw)
await update.message.reply_text(
f"⏳ Checking {symbol} (raw, no filters)…",
disable_web_page_preview=True,
)
loop = asyncio.get_running_loop()
res = await loop.run_in_executor(None, analyze_symbol_raw, symbol)
if not res.get("ok"):
reason = res.get("reason", "unknown")
reason_map = {
"candles": "no candle data available.",
"few_days": "not enough days of data.",
"bad_close": "invalid close price data.",
"price_calc": "error while calculating price change.",
"oi_short": "not enough OI history.",
"oi_zero": "zero initial OI value.",
}
text = reason_map.get(
reason,
f"failed to compute metrics (reason={reason}).",
)
await update.message.reply_text(
f"{symbol}: {text}",
disable_web_page_preview=True,
)
return
oi_days = min(N_DAYS, OI_DAYS)
block = format_symbol_block(res, oi_days, include_header=True)
await update.message.reply_html(block, disable_web_page_preview=True)
async def handle_sort_button(update: Update, txt: str):
global SORT_MODE
txt = txt.lower()
if txt == "sort oi contracts":
SORT_MODE = "oi_contracts"
elif txt == "sort oi usd":
SORT_MODE = "oi_usd"
elif txt == "sort price":
SORT_MODE = "price"
elif txt == "sort volume":
SORT_MODE = "volume"
else:
return
sort_label = SORT_MODE_LABELS.get(SORT_MODE, "by OI growth in contracts")
await update.message.reply_text(
f"Sorting mode changed to: {sort_label}.",
disable_web_page_preview=True,
)
async def on_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
txt_raw = update.message.text or ""
txt = txt_raw.strip().lower()
if txt == "scan":
return await cmd_scan(update, context)
if "autoscan" in txt:
return await toggle_autoscan(update, context)
if txt.startswith("sort "):
return await handle_sort_button(update, txt)
# Otherwise treat it as a ticker
return await handle_manual_symbol(update, context)
def main():
from datetime import datetime, timedelta
if not TG_TOKEN:
LOG.error(
"TG_TOKEN environment variable is not set. "
"Please export TG_TOKEN before running the bot."
)
raise SystemExit(1)
def seconds_to_next_hour() -> int:
now = datetime.utcnow()
next_hour = (now + timedelta(hours=1)).replace(
minute=0, second=0, microsecond=0
)
return int((next_hour - now).total_seconds())
builder = Application.builder().token(TG_TOKEN)
if PROXY_URL:
builder.proxy(PROXY_URL)
app = builder.build()
app.add_handler(CommandHandler("start", cmd_start))
app.add_handler(CommandHandler("scan", cmd_scan))
app.add_handler(CommandHandler("autostart", start_autoscan))
app.add_handler(CommandHandler("stop", stop_autoscan))
app.add_handler(CommandHandler("stopscan", stop_autoscan))
app.add_handler(CommandHandler("autoscan", start_autoscan))
app.add_handler(CommandHandler("oireset", cmd_oireset))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, on_message))
app.job_queue.run_repeating(
periodic_scan,
interval=3600,
first=seconds_to_next_hour(),
)
LOG.info("BOT STARTED")
app.run_polling(drop_pending_updates=True)
if __name__ == "__main__":
main()