Files
trading_bot_v3/v4/QUICKREF_PHASE2.md
mindesbunister 1345a35680 feat: Complete Phase 2 - Autonomous Trading System
- Add Pyth Network price monitoring (WebSocket + polling fallback)
- Add Position Manager with automatic exit logic (TP1/TP2/SL)
- Implement dynamic stop-loss adjustment (breakeven + profit lock)
- Add real-time P&L tracking and multi-position support
- Create comprehensive test suite (3 test scripts)
- Add 5 detailed documentation files (2500+ lines)
- Update configuration to $50 position size for safe testing
- All Phase 2 features complete and tested

Core Components:
- v4/lib/pyth/price-monitor.ts - Real-time price monitoring
- v4/lib/trading/position-manager.ts - Autonomous position management
- v4/app/api/trading/positions/route.ts - Query positions endpoint
- v4/test-*.ts - Comprehensive testing suite

Documentation:
- PHASE_2_COMPLETE_REPORT.md - Implementation summary
- v4/PHASE_2_SUMMARY.md - Detailed feature overview
- v4/TESTING.md - Testing guide
- v4/QUICKREF_PHASE2.md - Quick reference
- install-phase2.sh - Automated installation script
2025-10-23 14:40:29 +02:00

4.5 KiB

🚀 Phase 2 Quick Reference

What's New

Fully Autonomous Trading

  • Opens positions from signals
  • Monitors prices in real-time
  • Closes automatically at TP/SL
  • Adjusts stops dynamically

Quick Start

1. Install

./install-phase2.sh

2. Configure

# Edit .env.local:
DRIFT_WALLET_PRIVATE_KEY=your_key
SOLANA_RPC_URL=your_rpc
API_KEY=your_secret

3. Test

cd v4

# Test price monitoring
npx tsx test-price-monitor.ts

# Test position manager
npx tsx test-position-manager.ts

# Test full flow (REAL TRADE!)
npx tsx test-full-flow.ts

4. Trade

# Start server
npm run dev

# Trigger TradingView alert
# Bot handles everything!

Key Features

Smart Exits

  • TP1 (+0.7%): Close 50%, move SL to breakeven
  • TP2 (+1.5%): Close remaining 50%
  • SL (-1.5%): Close 100%
  • Emergency (-2.0%): Hard stop

Dynamic SL

  • After TP1: Move to breakeven
  • At +1.0% profit: Move to +0.4%
  • Never moves backward
  • Protects all gains

Real-Time

  • Pyth WebSocket (~400ms)
  • Polling fallback (2s)
  • Checks every 2 seconds
  • Market orders for speed

API Endpoints

Execute Trade

POST /api/trading/execute
{
  "symbol": "SOLUSDT",
  "direction": "long",
  "timeframe": "5"
}

Get Positions

GET /api/trading/positions

Trade Example

Entry

Signal: LONG SOL @ $140.00
Position: $1,000 (10x = $10,000)
SL: $137.90 (-1.5% = -$150)
TP1: $140.98 (+0.7% = +$70)
TP2: $142.10 (+1.5% = +$150)

TP1 Hit

✅ Price: $140.98
→ Close 50% (+$70)
→ Move SL to $140.21 (breakeven)
→ Risk = $0

TP2 Hit

✅ Price: $142.10
→ Close 50% (+$150)
→ Total P&L: +$220 (+22%)
→ Trade complete!

Testing Checklist

  • Run install-phase2.sh
  • Configure .env.local
  • Test price monitor (no risk)
  • Test position manager (no risk)
  • Test full flow with $10-50 position
  • Watch first 5-10 auto-exits
  • Verify on Drift UI
  • Scale up gradually

Configuration

Risk Parameters

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

Monitoring

PRICE_CHECK_INTERVAL_MS=2000
SLIPPAGE_TOLERANCE=1.0

Troubleshooting

"Cannot find module"

npm install @pythnetwork/price-service-client

"Drift service not initialized"

# Check .env.local has:
DRIFT_WALLET_PRIVATE_KEY=...
SOLANA_RPC_URL=...

"WebSocket disconnected"

Normal - will reconnect automatically
Polling fallback handles updates

"Position not closing"

Check:
1. Is price hitting targets?
2. Are logs showing price checks?
3. Is position manager running?

Most likely: Targets not hit yet!

Performance Targets

5-Minute Scalping

  • Win Rate: 60-70%
  • Avg Win: +7% to +22%
  • Avg Loss: -15%
  • Daily Target: +2% to +5%
  • Trades/Day: 5-15

Example Day

Trade 1: +7%  (TP1)
Trade 2: +22% (TP1+TP2)
Trade 3: -15% (SL)
Trade 4: +7%  (TP1)
Trade 5: +22% (TP1+TP2)
Daily: +43% 🎉

Safety Rules

  1. Start small: $10-50 positions
  2. Monitor closely: First 10 trades
  3. Verify exits: Check Drift UI
  4. Scale gradually: Increase 2x weekly
  5. Max risk: Never > 20% per trade

Documentation

  • PHASE_2_COMPLETE.md - Full features
  • PHASE_2_SUMMARY.md - Overview
  • TESTING.md - Testing guide
  • SETUP.md - Setup instructions
  • TRADING_BOT_V4_MANUAL.md - Complete manual

What's Next (Phase 3)

  • Database integration
  • Trade history persistence
  • Risk manager enforcement
  • Enhanced notifications
  • Performance analytics
  • Web dashboard

But you can trade NOW!


Support

Common Commands

# Install Phase 2
./install-phase2.sh

# Test monitoring
cd v4 && npx tsx test-price-monitor.ts

# Test manager
cd v4 && npx tsx test-position-manager.ts

# Test full flow
cd v4 && npx tsx test-full-flow.ts

# Start server
npm run dev

# Check positions
curl http://localhost:3000/api/trading/positions \
  -H "Authorization: Bearer YOUR_API_KEY"

Files Changed

New:
+ v4/lib/pyth/price-monitor.ts
+ v4/lib/trading/position-manager.ts
+ v4/app/api/trading/positions/route.ts
+ v4/test-price-monitor.ts
+ v4/test-position-manager.ts
+ v4/test-full-flow.ts
+ v4/PHASE_2_COMPLETE.md
+ v4/PHASE_2_SUMMARY.md
+ v4/TESTING.md
+ install-phase2.sh

Updated:
~ v4/app/api/trading/execute/route.ts
~ v4/SETUP.md

Phase 2 Complete! Let the bot trade! 🚀

Start small, monitor closely, scale gradually!