# Trading Bot v4 🚀 **Fully Autonomous Trading Bot** for TradingView → n8n → Drift Protocol (Solana) ## Status | Phase | Status | Description | |-------|--------|-------------| | Phase 1 | ✅ **COMPLETE** | Trade execution from TradingView signals | | Phase 2 | ✅ **COMPLETE** | Real-time monitoring & automatic exits | | Phase 3 | 🚧 **PLANNED** | Database, risk manager, notifications | ## What It Does 1. **Receives signals** from TradingView (5-minute chart) 2. **Executes trades** on Drift Protocol (Solana DEX) 3. **Monitors prices** in real-time via Pyth Network 4. **Closes positions** automatically at TP1/TP2/SL 5. **Adjusts stops** dynamically (breakeven, profit lock) **100% autonomous. No manual intervention required!** ## Quick Start ### 1. Install Phase 2 ```bash # From project root ./install-phase2.sh ``` ### 2. Configure ```bash # Edit .env.local DRIFT_WALLET_PRIVATE_KEY=your_base58_key SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_KEY API_KEY=your_random_secret_key ``` ### 3. Test ```bash cd v4 # Test price monitoring (safe) npx tsx test-price-monitor.ts # Test position manager (safe) npx tsx test-position-manager.ts # Test full flow (REAL TRADE - use small size!) npx tsx test-full-flow.ts ``` ### 4. Trade ```bash # Start server npm run dev # Configure TradingView alerts → n8n webhook # Bot handles everything automatically! ``` --- ## Features ### Phase 1: Trade Execution ✅ - Drift Protocol integration - Market order execution - TradingView signal normalization - n8n webhook endpoint - Risk validation API ### Phase 2: Autonomous Trading ✅ - **Pyth price monitoring** (WebSocket + polling) - **Position manager** (tracks all trades) - **Automatic exits** (TP1/TP2/SL/Emergency) - **Dynamic SL** (breakeven + profit lock) - **Multi-position** support - **Real-time P&L** tracking ### Phase 3: Coming Soon 🚧 - Database persistence (PostgreSQL/Prisma) - Advanced risk manager - Trade history & analytics - Enhanced notifications - Web dashboard --- ## File Structure ``` v4/ ├── README.md ← You are here ├── QUICKREF_PHASE2.md ← Quick reference ├── PHASE_2_COMPLETE.md ← Phase 2 features ├── PHASE_2_SUMMARY.md ← Detailed summary ├── TESTING.md ← Testing guide ├── SETUP.md ← Setup instructions │ ├── config/ │ └── trading.ts ← Trading configuration │ ├── lib/ │ ├── drift/ │ │ ├── client.ts ← Drift SDK wrapper │ │ └── orders.ts ← Order execution │ ├── pyth/ │ │ └── price-monitor.ts ← Real-time prices │ └── trading/ │ └── position-manager.ts ← Auto-exit logic │ ├── app/ │ └── api/ │ └── trading/ │ ├── execute/ │ │ └── route.ts ← Execute trade │ ├── check-risk/ │ │ └── route.ts ← Risk validation │ └── positions/ │ └── route.ts ← Query positions │ └── test-*.ts ← Test scripts ``` --- ## Documentation | Document | Purpose | |----------|---------| | `README.md` | This overview | | `QUICKREF_PHASE2.md` | Quick reference card | | `SETUP.md` | Detailed setup instructions | | `TESTING.md` | Comprehensive testing guide | | `PHASE_2_COMPLETE.md` | Phase 2 feature overview | | `PHASE_2_SUMMARY.md` | Detailed Phase 2 summary | **Root documentation:** - `../TRADING_BOT_V4_MANUAL.md` - Complete manual - `../QUICKSTART_V4.md` - Quick start guide - `../N8N_SETUP_GUIDE.md` - n8n configuration --- ## Trade Example ### Entry Signal ``` TradingView: LONG SOL @ $140.00 Position: $1,000 (10x = $10,000) SL: $137.90 (-1.5%) TP1: $140.98 (+0.7%) TP2: $142.10 (+1.5%) ``` ### TP1 Hit ``` ✅ Price reaches $140.98 → Auto-close 50% (+$70) → Move SL to $140.21 (breakeven) → Trade is now RISK-FREE ``` ### TP2 Hit ``` ✅ Price reaches $142.10 → Auto-close remaining 50% (+$150) → Total P&L: +$220 (+22% account) → Trade complete! ``` --- ## Safety Guidelines 1. **Start Small**: Use $10-50 positions first 2. **Test Thoroughly**: Run all test scripts 3. **Monitor Closely**: Watch first 10 auto-exits 4. **Verify Fills**: Check Drift UI after exits 5. **Scale Gradually**: Increase size weekly --- ## Resources - **Drift Protocol**: https://drift.trade - **Drift Docs**: https://docs.drift.trade - **Pyth Network**: https://pyth.network - **Solana RPC**: https://helius.dev --- **Ready to trade autonomously? Read `QUICKREF_PHASE2.md` to get started! 🚀** *Start small, monitor closely, scale gradually!*