# Phase 2 Testing Guide ## ๐Ÿงช Test Scripts Overview Phase 2 includes three comprehensive test scripts to validate the autonomous trading system. --- ## 1. test-price-monitor.ts **Purpose**: Test Pyth Network price monitoring **What it tests**: - WebSocket connection to Pyth Hermes - Price updates for multiple symbols (SOL, BTC, ETH) - Update frequency and reliability - RPC polling fallback - Price caching **How to run**: ```bash cd v4 npx tsx test-price-monitor.ts ``` **Expected output**: ``` ๐Ÿงช Testing Pyth Price Monitor... ๐Ÿ“Š Monitoring: SOL-PERP, BTC-PERP, ETH-PERP โฑ๏ธ Duration: 30 seconds ๐Ÿ“ก Source: Pyth Network (WebSocket + Polling) โœ… Price monitor started! ๐Ÿ’ฐ SOL-PERP $ 140.2350 (+0.000%) [1 updates] ๐Ÿ’ฐ BTC-PERP $43251.8700 (+0.000%) [1 updates] ๐Ÿ’ฐ ETH-PERP $ 2345.6200 (+0.000%) [1 updates] ๐Ÿ’ฐ SOL-PERP $ 140.2351 (+0.001%) [2 updates] ... ๐Ÿ“Š Test Results: SOL-PERP: Updates: 15 (0.50/sec) Avg Price: $140.2355 Min Price: $140.2340 Max Price: $140.2370 Range: $0.0030 (0.002%) Last Update: 0.1s ago โœ… PASS: Good update rate (0.50/sec) โœ… PASS: Recent updates (0.1s ago) ๐ŸŽ‰ Price monitor test complete! ``` **What to check**: - โœ… Updates should be 0.3-2 per second per symbol - โœ… Last update should be < 5 seconds ago - โœ… No connection errors - โœ… All symbols receiving updates **If WebSocket fails**: - Will automatically fall back to RPC polling - Updates will be ~0.5/sec (every 2 seconds) - This is normal and acceptable --- ## 2. test-position-manager.ts **Purpose**: Test position tracking and monitoring logic **What it tests**: - Adding trades to position manager - Real-time price monitoring integration - Exit condition checks (SL/TP1/TP2/Emergency) - Status reporting - Multi-position support **How to run**: ```bash cd v4 npx tsx test-position-manager.ts ``` **Expected output**: ``` ๐Ÿงช Testing Position Manager... ๐Ÿ“ Test 1: Adding simulated LONG trade... โœ… Long trade added Entry: $140.0 SL: $137.90 (-1.5%) TP1: $140.98 (+0.7%) TP2: $142.10 (+1.5%) ๐Ÿ“ Test 2: Adding simulated SHORT trade... โœ… Short trade added Entry: $43000 SL: $43645.00 (+1.5%) TP1: $42699.00 (-0.7%) TP2: $42355.00 (-1.5%) ๐Ÿ“ Test 3: Checking manager status... โœ… Status: { "isMonitoring": true, "activeTradesCount": 2, "symbols": ["SOL-PERP", "BTC-PERP"] } ๐Ÿ“ Test 4: Monitoring positions for 60 seconds... (Real prices from Pyth will update every 2s) Watch for automatic exit conditions! โฑ๏ธ 10s - Active trades: 2 โฑ๏ธ 20s - Active trades: 2 โฑ๏ธ 30s - Active trades: 2 ... ๐Ÿ“ Test 5: Final status check... ๐Ÿ“ Test 6: Closing all remaining positions... ๐ŸŽ‰ Position manager test complete! ``` **What to check**: - โœ… Both trades added successfully - โœ… Manager started monitoring (check console logs) - โœ… Real prices fetched from Pyth every 2s - โœ… Exit conditions checked every 2s - โœ… If price hits targets, trades close automatically - โœ… Clean shutdown without errors **During the test**: - Watch the console for price update logs - If real market price hits a target, exit will trigger - Most likely no exits will occur (targets unlikely to hit in 60s) - This tests the monitoring loop, not actual exits --- ## 3. test-full-flow.ts **Purpose**: End-to-end test with real trade execution **What it tests**: - Complete flow: Signal โ†’ Execute โ†’ Monitor โ†’ Auto-exit - API authentication - Drift position opening - Position manager integration - Real-time P&L tracking - Automatic exit execution **โš ๏ธ WARNING**: This executes a REAL trade on Drift! **Prerequisites**: 1. Set position size to small amount ($10-50) 2. Have USDC in Drift account 3. Server running (`npm run dev`) 4. Environment configured **How to run**: ```bash cd v4 npx tsx test-full-flow.ts ``` **Expected output**: ``` ๐Ÿงช Testing Full Trading Flow (END-TO-END) โš ๏ธ WARNING: This will execute a REAL trade on Drift! Make sure position size is small ($10-50) Press Ctrl+C to cancel, or wait 5 seconds to continue... ๐Ÿ“ Step 1: Executing trade... Payload: { "symbol": "SOLUSDT", "direction": "long", "timeframe": "5" } โœ… Trade executed! ID: trade-1234567890 Symbol: SOL-PERP Direction: LONG Entry Price: $ 140.2350 Position Size: $ 50.00 Leverage: 10x ๐Ÿ“ Step 2: Monitoring position... Duration: 120 seconds (2 minutes) Updates: Every 10 seconds Waiting for automatic exit... โฑ๏ธ 10s elapsed... Current Price: $140.2451 Unrealized P&L: $0.72 (+1.44% account) TP1 Hit: No SL Moved: No โฑ๏ธ 20s elapsed... Current Price: $140.3501 Unrealized P&L: $8.22 (+16.44% account) TP1 Hit: YES โœ… SL Moved: YES โœ… โฑ๏ธ 30s elapsed... โœ… TRADE CLOSED AUTOMATICALLY! Position no longer in active list ๐Ÿ“ Step 3: Final check... โœ… Trade successfully closed automatically! Check your Drift account for final P&L ๐ŸŽ‰ End-to-end test complete! ``` **What to check**: - โœ… Trade executes successfully - โœ… Position manager starts monitoring - โœ… Price updates every 10 seconds - โœ… P&L calculated correctly - โœ… TP1 detection works - โœ… SL moves to breakeven after TP1 - โœ… Position closes automatically - โœ… Final P&L matches Drift UI **Possible outcomes**: 1. **TP1 Hit โ†’ TP2 Hit** (Best case): - Price reaches +0.7%, closes 50% - SL moves to breakeven - Price reaches +1.5%, closes remaining 50% - Total profit: +$70-220 (depending on size) 2. **TP1 Hit โ†’ SL at Breakeven** (Break even): - Price reaches +0.7%, closes 50% - Price reverses, hits breakeven SL - Closes remaining 50% at entry - Total profit: +$35-70 (from TP1) 3. **SL Hit** (Loss): - Price drops to -1.5% - Closes 100% of position - Total loss: -$7.50-15 (on $50 position) 4. **No Exit in 2 Minutes** (Common): - Targets not reached yet - Position still active - Will auto-close when targets hit - This is normal! --- ## ๐ŸŽฏ Testing Strategy ### Week 1: Component Testing ```bash # Day 1-2: Price monitoring npx tsx test-price-monitor.ts # Run 5-10 times, verify consistent updates # Day 3-4: Position manager npx tsx test-position-manager.ts # Run 5-10 times, verify tracking works # Day 5-7: Full flow (supervised) npx tsx test-full-flow.ts # Run with $10 positions # Watch each trade closely ``` ### Week 2: Live Testing ```bash # Execute real trades via TradingView # Monitor logs in real-time # Verify auto-exits work # Check P&L on Drift # Start with 5-10 trades # Gradually increase position size ``` ### Week 3: Production ```bash # Let bot run fully autonomous # Check positions 2-3x per day # Review daily P&L # Adjust parameters if needed ``` --- ## ๐Ÿ“Š What Success Looks Like ### Price Monitor Test: - โœ… 0.3-2 updates per second per symbol - โœ… No dropped connections - โœ… < 5 second lag between updates - โœ… All symbols updating ### Position Manager Test: - โœ… Trades added without errors - โœ… Monitoring loop running - โœ… Price checks every 2 seconds - โœ… Clean shutdown ### Full Flow Test: - โœ… Trade executes on Drift - โœ… Position manager activates - โœ… P&L tracks correctly - โœ… Auto-exit when targets hit - โœ… Matches Drift UI exactly --- ## ๐Ÿ› Common Issues ### "Cannot find module" ```bash # Install missing dependency npm install @pythnetwork/price-service-client ``` ### "Drift service not initialized" ```bash # Check .env.local has: DRIFT_WALLET_PRIVATE_KEY=your_key_here SOLANA_RPC_URL=your_rpc_url ``` ### "API_KEY not set" ```bash # Add to .env.local: API_KEY=your_secret_key_here ``` ### "WebSocket connection failed" ```bash # Normal - will fall back to polling # RPC polling happens every 2s # If RPC also fails, check SOLANA_RPC_URL ``` ### "Position not auto-closing" ```bash # Check: 1. Is price actually hitting targets? 2. Are logs showing price checks? 3. Is position manager running? 4. Check slippage tolerance # Most likely: Targets not hit yet (normal!) ``` --- ## ๐Ÿ’ก Pro Tips 1. **Run price monitor first** - Validates Pyth connection - Shows update frequency - Reveals RPC issues early 2. **Test position manager next** - Confirms monitoring logic - Tests multi-position support - No real trades = safe 3. **Full flow test last** - Only after components work - Start with $10-20 positions - Watch first 5-10 trades 4. **Monitor the logs** - Console shows all price updates - Exit conditions logged - Helps debug issues 5. **Compare with Drift UI** - Verify positions match - Check P&L accuracy - Confirm closes executed --- ## ๐Ÿ“ž Next Steps After all tests pass: 1. **Configure TradingView alerts** - Use n8n webhook URL - Test with manual triggers 2. **Start with small positions** - $10-50 per trade - 5-10 test trades - Supervised monitoring 3. **Scale up gradually** - Increase to $100-300 - Add more symbols - Reduce supervision 4. **Monitor performance** - Track win rate - Review P&L - Adjust parameters 5. **Prepare for Phase 3** - Database setup - Risk manager config - Notification channels --- **Ready to test? Start with test-price-monitor.ts! ๐Ÿš€**