Add Telegram Report Integration: 3-hourly performance reports sent to Telegram
This commit is contained in:
parent
336542728e
commit
0c4086f243
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
cd /home/marc/bot-deploy
|
||||
python3 src/report_generator.py | hermes send-message telegram --message-file /dev/stdin
|
||||
|
|
@ -1,50 +1,58 @@
|
|||
#!/usr/bin/env python3
|
||||
import os, json, logging, requests
|
||||
import os, json, subprocess
|
||||
from datetime import datetime
|
||||
from binance.client import Client
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
env = {}
|
||||
with open("/home/marc/bot-deploy/.env") as f:
|
||||
with open('/home/marc/bot-deploy/.env') as f:
|
||||
env = {}
|
||||
for line in f:
|
||||
k, _, v = line.partition("=")
|
||||
if k and v:
|
||||
env[k.strip()] = v.strip()
|
||||
k, _, v = line.partition('=')
|
||||
env[k.strip()] = v.strip()
|
||||
|
||||
def get_bot_state():
|
||||
try:
|
||||
r = requests.get("http://localhost:7000/api/state", timeout=5)
|
||||
return r.json()
|
||||
except:
|
||||
return {}
|
||||
# Load bot state
|
||||
with open('/home/marc/bot-deploy/trades.json') as f:
|
||||
bot_state = json.load(f)
|
||||
|
||||
def generate_report():
|
||||
state = get_bot_state()
|
||||
if not state:
|
||||
return None
|
||||
|
||||
completed = state.get("completed_trades", [])
|
||||
if not completed:
|
||||
return "📊 No trades yet today"
|
||||
|
||||
total = len(completed)
|
||||
wins = len([t for t in completed if t.get("profit_usd", 0) > 0])
|
||||
losses = total - wins
|
||||
profit = sum(t.get("profit_usd", 0) for t in completed)
|
||||
wr = (wins/total*100) if total > 0 else 0
|
||||
|
||||
report = f"""📊 **Performance Report** - {datetime.now().strftime("%H:%M")}
|
||||
# Get balance from Binance
|
||||
c = Client(env.get('BINANCE_API_KEY_LIVE'), env.get('BINANCE_API_SECRET_LIVE'))
|
||||
acc = c.get_account()
|
||||
balance = {a['asset']: float(a['free']) for a in acc['balances']}
|
||||
|
||||
🎯 Trades: {total} | ✅ {wins} ({wr:.0f}%) | ❌ {losses}
|
||||
💰 P&L: ${profit:.2f} | Avg: ${profit/total:.2f}
|
||||
💵 USDT: ${state.get("balance", {}).get("USDT", {}).get("free", 0):.2f}"""
|
||||
|
||||
return report
|
||||
# Calculate metrics
|
||||
portfolio_value = balance.get('USDT', 0)
|
||||
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)
|
||||
|
||||
completed = bot_state.get('completed', [])
|
||||
daily_pnl = sum(t.get('profit_usd', 0) for t in completed)
|
||||
wins = len([t for t in completed if t.get('profit_usd', 0) > 0])
|
||||
losses = len([t for t in completed if t.get('profit_usd', 0) < 0])
|
||||
|
||||
# Format report
|
||||
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M UTC')
|
||||
report = f'''📊 **TRADING BOT REPORT** — {timestamp}
|
||||
|
||||
💰 **PORTFOLIO**
|
||||
• Total: ${portfolio_value:.2f}
|
||||
• USDT Free: ${balance.get('USDT', 0):.2f}
|
||||
• Open Trades: {len(bot_state.get('current', {}))}
|
||||
|
||||
📈 **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}
|
||||
|
||||
🟢 **BOT STATUS**: OPERATIONAL
|
||||
🔗 Dashboard: https://bot.bizmark.cloud
|
||||
|
||||
---
|
||||
*Next report in 3 hours*
|
||||
'''
|
||||
|
||||
# Send via Telegram using Hermes send_message
|
||||
print(report)
|
||||
|
||||
if __name__ == "__main__":
|
||||
report = generate_report()
|
||||
if report:
|
||||
print(report)
|
||||
logger.info("✅ Report generated")
|
||||
|
|
|
|||
Loading…
Reference in New Issue