Features: - Autonomous trading system with Drift Protocol on Solana - Real-time position monitoring with Pyth price feeds - Dynamic stop-loss and take-profit management - n8n workflow integration for TradingView signals - Beautiful web UI for settings management - REST API for trade execution and monitoring - Next.js 15 with standalone output mode - TypeScript with strict typing - Docker containerization with multi-stage builds - PostgreSQL database for trade history - Singleton pattern for Drift client connection pooling - BN.js for BigNumber handling (Drift SDK requirement) - Configurable stop-loss and take-profit levels - Breakeven trigger and profit locking - Daily loss limits and trade cooldowns - Slippage tolerance controls - DRY_RUN mode for safe testing - Real-time risk calculator - Interactive sliders for all parameters - Live preview of trade outcomes - Position sizing and leverage controls - Beautiful gradient design with Tailwind CSS - POST /api/trading/execute - Execute trades - POST /api/trading/close - Close positions - GET /api/trading/positions - Monitor active trades - GET /api/trading/check-risk - Validate trade signals - GET /api/settings - View configuration - POST /api/settings - Update configuration - Fixed Borsh serialization errors (simplified order params) - Resolved RPC rate limiting with singleton pattern - Fixed BigInt vs BN type mismatches - Corrected order execution flow - Improved position state management - Complete setup guides - Docker deployment instructions - n8n workflow configuration - API reference documentation - Risk management guidelines - Runs on port 3001 (external), 3000 (internal) - Uses Helius RPC for optimal performance - Production-ready with error handling - Health monitoring and logging
9.0 KiB
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:
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:
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:
- Set position size to small amount ($10-50)
- Have USDC in Drift account
- Server running (
npm run dev) - Environment configured
How to run:
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:
-
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)
-
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)
-
SL Hit (Loss):
- Price drops to -1.5%
- Closes 100% of position
- Total loss: -$7.50-15 (on $50 position)
-
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
# 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
# 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
# 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"
# Install missing dependency
npm install @pythnetwork/price-service-client
"Drift service not initialized"
# Check .env.local has:
DRIFT_WALLET_PRIVATE_KEY=your_key_here
SOLANA_RPC_URL=your_rpc_url
"API_KEY not set"
# Add to .env.local:
API_KEY=your_secret_key_here
"WebSocket connection failed"
# Normal - will fall back to polling
# RPC polling happens every 2s
# If RPC also fails, check SOLANA_RPC_URL
"Position not auto-closing"
# 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
-
Run price monitor first
- Validates Pyth connection
- Shows update frequency
- Reveals RPC issues early
-
Test position manager next
- Confirms monitoring logic
- Tests multi-position support
- No real trades = safe
-
Full flow test last
- Only after components work
- Start with $10-20 positions
- Watch first 5-10 trades
-
Monitor the logs
- Console shows all price updates
- Exit conditions logged
- Helps debug issues
-
Compare with Drift UI
- Verify positions match
- Check P&L accuracy
- Confirm closes executed
📞 Next Steps
After all tests pass:
-
Configure TradingView alerts
- Use n8n webhook URL
- Test with manual triggers
-
Start with small positions
- $10-50 per trade
- 5-10 test trades
- Supervised monitoring
-
Scale up gradually
- Increase to $100-300
- Add more symbols
- Reduce supervision
-
Monitor performance
- Track win rate
- Review P&L
- Adjust parameters
-
Prepare for Phase 3
- Database setup
- Risk manager config
- Notification channels
Ready to test? Start with test-price-monitor.ts! 🚀