Files
trading_bot_v4/.env.example
mindesbunister 03e91fc18d feat: ATR-based trailing stop + rate limit monitoring
MAJOR FIXES:
- ATR-based trailing stop for runners (was fixed 0.3%, now adapts to volatility)
- Fixes runners with +7-9% MFE exiting for losses
- Typical improvement: 2.24x more room (0.3% → 0.67% at 0.45% ATR)
- Enhanced rate limit logging with database tracking
- New /api/analytics/rate-limits endpoint for monitoring

DETAILS:
- Position Manager: Calculate trailing as (atrAtEntry / price × 100) × multiplier
- Config: TRAILING_STOP_ATR_MULTIPLIER=1.5, MIN=0.25%, MAX=0.9%
- Settings UI: Added ATR multiplier controls
- Rate limits: Log hits/recoveries/exhaustions to SystemEvent table
- Documentation: ATR_TRAILING_STOP_FIX.md + RATE_LIMIT_MONITORING.md

IMPACT:
- Runners can now capture big moves (like morning's $172→$162 SOL drop)
- Rate limit visibility prevents silent failures
- Data-driven optimization for RPC endpoint health
2025-11-11 14:51:41 +01:00

164 lines
4.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Trading Bot v4 - Environment Variables Template
# Copy this file to .env.local and fill in your values
#
# IMPORTANT: Never commit .env.local to git!
# ================================
# REQUIRED - DRIFT PROTOCOL
# ================================
# Your Solana wallet private key (base58 format)
# Get from: Phantom → Settings → Export Private Key
DRIFT_WALLET_PRIVATE_KEY=your_base58_private_key_here
# Drift environment (mainnet-beta for production, devnet for testing)
DRIFT_ENV=mainnet-beta
# API secret key for authenticating n8n requests
# Generate with: openssl rand -hex 32
# ⚠️ MUST match API_SECRET_KEY in n8n environment variables
API_SECRET_KEY=your_random_secret_key_here
# ================================
# REQUIRED - SOLANA RPC ENDPOINT
# ================================
# Solana RPC URL (get free key at https://helius.dev)
# Helius free tier: 100,000 requests/day
SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_HELIUS_API_KEY
# Alternative RPC providers:
# QuickNode: https://solana-mainnet.quiknode.pro/YOUR_ENDPOINT/
# Alchemy: https://solana-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY
# Ankr: https://rpc.ankr.com/solana
# ================================
# REQUIRED - PYTH NETWORK (Price Feeds)
# ================================
# Pyth Hermes WebSocket endpoint
# FREE - No API key needed!
PYTH_HERMES_URL=https://hermes.pyth.network
# ================================
# TRADING CONFIGURATION
# ================================
# Position size in USD (default: 50 for testing)
# With 5x leverage: $50 position = $250 notional value
MAX_POSITION_SIZE_USD=50
# Leverage multiplier (1-20, default: 5)
LEVERAGE=5
# Risk parameters (as percentages)
# Stop Loss: Close 100% of position when price drops this much
STOP_LOSS_PERCENT=-1.5
# Take Profit 1: Close 50% of position at this profit level
TAKE_PROFIT_1_PERCENT=0.7
# Take Profit 2: Close remaining 50% at this profit level
TAKE_PROFIT_2_PERCENT=1.5
# Move SL to breakeven when profit reaches this level
BREAKEVEN_TRIGGER_PERCENT=0.4
# ATR-based Trailing Stop (for 25% runner after TP2)
# Trailing distance = (ATR × multiplier)
# Example: 0.5% ATR × 1.5 = 0.75% trailing (more room than fixed 0.3%)
TRAILING_STOP_ATR_MULTIPLIER=1.5
TRAILING_STOP_MIN_PERCENT=0.25
TRAILING_STOP_MAX_PERCENT=0.9
TRAILING_STOP_ACTIVATION=0.5
# Risk limits
# Stop trading if daily loss exceeds this amount (USD)
MAX_DAILY_DRAWDOWN=-50
# Maximum trades per hour (prevents overtrading)
MAX_TRADES_PER_HOUR=6
# Maximum acceptable slippage (percentage)
SLIPPAGE_TOLERANCE=1.0
# ================================
# OPTIONAL - DEVELOPMENT
# ================================
# Node environment
NODE_ENV=production
# Log level (debug, info, warn, error)
LOG_LEVEL=info
# Enable dry run mode (simulate trades without executing)
DRY_RUN=false
# API server port
PORT=3000
# ================================
# SETUP CHECKLIST
# ================================
# [ ] 1. Copy this file to .env.local
# [ ] 2. Get Solana wallet private key from Phantom
# [ ] 3. Get free Helius RPC key: https://helius.dev
# [ ] 4. Generate API_SECRET_KEY: openssl rand -hex 32
# [ ] 5. Set same API_SECRET_KEY in n8n environment variables
# [ ] 6. Set MAX_POSITION_SIZE_USD=50 for testing
# [ ] 7. Start bot: npm run dev (or use Docker)
# [ ] 8. Import n8n-workflow-simple.json to n8n
# [ ] 9. Configure TradingView alert webhook
# [ ] 10. Test with small position first!
# ================================
# EXPECTED RISK PER TRADE
# ================================
# With default settings:
# - Position Size: $50
# - Leverage: 5x
# - Notional Value: $250
# - Max Loss (SL): $1.875 (-1.5% on 5x)
# - TP1 Gain: $0.875 (+0.7% on 5x, 50% position)
# - TP2 Gain: $1.875 (+1.5% on 5x, remaining 50%)
# - Full Win: $2.75 total profit
# ================================
# n8n ENVIRONMENT VARIABLES
# ================================
# You need to set these in n8n (Settings → Environment Variables):
# - TRADING_BOT_API_URL=http://your-server:3000
# - API_SECRET_KEY=same_as_above
# - TELEGRAM_CHAT_ID=your_telegram_chat_id
#
# Note: Telegram notifications are handled by n8n, not by v4 bot
# ================================
# SECURITY NOTES
# ================================
# ⚠️ Never commit .env.local to git
# ⚠️ Use a dedicated trading wallet with limited funds
# ⚠️ Start with small position sizes ($50-100)
# ⚠️ Keep private keys secure
# ⚠️ Rotate API_SECRET_KEY regularly
# ================================
# GETTING API KEYS
# ================================
# Helius RPC: https://helius.dev (free tier available)
# Phantom Wallet: Download from https://phantom.app
# Random secret: openssl rand -hex 32
# Pyth Network: No API key needed - it's free!
# Getting API Keys:
# - Helius RPC: https://helius.dev (free: 100k requests/day)
# - Telegram Bot: @BotFather on Telegram
# - Random secret: openssl rand -hex 32