# Trading Bot v4 - Complete Implementation Manual ## ๐ŸŽฏ Project Overview **Trading Bot v4** is a fully automated 5-minute scalping system for Drift Protocol (Solana DEX) with the following features: - **TradingView Signal Detection**: Green/red dot signals on 5-minute charts - **Webhook-Driven Execution**: n8n workflow automation - **10x Leverage Trading**: $1000 capital with tight risk management - **Real-Time Monitoring**: Pyth Network price feeds (2-second updates) - **Smart Exit Logic**: Partial profit-taking with trailing stops - **Risk Management**: Daily limits, cooldown periods, and emergency stops --- ## ๐Ÿ“‹ Table of Contents 1. [Architecture Overview](#architecture-overview) 2. [Prerequisites](#prerequisites) 3. [Phase 1: TradingView Alert Setup](#phase-1-tradingview-alert-setup) 4. [Phase 2: n8n Workflow Configuration](#phase-2-n8n-workflow-configuration) 5. [Phase 3: Next.js Backend Setup](#phase-3-nextjs-backend-setup) 6. [Phase 4: Drift Protocol Integration](#phase-4-drift-protocol-integration) 7. [Phase 5: Price Monitoring System](#phase-5-price-monitoring-system) 8. [Phase 6: Trade Execution & Monitoring](#phase-6-trade-execution--monitoring) 9. [Phase 7: Testing & Deployment](#phase-7-testing--deployment) 10. [Configuration & Settings](#configuration--settings) 11. [Troubleshooting](#troubleshooting) --- ## ๐Ÿ—๏ธ Architecture Overview ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ TradingView โ”‚ โ”‚ 5min Chart โ”‚ โ”‚ Green/Red Dots โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Alert triggers โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ TradingView โ”‚ โ”‚ Webhook Alert โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ POST request โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ n8n Workflow โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ 1. Webhook Receiver โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ 2. Risk Check โ”‚ โ”‚ โ”‚ โ”‚ - Daily limits โ”‚ โ”‚ โ”‚ โ”‚ - Cooldown period โ”‚ โ”‚ โ”‚ โ”‚ - Max trades/hour โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ 3. Signal Validation โ”‚ โ”‚ โ”‚ โ”‚ - Verify signal strength โ”‚ โ”‚ โ”‚ โ”‚ - Check market conditions โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ 4. Execute Trade (Next.js API) โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ 5. Send Notifications โ”‚ โ”‚ โ”‚ โ”‚ - Telegram โ”‚ โ”‚ โ”‚ โ”‚ - Discord โ”‚ โ”‚ โ”‚ โ”‚ - Email โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Next.js Trading Bot API โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ POST /api/trading/execute โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Drift Trading Strategy โ”‚ โ”‚ โ”‚ โ”‚ - Open position (market order) โ”‚ โ”‚ โ”‚ โ”‚ - Set SL/TP targets โ”‚ โ”‚ โ”‚ โ”‚ - Start monitoring โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Pyth Price Monitor โ”‚ โ”‚ โ”‚ โ”‚ - WebSocket subscription โ”‚ โ”‚ โ”‚ โ”‚ - 2-second polling fallback โ”‚ โ”‚ โ”‚ โ”‚ - Real-time price updates โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Position Manager โ”‚ โ”‚ โ”‚ โ”‚ - Check exit conditions โ”‚ โ”‚ โ”‚ โ”‚ - Execute market closes โ”‚ โ”‚ โ”‚ โ”‚ - Update database โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Drift Protocol (Solana) โ”‚ โ”‚ โ”‚ โ”‚ - Open/Close positions โ”‚ โ”‚ - 10x leverage perpetuals โ”‚ โ”‚ - Real-time oracle prices (Pyth) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿ”ง Prerequisites ### Required Accounts & Services 1. **TradingView Pro/Premium** (for webhook alerts) 2. **n8n Instance** (cloud or self-hosted) 3. **Solana Wallet** with funded account 4. **Drift Protocol Account** (create at drift.trade) 5. **RPC Provider** (Helius, QuickNode, or Alchemy) 6. **Notification Services** (optional): - Telegram bot token - Discord webhook - Email SMTP ### Required Software ```bash Node.js >= 18.x npm or yarn Docker (for deployment) PostgreSQL (for trade history) ``` ### Environment Variables Create `.env.local`: ```bash # Solana & Drift SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_KEY DRIFT_WALLET_PRIVATE_KEY=your_base58_private_key DRIFT_PROGRAM_ID=dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH # n8n Integration N8N_WEBHOOK_SECRET=your_random_secret_key_here N8N_API_URL=https://your-n8n-instance.com # Pyth Network PYTH_HERMES_URL=https://hermes.pyth.network # TradingView TRADINGVIEW_WEBHOOK_SECRET=another_random_secret # Risk Management MAX_POSITION_SIZE_USD=1000 LEVERAGE=10 STOP_LOSS_PERCENT=-1.5 TAKE_PROFIT_1_PERCENT=0.7 TAKE_PROFIT_2_PERCENT=1.5 EMERGENCY_STOP_PERCENT=-2.0 MAX_DAILY_DRAWDOWN=-150 MAX_TRADES_PER_HOUR=6 MIN_TIME_BETWEEN_TRADES=600 # Notifications TELEGRAM_BOT_TOKEN=your_bot_token TELEGRAM_CHAT_ID=your_chat_id DISCORD_WEBHOOK_URL=your_discord_webhook EMAIL_SMTP_HOST=smtp.gmail.com EMAIL_SMTP_PORT=587 EMAIL_FROM=your-email@gmail.com EMAIL_TO=notification-email@gmail.com EMAIL_PASSWORD=your_app_password # Database DATABASE_URL=postgresql://user:password@localhost:5432/trading_bot_v4 # Monitoring PRICE_CHECK_INTERVAL_MS=2000 ``` --- ## ๐Ÿ“Š Phase 1: TradingView Alert Setup ### Step 1.1: Create Your Trading Strategy Your 5-minute chart should have: - Green dots = Buy signal (long) - Red dots = Sell signal (short) ### Step 1.2: Configure Alert 1. Right-click on your chart โ†’ **Add Alert** 2. **Condition**: Your indicator with green/red dot logic 3. **Alert name**: `SOLUSDT.P Buy Signal` or `SOLUSDT.P Sell Signal` 4. **Message** (JSON format): ```json { "action": "{{strategy.order.action}}", "symbol": "{{ticker}}", "timeframe": "{{interval}}", "price": "{{close}}", "timestamp": "{{timenow}}", "signal_type": "buy", "strength": "strong", "strategy": "5min_scalp_v4" } ``` ### Step 1.3: Configure Notifications Tab (see screenshot) โœ… **Enable these:** - โœ… Notify in app - โœ… Show toast notification - โœ… **Webhook URL** โ† This is critical! - โœ… Play sound โŒ **Disable these:** - โŒ Send email (we'll use n8n for this) - โŒ Send plain text ### Step 1.4: Set Webhook URL **Webhook URL format:** ``` https://your-n8n-instance.com/webhook/tradingview-signal ``` Or if using n8n cloud: ``` https://your-username.app.n8n.cloud/webhook/tradingview-signal ``` **Important:** Add a secret parameter: ``` https://your-n8n-instance.com/webhook/tradingview-signal?secret=YOUR_SECRET_KEY ``` ### Step 1.5: Test Alert 1. Click **Save** on alert 2. Manually trigger alert to test 3. Check n8n workflow execution logs --- ## ๐Ÿ”„ Phase 2: n8n Workflow Configuration ### Step 2.1: Install n8n **Option A: Docker (Recommended)** ```bash docker run -it --rm \ --name n8n \ -p 5678:5678 \ -v ~/.n8n:/home/node/.n8n \ n8nio/n8n ``` **Option B: npm** ```bash npm install -g n8n n8n start ``` Access at: `http://localhost:5678` ### Step 2.2: Import Trading Bot Workflow Create a new workflow with these nodes: #### Node 1: Webhook Trigger ```json { "name": "TradingView Signal", "type": "n8n-nodes-base.webhook", "typeVersion": 1, "position": [250, 300], "parameters": { "path": "tradingview-signal", "authentication": "headerAuth", "responseMode": "responseNode", "options": {} } } ``` #### Node 2: Verify Secret ```javascript // Function node const secret = $json.query?.secret; const expectedSecret = 'YOUR_SECRET_KEY'; // Use environment variable if (secret !== expectedSecret) { throw new Error('Invalid webhook secret'); } return { json: { verified: true, ...$json.body } }; ``` #### Node 3: Extract Signal Data ```javascript // Function node const signal = { action: $json.action || 'buy', symbol: $json.symbol || 'SOL-PERP', timeframe: $json.timeframe || '5', price: parseFloat($json.price) || 0, timestamp: $json.timestamp || new Date().toISOString(), signalType: $json.signal_type || 'buy', strength: $json.strength || 'moderate', strategy: $json.strategy || '5min_scalp_v4' }; // Normalize symbol for Drift Protocol if (signal.symbol.includes('SOL')) { signal.driftSymbol = 'SOL-PERP'; } else if (signal.symbol.includes('BTC')) { signal.driftSymbol = 'BTC-PERP'; } else if (signal.symbol.includes('ETH')) { signal.driftSymbol = 'ETH-PERP'; } // Determine direction signal.direction = signal.action === 'buy' ? 'long' : 'short'; return { json: signal }; ``` #### Node 4: Risk Check API Call ```json { "name": "Check Risk Limits", "type": "n8n-nodes-base.httpRequest", "typeVersion": 3, "position": [650, 300], "parameters": { "method": "POST", "url": "https://your-trading-bot.com/api/trading/check-risk", "authentication": "predefinedCredentialType", "nodeCredentialType": "httpHeaderAuth", "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Content-Type", "value": "application/json" }, { "name": "Authorization", "value": "Bearer YOUR_API_KEY" } ] }, "sendBody": true, "bodyParameters": { "parameters": [ { "name": "symbol", "value": "={{ $json.driftSymbol }}" } ] }, "options": {} } } ``` #### Node 5: IF Risk Passed ```json { "name": "Risk Check Passed?", "type": "n8n-nodes-base.if", "typeVersion": 1, "position": [850, 300], "parameters": { "conditions": { "boolean": [ { "value1": "={{ $json.allowed }}", "value2": true } ] } } } ``` #### Node 6: Execute Trade ```json { "name": "Execute Trade", "type": "n8n-nodes-base.httpRequest", "typeVersion": 3, "position": [1050, 250], "parameters": { "method": "POST", "url": "https://your-trading-bot.com/api/trading/execute", "authentication": "predefinedCredentialType", "nodeCredentialType": "httpHeaderAuth", "sendBody": true, "bodyParameters": { "parameters": [ { "name": "symbol", "value": "={{ $json.driftSymbol }}" }, { "name": "direction", "value": "={{ $json.direction }}" }, { "name": "timeframe", "value": "={{ $json.timeframe }}" }, { "name": "signalStrength", "value": "={{ $json.strength }}" } ] } } } ``` #### Node 7: Send Success Notification (Telegram) ```json { "name": "Telegram Success", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "position": [1250, 200], "parameters": { "chatId": "={{ $env.TELEGRAM_CHAT_ID }}", "text": "๐ŸŽฏ Trade Executed!\n\n๐Ÿ“Š Symbol: {{ $json.symbol }}\n๐Ÿ“ˆ Direction: {{ $json.direction }}\n๐Ÿ’ฐ Entry: ${{ $json.entryPrice }}\n๐ŸŽฒ Leverage: 10x\nโฑ๏ธ Time: {{ $json.timestamp }}\n\nโœ… Position opened successfully" } } ``` #### Node 8: Send Error Notification ```json { "name": "Telegram Error", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "position": [1050, 450], "parameters": { "chatId": "={{ $env.TELEGRAM_CHAT_ID }}", "text": "โŒ Trade Blocked\n\nโš ๏ธ Reason: {{ $json.reason }}\n๐Ÿ“Š Symbol: {{ $json.symbol }}\nโฑ๏ธ Time: {{ $json.timestamp }}" } } ``` #### Node 9: Webhook Response ```json { "name": "Response", "type": "n8n-nodes-base.respondToWebhook", "typeVersion": 1, "position": [1450, 300], "parameters": { "respondWith": "json", "responseBody": "={{ JSON.stringify($json) }}" } } ``` ### Step 2.3: Save and Activate Workflow 1. Click **Save** button 2. Click **Active** toggle to enable 3. Copy webhook URL 4. Test with TradingView alert ### Step 2.4: Add Additional Notification Nodes (Optional) **Discord Notification:** ```javascript // HTTP Request node { "method": "POST", "url": "YOUR_DISCORD_WEBHOOK_URL", "body": { "content": null, "embeds": [{ "title": "๐ŸŽฏ New Trade Executed", "color": 5814783, "fields": [ { "name": "Symbol", "value": "{{ $json.symbol }}", "inline": true }, { "name": "Direction", "value": "{{ $json.direction }}", "inline": true }, { "name": "Entry Price", "value": "${{ $json.entryPrice }}", "inline": true }, { "name": "Stop Loss", "value": "${{ $json.stopLoss }}", "inline": true }, { "name": "Take Profit 1", "value": "${{ $json.takeProfit1 }}", "inline": true }, { "name": "Take Profit 2", "value": "${{ $json.takeProfit2 }}", "inline": true } ], "timestamp": "{{ $json.timestamp }}" }] } } ``` **Email Notification:** Use n8n's built-in Email node with HTML template --- ## ๐Ÿ’ป Phase 3: Next.js Backend Setup ### Step 3.1: Create Required Files Run this command to create the file structure: ```bash mkdir -p lib/v4 mkdir -p app/api/trading mkdir -p prisma ``` ### Step 3.2: Update Prisma Schema I'll create the database schema in the next step. ### Step 3.3: Install Dependencies ```bash npm install @solana/web3.js @coral-xyz/anchor @drift-labs/sdk npm install @pythnetwork/price-service-client npm install @prisma/client npm install ws # WebSocket support npm install -D prisma ``` --- ## ๐Ÿš€ Phase 4: Drift Protocol Integration I'll create the Drift integration files next. --- ## ๐Ÿ“ก Phase 5: Price Monitoring System Implementation of Pyth Network real-time price monitoring. --- ## ๐ŸŽฏ Phase 6: Trade Execution & Monitoring Complete trading logic with risk management. --- ## ๐Ÿงช Phase 7: Testing & Deployment Testing procedures and deployment guide. --- ## โš™๏ธ Configuration & Settings ### Risk Management Settings ```typescript export const DEFAULT_RISK_CONFIG = { positionSize: 1000, // $1000 per trade leverage: 10, // 10x leverage = $10,000 position stopLossPercent: -1.5, // -1.5% = -15% account loss takeProfit1Percent: 0.7, // +0.7% = +7% account gain (50% close) takeProfit2Percent: 1.5, // +1.5% = +15% account gain (50% close) emergencyStopPercent: -2.0, // -2% = -20% hard stop maxDailyDrawdown: -150, // Stop trading at -$150 loss maxTradesPerHour: 6, // Max 6 trades per hour minTimeBetweenTrades: 600, // 10 minutes cooldown } ``` ### Supported Markets ```typescript const SUPPORTED_MARKETS = { 'SOL-PERP': 0, // Solana perpetual 'BTC-PERP': 1, // Bitcoin perpetual 'ETH-PERP': 2, // Ethereum perpetual // Add more markets as Drift adds them } ``` --- ## ๐Ÿ” Troubleshooting ### Common Issues **1. Webhook not receiving alerts** - Check TradingView alert is active - Verify webhook URL is correct - Check n8n workflow is activated - Test webhook with curl: ```bash curl -X POST https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET \ -H "Content-Type: application/json" \ -d '{"action":"buy","symbol":"SOLUSDT","timeframe":"5"}' ``` **2. Trade execution fails** - Check Solana wallet has sufficient SOL for gas - Verify Drift account is initialized - Check RPC endpoint is responding - Review error logs in Next.js console **3. Price monitoring not updating** - Verify Pyth WebSocket connection - Check RPC rate limits - Review price cache timestamps - Test with manual price fetch **4. Position not closing at targets** - Check price monitoring is active - Verify exit condition logic - Review slippage tolerance settings - Check Drift market liquidity --- ## ๐Ÿ“ˆ Expected Performance Based on your strategy: ### Win Scenarios - **Small wins (1%)**: ~60% of trades - **Medium wins (1.5%)**: ~30% of trades - **Large wins (2%+)**: ~10% of trades ### Risk Per Trade - **Max loss**: -$150 (-15% account) - **Average loss**: -$100 (-10% account) - **Win/Loss ratio**: Target 2:1 ### Daily Targets - **Trades per day**: 12-24 (based on 6/hour limit) - **Target profit**: $200-400 daily (+20-40%) - **Max drawdown**: -$150 (-15%) --- ## ๐ŸŽ“ Next Steps After reading this manual, we'll implement: 1. โœ… Complete file structure (next message) 2. โœ… Database schema and migrations 3. โœ… Drift Protocol integration code 4. โœ… Pyth price monitoring system 5. โœ… Trade execution engine 6. โœ… n8n workflow export file 7. โœ… Testing scripts 8. โœ… Deployment guide --- ## ๐Ÿ“ž Support & Resources - **Drift Protocol Docs**: https://docs.drift.trade - **Pyth Network Docs**: https://docs.pyth.network - **n8n Docs**: https://docs.n8n.io - **Solana Docs**: https://docs.solana.com --- **Ready to implement? Let's build Trading Bot v4! ๐Ÿš€**