Repository navigation
Expand file tree
/
Copy pathbybit_bot.py
More file actions
265 lines (219 loc) · 9.5 KB
/
Copy pathbybit_bot.py
File metadata and controls
265 lines (219 loc) · 9.5 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
#!/usr/bin/env python3
import asyncio
import json
import logging
import os
import signal
import sys
import websockets
import aiohttp
from datetime import datetime
from telegram import Bot
from telegram.error import TelegramError
from dotenv import load_dotenv
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class BybitLiquidationBot:
def __init__(self):
load_dotenv()
self.bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
self.chat_id = os.getenv('TELEGRAM_CHAT_ID')
self.min_size = float(os.getenv('MIN_LIQUIDATION_SIZE', '1000'))
if not self.bot_token or not self.chat_id:
logger.error("Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID environment variables")
sys.exit(1)
self.telegram_bot = Bot(token=self.bot_token)
self.ws = None
self.running = False
self.bybit_ws_url = "wss://stream.bybit.com/v5/public/linear"
self.bybit_api_url = "https://api.bybit.com/v5/market/instruments-info"
async def send_telegram_message(self, message):
try:
await self.telegram_bot.send_message(
chat_id=self.chat_id,
text=message,
parse_mode='HTML'
)
logger.info("Message sent to Telegram")
except TelegramError as e:
logger.error(f"Failed to send Telegram message: {e}")
async def send_startup_message(self):
symbols = await self.get_all_futures_symbols()
symbol_count = len(symbols)
message = f"""
🤖 <b>Bybit Liquidation Bot Started</b>
✅ Monitoring {symbol_count} futures symbols
📊 Minimum size: ${self.min_size:,.0f}
⚡ Real-time monitoring
📈 Covers: Linear futures
Bot is now running...
""".strip()
await self.send_telegram_message(message)
async def format_liquidation_alert(self, data):
symbol = data.get('symbol', 'Unknown')
side = data.get('side', 'Unknown')
size = data.get('size', '0')
price = data.get('price', '0')
amount = data.get('amount', '0')
time = data.get('time', 'Unknown')
side_emoji = "🔴" if side == "Sell" else "🟢"
message = f"""
🚨 <b>LIQUIDATION ALERT</b> 🚨
{side_emoji} <b>{symbol}</b> {side} Liquidation
💰 Size: {size}
💸 Amount: ${amount}
💵 Price: ${price}
⏰ Time: {time}
#Liquidation #{symbol.replace('USDT', '')}
""".strip()
return message
async def get_all_futures_symbols(self):
try:
symbols = []
categories = ['linear']
async with aiohttp.ClientSession() as session:
for category in categories:
params = {
'category': category,
'limit': 1000
}
async with session.get(self.bybit_api_url, params=params) as response:
if response.status == 200:
data = await response.json()
if data.get('retCode') == 0:
for item in data.get('result', {}).get('list', []):
symbol = item.get('symbol')
if symbol:
symbols.append(f"allLiquidation.{symbol}")
logger.info(f"Found {len([s for s in symbols if category in s])} {category} symbols")
else:
logger.error(f"API error for {category}: {data.get('retMsg')}")
else:
logger.error(f"HTTP error for {category}: {response.status}")
logger.info(f"Total symbols to monitor: {len(symbols)}")
return symbols
except Exception as e:
logger.error(f"Failed to fetch symbols: {e}")
fallback_symbols = [
"allLiquidation.BTCUSDT", "allLiquidation.ETHUSDT", "allLiquidation.ADAUSDT",
"allLiquidation.BNBUSDT", "allLiquidation.SOLUSDT", "allLiquidation.XRPUSDT",
"allLiquidation.DOTUSDT", "allLiquidation.DOGEUSDT", "allLiquidation.AVAXUSDT",
"allLiquidation.MATICUSDT"
]
logger.info(f"Using fallback symbols: {len(fallback_symbols)}")
return fallback_symbols
async def connect_to_bybit(self):
try:
self.ws = await websockets.connect(self.bybit_ws_url)
logger.info("Connected to Bybit WebSocket")
symbols = await self.get_all_futures_symbols()
if symbols:
subscribe_msg = {"op": "subscribe", "args": symbols}
await self.ws.send(json.dumps(subscribe_msg))
logger.info(f"Subscribed to {len(symbols)} liquidation topics")
else:
logger.error("No symbols found to subscribe to")
return False
return True
except Exception as e:
logger.error(f"Connection failed: {e}")
await self.send_telegram_message(f"❌ Connection failed: {str(e)}")
return False
async def listen_for_liquidations(self):
if not self.ws:
return
try:
async for message in self.ws:
try:
data = json.loads(message)
if data.get('topic') and data.get('topic').startswith('allLiquidation'):
await self.process_liquidation(data.get('data', {}))
except json.JSONDecodeError as e:
logger.error(f"Failed to parse message: {e}")
except Exception as e:
logger.error(f"Error processing message: {e}")
except websockets.exceptions.ConnectionClosed:
logger.warning("WebSocket connection closed")
await self.send_telegram_message("⚠️ WebSocket connection lost")
except Exception as e:
logger.error(f"WebSocket error: {e}")
await self.send_telegram_message(f"❌ WebSocket error: {str(e)}")
async def process_liquidation(self, liquidation_data):
if not liquidation_data:
return
for liquidation in liquidation_data:
try:
symbol = liquidation.get('s', '')
side = liquidation.get('S', '')
size = float(liquidation.get('v', '0'))
price = liquidation.get('p', '0')
liquidation_value = size * float(price)
if liquidation_value >= self.min_size:
timestamp = liquidation.get('T', '')
if timestamp:
try:
dt = datetime.fromtimestamp(int(timestamp) / 1000)
formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S')
except:
formatted_time = timestamp
else:
formatted_time = "Unknown"
notification_data = {
'symbol': symbol,
'side': side,
'size': f"{size:,.2f}",
'price': f"{float(price):,.2f}",
'amount': f"{size * float(price):,.2f}",
'time': formatted_time
}
message = await self.format_liquidation_alert(notification_data)
await self.send_telegram_message(message)
logger.info(f"Alert sent: {symbol} {side} ${liquidation_value:,.2f}")
except Exception as e:
print(liquidation)
logger.error(f"Error processing liquidation: {e}")
async def start_monitoring(self):
self.running = True
logger.info("Starting Bybit Liquidation Bot...")
await self.send_startup_message()
while self.running:
try:
if await self.connect_to_bybit():
await self.listen_for_liquidations()
if self.running:
logger.info("Reconnecting in 5 seconds...")
await asyncio.sleep(5)
except KeyboardInterrupt:
logger.info("Received interrupt signal")
break
except Exception as e:
logger.error(f"Unexpected error: {e}")
if self.running:
await asyncio.sleep(5)
async def stop(self):
self.running = False
if self.ws:
await self.ws.close()
logger.info("Bot stopped")
async def main():
bot = BybitLiquidationBot()
try:
await bot.start_monitoring()
except KeyboardInterrupt:
logger.info("Bot interrupted by user")
finally:
await bot.stop()
def signal_handler(signum, frame):
logger.info(f"Received signal {signum}")
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
asyncio.run(main())
except Exception as e:
logger.error(f"Fatal error: {e}")
sys.exit(1)