# 🚀 Deployment Guide

**Production deployment options for your trading bot**

## 🎯 Quick Production Start

```bash
python trading_bot.py
```
This single command handles everything:
- ✅ Configuration validation
- ✅ Auto-restart on errors
- ✅ Error notifications to your phone
- ✅ Comprehensive logging
- ✅ Graceful shutdown

## 🖥️ Deployment Options

### **Option 1: Screen/Tmux (Simplest)**
```bash
# Start a detachable session
screen -S trading-bot

# Run the bot
source venv/bin/activate
python trading_bot.py

# Detach: Ctrl+A, then D
# Reattach: screen -r trading-bot
# Stop: screen -r trading-bot, then Ctrl+C
```

### **Option 2: Systemd Service (Linux)**
Create `/etc/systemd/system/trading-bot.service`:
```ini
[Unit]
Description=Hyperliquid Trading Bot
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/ManualTrader
Environment=PATH=/path/to/ManualTrader/venv/bin
ExecStart=/path/to/ManualTrader/venv/bin/python trading_bot.py
Restart=always
RestartSec=30
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
```

```bash
# Enable and start service
sudo systemctl enable trading-bot
sudo systemctl start trading-bot

# Check status
sudo systemctl status trading-bot

# View logs
sudo journalctl -u trading-bot -f
```

### **Option 3: Docker**
```dockerfile
FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
CMD ["python", "trading_bot.py"]
```

```bash
# Build and run
docker build -t trading-bot .
docker run -d --name trading-bot \
  --env-file .env \
  --restart unless-stopped \
  trading-bot
```

### **Option 4: PM2 (Node.js Process Manager)**
```bash
npm install -g pm2

# Start bot with PM2
pm2 start trading_bot.py --interpreter python3 --name "trading-bot"

# Save configuration
pm2 startup
pm2 save

# Monitor
pm2 status
pm2 logs trading-bot
```

## 🛡️ Production Safety

### **Built-in Safety Features**
- **Auto-restart** - Up to 10 attempts with exponential backoff
- **Error notifications** - Sent to your Telegram immediately
- **Data persistence** - Statistics survive crashes and restarts
- **Comprehensive logging** - Daily log files with error tracking

### **Additional Monitoring**
```bash
# Check if bot is running
ps aux | grep "python.*trading_bot"

# Monitor logs in real-time
tail -f logs/trading_bot_$(date +%Y%m%d).log

# Check error log
tail -f logs/bot_errors.log

# View bot status via Telegram
# Send /start to your bot, it shows operational status
```

### **Resource Monitoring**
```bash
# Monitor system resources
htop

# Check disk space (logs can grow)
df -h

# Monitor bot memory usage
ps -p $(pgrep -f trading_bot.py) -o pid,vsz,rss,pcpu,pmem,comm
```

## 🔄 Safe Update Process

### **1. Backup Current State**
```bash
# Backup your configuration and data
cp .env .env.backup
cp trading_stats.json trading_stats.backup
```

### **2. Stop Bot Gracefully**
```bash
# For screen/tmux: Ctrl+C
# For systemd:
sudo systemctl stop trading-bot

# For Docker:
docker stop trading-bot

# For PM2:
pm2 stop trading-bot
```

### **3. Update Code**
```bash
git pull origin main
# or download new version
```

### **4. Test Before Restart**
```bash
# Validate configuration
python -c "import sys; sys.path.insert(0, 'src'); from config import Config; Config.validate()"

# Quick test (optional)
python utils/simple_bot.py
```

### **5. Restart Production**
```bash
# Screen/tmux: screen -S trading-bot, then python trading_bot.py
# Systemd: sudo systemctl start trading-bot
# Docker: docker start trading-bot
# PM2: pm2 start trading-bot
```

## ⚙️ Environment-Specific Configuration

### **Development**
```env
HYPERLIQUID_TESTNET=true
LOG_LEVEL=DEBUG
```

### **Staging**
```env
HYPERLIQUID_TESTNET=true
LOG_LEVEL=INFO
```

### **Production**
```env
HYPERLIQUID_TESTNET=false  # REAL MONEY!
LOG_LEVEL=INFO
```

## 📊 Performance Tuning

### **Log Management**
```bash
# Rotate logs (optional - bot creates daily files)
find logs/ -name "*.log" -mtime +30 -delete

# Compress old logs
gzip logs/trading_bot_$(date -d '1 day ago' +%Y%m%d).log
```

### **Resource Limits**
The bot is lightweight but you can set limits:
```bash
# Systemd service limits
[Service]
MemoryLimit=512M
CPUQuota=50%
```

## 🔍 Troubleshooting Production Issues

### **Bot Not Starting**
```bash
# Check configuration
python -c "import sys; sys.path.insert(0, 'src'); from config import Config; Config.validate()"

# Check dependencies
pip list | grep -E "hyperliquid|telegram"

# Check permissions
ls -la trading_bot.py
```

### **Bot Crashes Repeatedly**
```bash
# Check error log
tail -20 logs/bot_errors.log

# Check system logs (systemd)
sudo journalctl -u trading-bot --since "1 hour ago"

# Test in foreground
python trading_bot.py
```

### **Telegram Not Working**
```bash
# Test bot token
curl "https://api.telegram.org/bot<YOUR_TOKEN>/getMe"

# Re-verify Chat ID
python utils/get_telegram_chat_id.py
```

## 📈 Best Practices

### **Security**
- Keep `.env` file permissions restricted: `chmod 600 .env`
- Regularly rotate API keys
- Monitor unusual trading activity
- Use testnet for development

### **Monitoring**
- Set up alerts for bot downtime
- Monitor trading performance via `/stats`
- Check logs regularly
- Keep system updated

### **Data Protection**
- Backup `trading_stats.json` regularly
- Monitor disk space for logs
- Keep multiple backups of configuration

## 🎯 Quick Commands

```bash
# Check bot status
ps aux | grep trading_bot

# View real-time logs
tail -f logs/trading_bot_$(date +%Y%m%d).log

# Stop bot safely (screen/tmux)
screen -r trading-bot
# Then Ctrl+C

# Restart bot
python trading_bot.py

# Check bot health via Telegram
# Send /start to your bot
```

**Your bot is now production-ready with professional deployment! 🚀🛡️**