trading-algo/monitor.py
Gal Podlipnik 761e595389 docker
2025-07-17 02:43:19 +02:00

113 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""
Trading System Monitor
Real-time monitoring for Docker deployment.
"""
import time
import os
from datetime import datetime, timedelta
def monitor_system():
"""Monitor the trading system performance"""
print("🔍 Trading System Monitor - Starting...")
print("=" * 60)
while True:
try:
current_time = datetime.now()
print(f"\n📊 System Status - {current_time.strftime('%Y-%m-%d %H:%M:%S')}")
print("-" * 40)
# Check health file
health_status = "❌ UNKNOWN"
if os.path.exists('/tmp/health'):
try:
with open('/tmp/health', 'r') as f:
health_data = f.read().strip()
if health_data.startswith('OK:'):
parts = health_data.split(':')
health_time = datetime.fromisoformat(parts[1])
price = parts[2] if len(parts) > 2 else "N/A"
# Check if health is recent (within 10 minutes)
if current_time - health_time < timedelta(minutes=10):
health_status = f"✅ HEALTHY (Price: ${price})"
else:
health_status = f"⚠️ STALE ({(current_time - health_time).seconds//60}m ago)"
else:
health_status = "❌ ERROR"
except Exception as e:
health_status = f"❌ ERROR: {e}"
print(f"Health Status: {health_status}")
# Check log files
log_files = {
'trading.log': 'General',
'trades.log': 'Trades',
'risk.log': 'Risk Events',
'performance.log': 'Performance'
}
for log_file, description in log_files.items():
log_path = f'/app/logs/{log_file}'
if os.path.exists(log_path):
try:
stat = os.stat(log_path)
size_mb = stat.st_size / (1024 * 1024)
mod_time = datetime.fromtimestamp(stat.st_mtime)
age_minutes = (current_time - mod_time).seconds // 60
print(f"{description:12}: {size_mb:.1f}MB, {age_minutes}m ago")
except Exception as e:
print(f"{description:12}: Error - {e}")
else:
print(f"{description:12}: Not found")
# Show recent performance if available
perf_file = '/app/logs/performance.log'
if os.path.exists(perf_file):
try:
with open(perf_file, 'r') as f:
lines = f.readlines()
if lines:
recent_lines = lines[-3:] # Last 3 entries
print("\n📈 Recent Performance:")
for line in recent_lines:
if line.strip():
print(f" {line.strip()}")
except Exception as e:
print(f"Performance read error: {e}")
# Show recent trades if available
trades_file = '/app/logs/trades.log'
if os.path.exists(trades_file):
try:
with open(trades_file, 'r') as f:
lines = f.readlines()
if lines:
recent_trades = [line for line in lines[-5:] if 'BUY' in line or 'SELL' in line]
if recent_trades:
print("\n💰 Recent Trades:")
for trade in recent_trades[-2:]: # Last 2 trades
print(f" {trade.strip()}")
except Exception as e:
print(f"Trades read error: {e}")
print("\n" + "=" * 60)
# Wait 5 minutes before next check
time.sleep(300)
except KeyboardInterrupt:
print("\n👋 Monitor stopped by user")
break
except Exception as e:
print(f"❌ Monitor error: {e}")
time.sleep(60) # Wait 1 minute on error
if __name__ == "__main__":
monitor_system()