Add Telegram Report Integration: 3-hourly performance reports sent to Telegram

This commit is contained in:
Marc Blatter 2026-07-04 20:32:24 +02:00
parent 336542728e
commit 0c4086f243
2 changed files with 52 additions and 41 deletions

3
run_report.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
cd /home/marc/bot-deploy
python3 src/report_generator.py | hermes send-message telegram --message-file /dev/stdin

View File

@ -1,50 +1,58 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, json, logging, requests import os, json, subprocess
from datetime import datetime from datetime import datetime
from binance.client import Client from binance.client import Client
logging.basicConfig(level=logging.INFO) with open('/home/marc/bot-deploy/.env') as f:
logger = logging.getLogger(__name__)
env = {} env = {}
with open("/home/marc/bot-deploy/.env") as f:
for line in f: for line in f:
k, _, v = line.partition("=") k, _, v = line.partition('=')
if k and v:
env[k.strip()] = v.strip() env[k.strip()] = v.strip()
def get_bot_state(): # Load bot state
try: with open('/home/marc/bot-deploy/trades.json') as f:
r = requests.get("http://localhost:7000/api/state", timeout=5) bot_state = json.load(f)
return r.json()
except:
return {}
def generate_report(): # Get balance from Binance
state = get_bot_state() c = Client(env.get('BINANCE_API_KEY_LIVE'), env.get('BINANCE_API_SECRET_LIVE'))
if not state: acc = c.get_account()
return None balance = {a['asset']: float(a['free']) for a in acc['balances']}
completed = state.get("completed_trades", []) # Calculate metrics
if not completed: portfolio_value = balance.get('USDT', 0)
return "📊 No trades yet today" for asset in ['ETH', 'BTC', 'SOL', 'BNB', 'XRP']:
if asset in balance:
# Rough values (should use ticker for precision)
prices = {'ETH': 1790, 'BTC': 63000, 'SOL': 83.5, 'BNB': 578, 'XRP': 2.5}
portfolio_value += balance.get(asset, 0) * prices.get(asset, 0)
total = len(completed) completed = bot_state.get('completed', [])
wins = len([t for t in completed if t.get("profit_usd", 0) > 0]) daily_pnl = sum(t.get('profit_usd', 0) for t in completed)
losses = total - wins wins = len([t for t in completed if t.get('profit_usd', 0) > 0])
profit = sum(t.get("profit_usd", 0) for t in completed) losses = len([t for t in completed if t.get('profit_usd', 0) < 0])
wr = (wins/total*100) if total > 0 else 0
report = f"""📊 **Performance Report** - {datetime.now().strftime("%H:%M")} # Format report
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M UTC')
report = f'''📊 **TRADING BOT REPORT** — {timestamp}
🎯 Trades: {total} | {wins} ({wr:.0f}%) | {losses} 💰 **PORTFOLIO**
💰 P&L: ${profit:.2f} | Avg: ${profit/total:.2f} Total: ${portfolio_value:.2f}
💵 USDT: ${state.get("balance", {}).get("USDT", {}).get("free", 0):.2f}""" USDT Free: ${balance.get('USDT', 0):.2f}
Open Trades: {len(bot_state.get('current', {}))}
return report 📈 **TODAY'S PERFORMANCE**
Trades: {len(completed)}
Wins: {wins} | Losses: {losses}
Win Rate: {(wins/(wins+losses)*100) if (wins+losses) > 0 else 0:.1f}%
Daily P&L: ${daily_pnl:.2f}
if __name__ == "__main__": 🟢 **BOT STATUS**: OPERATIONAL
report = generate_report() 🔗 Dashboard: https://bot.bizmark.cloud
if report:
---
*Next report in 3 hours*
'''
# Send via Telegram using Hermes send_message
print(report) print(report)
logger.info("✅ Report generated")