-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtelegram_bot.py
More file actions
475 lines (374 loc) · 18 KB
/
telegram_bot.py
File metadata and controls
475 lines (374 loc) · 18 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
#!/usr/bin/env python3
"""
Telegram Bot for Binance Trading Bot Account Overview - Hourly Reports
"""
import os
import csv
import logging
import time
import requests
import pandas as pd
from datetime import datetime, timedelta
from typing import Dict, List
import pytz
from binance.um_futures import UMFutures
import dotenv
dotenv.load_dotenv()
# Configure logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
class BinanceAccountAnalyzer:
"""Analyze Binance account data and transaction logs"""
def __init__(self, api_key: str, api_secret: str):
self.client = UMFutures(api_key, api_secret)
def get_account_overview(self) -> Dict:
"""Get current account balance, unrealized PnL, positions"""
try:
account_positions = self.client.get_position_risk()
# Get current positions
positions = []
for position in account_positions:
if float(position['positionAmt']) != 0: # Only active positions
positions.append({
'symbol': position['symbol'],
'size': float(position['positionAmt']),
'unrealized_pnl': float(position['unRealizedProfit']),
'entry_price': float(position['entryPrice']),
'mark_price': float(position['markPrice']),
'liquidation_price': float(position['liquidationPrice'])
})
# Get account information
account_info = self.client.account()
# Get account balance
total_balance = 0
for asset in account_info['assets']:
if float(asset['walletBalance']) > 0:
total_balance += float(asset['walletBalance'])
total_balance += float(asset['unrealizedProfit'])
# Calculate total unrealized PnL
total_unrealized_pnl = sum(pos['unrealized_pnl'] for pos in positions)
return {
'total_balance': total_balance,
'total_unrealized_pnl': total_unrealized_pnl,
'positions': positions
}
except Exception as e:
logger.error(f"Error getting account overview: {e}")
return {
'total_balance': 0,
'total_unrealized_pnl': 0,
'positions': []
}
class TransactionLogAnalyzer:
"""Analyze transaction log CSV files using pandas"""
def __init__(self):
# Get timezone from environment variable, default to UTC
timezone_name = os.getenv('TIMEZONE', 'UTC')
self.timezone = pytz.timezone(timezone_name)
def _load_transaction_df(self, symbol: str) -> pd.DataFrame:
"""Load transaction log CSV as pandas DataFrame"""
csv_file = f"{symbol}_transactions_log.csv"
if not os.path.exists(csv_file):
return pd.DataFrame()
try:
# Read CSV with pandas
df = pd.read_csv(csv_file)
# Ensure we have the required columns
if len(df.columns) < 8:
logger.warning(f"CSV file {csv_file} has insufficient columns")
return pd.DataFrame()
# Rename columns for clarity (assuming format: datetime, symbol, orderid, order_type, counter_order, price, amount, status)
df.columns = ['datetime', 'symbol', 'orderid', 'order_type', 'counter_order', 'price', 'amount', 'status']
# Convert datetime column
df['datetime'] = pd.to_datetime(df['datetime'], errors='coerce')
# Convert numeric columns
df['price'] = pd.to_numeric(df['price'], errors='coerce')
df['amount'] = pd.to_numeric(df['amount'], errors='coerce')
# Filter only FILLED transactions
df = df[df['status'] == 'FILLED'].copy()
return df
except Exception as e:
logger.error(f"Error loading transaction log {csv_file}: {e}")
return pd.DataFrame()
def get_last_trade_time(self, symbol: str) -> datetime:
"""Get the last trade time for a specific symbol using pandas"""
df = self._load_transaction_df(symbol)
if df.empty:
return None
# Get the latest datetime
last_trade_time = df['datetime'].max()
if pd.isna(last_trade_time):
return None
# Convert to timezone-aware datetime if needed
if last_trade_time.tz is None:
last_trade_time = self.timezone.localize(last_trade_time)
return last_trade_time
def analyze_transactions(self, symbol: str, time_period: timedelta) -> Dict:
"""Analyze transaction log for given symbol and time period using pandas"""
df = self._load_transaction_df(symbol)
if df.empty:
return {
'num_trades': 0,
'realized_pnl': 0.0,
'total_volume': 0.0
}
# Calculate cutoff time in timezone
now_with_timezone = datetime.now(self.timezone)
cutoff_time = now_with_timezone - time_period
# Filter transactions within time period
df_filtered = df[df['datetime'] >= cutoff_time].copy()
if df_filtered.empty:
return {
'num_trades': 0,
'realized_pnl': 0.0,
'total_volume': 0.0
}
# Calculate metrics
num_trades = len(df_filtered)
total_volume = (df_filtered['price'] * df_filtered['amount']).abs().sum()
# Calculate realized PnL (trades with counter_order)
realized_trades = df_filtered[df_filtered['counter_order'].notna() & (df_filtered['counter_order'] != '')]
realized_pnl = (realized_trades['amount'] * 0.5).sum() # Assuming 0.5 multiplier for realized trades
# Get last trade time
last_trade_time = df_filtered['datetime'].max()
if pd.isna(last_trade_time):
last_trade_time = None
return {
'num_trades': num_trades,
'realized_pnl': realized_pnl,
'total_volume': total_volume,
'last_trade_time': last_trade_time
}
def get_all_trading_symbols(self) -> List[str]:
"""Get all symbols that have transaction logs"""
symbols = []
for filename in os.listdir('.'):
if filename.endswith('_transactions_log.csv'):
symbol = filename.replace('_transactions_log.csv', '')
symbols.append(symbol)
return symbols
class TelegramBot:
"""Simple Telegram bot using REST API"""
def __init__(self, token: str):
self.token = token
self.base_url = f"https://api.telegram.org/bot{token}"
def send_message(self, chat_id: str, text: str, parse_mode: str = None) -> bool:
"""Send message using Telegram REST API"""
try:
url = f"{self.base_url}/sendMessage"
data = {
'chat_id': chat_id,
'text': text
}
if parse_mode:
data['parse_mode'] = parse_mode
response = requests.post(url, data=data, timeout=10)
response.raise_for_status()
result = response.json()
if result.get('ok'):
logger.info("Message sent successfully")
return True
else:
logger.error(f"Failed to send message: {result}")
return False
except Exception as e:
logger.error(f"Error sending Telegram message: {e}")
return False
class HourlyReporter:
"""Send hourly account overview reports via Telegram"""
def __init__(self, token: str, chat_id: str, api_key: str, api_secret: str):
self.token = token
self.chat_id = chat_id
self.bot = TelegramBot(token=token)
self.account_analyzer = BinanceAccountAnalyzer(api_key, api_secret)
self.transaction_analyzer = TransactionLogAnalyzer()
self.last_alert_time = {} # Track last alert time per symbol to avoid spam
def check_trading_activity(self):
"""Check trading activity for all symbols and send alerts if no recent trades"""
try:
logger.info("Checking trading activity...")
current_time = datetime.now()
symbols = self.transaction_analyzer.get_all_trading_symbols()
inactive_symbols = []
for symbol in symbols:
last_trade_time = self.transaction_analyzer.get_last_trade_time(symbol)
if last_trade_time is None:
# No trades ever recorded for this symbol
inactive_symbols.append(f"{symbol} (no trades recorded)")
continue
# Check if last trade was more than 10 minutes ago
time_since_last_trade = current_time - last_trade_time.replace(tzinfo=None)
if time_since_last_trade > timedelta(minutes=10):
# Check if we already sent an alert for this symbol recently (within last 30 minutes)
last_alert = self.last_alert_time.get(symbol)
if last_alert is None or (current_time - last_alert) > timedelta(minutes=30):
inactive_symbols.append(f"{symbol} (last trade: {last_trade_time.strftime('%H:%M:%S')})")
self.last_alert_time[symbol] = current_time
if inactive_symbols:
alert_message = self._format_trading_alert(inactive_symbols)
success = self.bot.send_message(
chat_id=self.chat_id,
text=alert_message,
parse_mode='Markdown'
)
if success:
logger.info(f"Trading activity alert sent for {len(inactive_symbols)} symbols")
else:
logger.error("Failed to send trading activity alert")
else:
logger.info("All symbols have recent trading activity")
except Exception as e:
logger.error(f"Error checking trading activity: {e}")
def _format_trading_alert(self, inactive_symbols: List[str]) -> str:
"""Format the trading activity alert message"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
response = f"⚠️ *Trading Activity Alert - {current_time}*\n\n"
response += f"🚨 *No trades detected in the last 10 minutes for:*\n\n"
for symbol_info in inactive_symbols:
response += f"• {symbol_info}\n"
response += f"\n⏰ Check your trading bots and ensure they are running properly."
return response
def send_hourly_report(self):
"""Send hourly account overview report"""
try:
logger.info("Generating hourly report...")
# Get account overview from Binance
account_data = self.account_analyzer.get_account_overview()
# Analyze transaction logs for all available symbols
transaction_data = {}
for filename in os.listdir('.'):
if filename.endswith('_transactions_log.csv'):
symbol = filename.replace('_transactions_log.csv', '')
# Analyze last 1 hour for hourly reports
transaction_data[symbol] = self.transaction_analyzer.analyze_transactions(
symbol, timedelta(hours=1)
)
# Get 24-hour totals for summary
transaction_data_24h = {}
for filename in os.listdir('.'):
if filename.endswith('_transactions_log.csv'):
symbol = filename.replace('_transactions_log.csv', '')
transaction_data_24h[symbol] = self.transaction_analyzer.analyze_transactions(
symbol, timedelta(hours=24)
)
# Format response message
response = self._format_hourly_report(account_data, transaction_data, transaction_data_24h)
# Send message using REST API
success = self.bot.send_message(
chat_id=self.chat_id,
text=response,
parse_mode='Markdown'
)
if success:
logger.info("Hourly report sent successfully")
else:
logger.error("Failed to send hourly report")
except Exception as e:
logger.error(f"Error sending hourly report: {e}")
def _format_hourly_report(self, account_data: Dict, transaction_data: Dict, transaction_data_24h: Dict) -> str:
"""Format the hourly report message"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
response = f"🕐 *Scalp Trading Hourly Report - {current_time}*\n\n"
# Account balance and PnL
response += f"💰 *Account Balance:* ${account_data['total_balance']:,.2f}\n"
response += f"📈 *Unrealized PnL:* ${account_data['total_unrealized_pnl']:,.2f}\n\n"
# Current positions
if account_data['positions']:
response += "📋 *Current Positions:*\n"
for pos in account_data['positions']:
response += f"{pos['symbol']}: {pos['size']:.4f} @ ${pos['entry_price']:,.2f}\n"
response += f" Mark: ${pos['mark_price']:,.2f} | PnL: ${pos['unrealized_pnl']:,.2f}\n\n"
else:
response += "📋 *Current Positions:* None\n\n"
# Trading activity (last 1 hour)
if transaction_data:
response += "📈 *Trading Activity (Last 1h):*\n"
total_trades = 0
total_realized_pnl = 0.0
total_volume = 0.0
for symbol, data in transaction_data.items():
if data['num_trades'] > 0:
total_trades += data['num_trades']
total_realized_pnl += data['realized_pnl']
total_volume += data['total_volume']
response += f"{symbol}:\n"
response += f" Trades: {data['num_trades']}\n"
response += f" Realized PnL: ${data['realized_pnl']:,.2f}\n"
response += f" Volume: ${data['total_volume']:,.2f}\n"
response += f" Last Trade: {data['last_trade_time'].strftime('%Y-%m-%d %H:%M:%S')}\n\n"
if total_trades > 0:
response += "📊 *Totals (1h):*\n"
response += f" Total Trades: {total_trades}\n"
response += f" Total Realized PnL: ${total_realized_pnl:,.2f}\n"
response += f" Total Volume: ${total_volume:,.2f}\n"
else:
response += "No trading activity in the last hour.\n"
# Add 24-hour totals
total_trades_24h = 0
total_realized_pnl_24h = 0.0
total_volume_24h = 0.0
for symbol, data in transaction_data_24h.items():
if data['num_trades'] > 0:
total_trades_24h += data['num_trades']
total_realized_pnl_24h += data['realized_pnl']
total_volume_24h += data['total_volume']
if total_trades_24h > 0:
response += "\n📊 *Totals (24h):*\n"
response += f" Total Trades: {total_trades_24h}\n"
response += f" Total Realized PnL: ${total_realized_pnl_24h:,.2f}\n"
response += f" Total Volume: ${total_volume_24h:,.2f}\n"
else:
response += "📈 *Trading Activity:* No transaction logs found.\n"
return response
def run_scheduler(self):
"""Run the hourly scheduler with 10-minute trading activity checks"""
logger.info("Scheduler started. Hourly reports at :00, trading activity checks every 10 minutes")
# Send initial report
try:
self.send_hourly_report()
except Exception as e:
logger.error(f"Error sending initial report: {e}")
# Initialize last activity check time
last_activity_check = datetime.now()
# Keep the scheduler running
while True:
current_time = datetime.now()
# Check if it's time for hourly report (at :00)
if current_time.minute == 0:
try:
self.send_hourly_report()
except Exception as e:
logger.error(f"Error sending hourly report: {e}")
# Check trading activity every 10 minutes
if (current_time - last_activity_check).total_seconds() >= 600: # 10 minutes
try:
self.check_trading_activity()
last_activity_check = current_time
except Exception as e:
logger.error(f"Error checking trading activity: {e}")
# Sleep for 1 minute before next check
time.sleep(60)
def main():
"""Main function"""
# Get environment variables
telegram_token = os.getenv('TELEGRAM_BOT_TOKEN')
telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID')
binance_api_key = os.getenv('API_KEY')
binance_api_secret = os.getenv('API_SECRET')
if not telegram_token:
logger.error("TELEGRAM_BOT_TOKEN environment variable not set")
return
if not telegram_chat_id:
logger.error("TELEGRAM_CHAT_ID environment variable not set")
return
if not binance_api_key or not binance_api_secret:
logger.error("API_KEY and API_SECRET environment variables not set")
return
reporter = HourlyReporter(telegram_token, telegram_chat_id, binance_api_key, binance_api_secret)
# Create and run hourly reporter
reporter.run_scheduler()
if __name__ == "__main__":
main()