Files
trading_bot_v4/docs/guides/TESTING.md
mindesbunister 14d5de2c64 chore: Organize workspace structure - move docs, workflows, scripts to subdirectories
Organization:
- Created docs/ with setup/, guides/, history/ subdirectories
- Created workflows/ with trading/, analytics/, telegram/, archive/ subdirectories
- Created scripts/ with docker/, setup/, testing/ subdirectories
- Created tests/ for TypeScript test files
- Created archive/ for unused reference files

Moved files:
- 17 documentation files → docs/
- 16 workflow JSON files → workflows/
- 10 shell scripts → scripts/
- 4 test files → tests/
- 5 unused files → archive/

Updated:
- README.md with new file structure and documentation paths

Deleted:
- data/ (empty directory)
- screenshots/ (empty directory)

Critical files remain in root:
- telegram_command_bot.py (active bot - used by Dockerfile)
- watch-restart.sh (systemd service dependency)
- All Dockerfiles and docker-compose files
- All environment files

Validation:
 Containers running (trading-bot-v4, telegram-trade-bot, postgres)
 API responding (positions endpoint tested)
 Telegram bot functional (/status command tested)
 All critical files present in root

No code changes - purely organizational.
System continues running without interruption.

Recovery: git revert HEAD or git reset --hard cleanup-before
2025-10-27 12:59:25 +01:00

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:

  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:

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

# 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

  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! 🚀