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
This commit is contained in:
505
docs/guides/N8N_WORKFLOW_GUIDE.md
Normal file
505
docs/guides/N8N_WORKFLOW_GUIDE.md
Normal file
@@ -0,0 +1,505 @@
|
||||
# n8n Workflow Setup Guide - Trading Bot v4
|
||||
|
||||
Complete guide to set up the automated trading workflow in n8n.
|
||||
|
||||
---
|
||||
|
||||
## 🔐 Security Model (Simplified)
|
||||
|
||||
This workflow uses **ONE secret** for authentication:
|
||||
|
||||
**API_SECRET_KEY** - Authenticates n8n → v4 Trading Bot API
|
||||
- Set in `v4/.env` (generate with: `openssl rand -hex 32`)
|
||||
- Set in n8n environment variables (must match v4)
|
||||
- Used when calling `/api/trading/check-risk` and `/api/trading/execute`
|
||||
|
||||
**Your TradingView webhook** (`https://flow.egonetix.de/webhook/tradingview-webhook`) is directly accessible. If you need additional security, use `n8n-workflow-complete.json` which includes optional TradingView secret validation.
|
||||
|
||||
---
|
||||
|
||||
## 📋 Workflow Overview
|
||||
|
||||
```
|
||||
TradingView Alert (Webhook)
|
||||
↓
|
||||
Parse Signal (SOL/BTC/ETH, LONG/SHORT)
|
||||
↓
|
||||
Check Risk Limits (API call to /api/trading/check-risk)
|
||||
↓
|
||||
Execute Trade (API call to /api/trading/execute)
|
||||
↓
|
||||
Format Message (Success/Error/Blocked)
|
||||
↓
|
||||
Send Telegram Notification
|
||||
```
|
||||
|
||||
**Two workflow versions available:**
|
||||
- `n8n-workflow-simple.json` - **Recommended** - Direct flow without secret validation
|
||||
- `n8n-workflow-complete.json` - With optional TradingView webhook secret validation
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Quick Setup
|
||||
|
||||
### Step 1: Import Workflow
|
||||
|
||||
1. Open your n8n instance (e.g., https://flow.egonetix.de)
|
||||
2. Click **"+ New Workflow"**
|
||||
3. Click **"⋮"** (three dots) → **"Import from File"**
|
||||
4. Select `n8n-workflow-simple.json` (recommended) or `n8n-workflow-complete.json`
|
||||
5. Click **"Import"**
|
||||
|
||||
### Step 2: Configure Environment Variables
|
||||
|
||||
In n8n, go to **Settings** → **Environment Variables** and add:
|
||||
|
||||
```bash
|
||||
# Your trading bot API URL (where v4 is running)
|
||||
TRADING_BOT_API_URL=http://your-server:3000
|
||||
|
||||
# API secret key (must match v4/.env)
|
||||
API_SECRET_KEY=your_secret_key_from_v4_env
|
||||
|
||||
# Telegram credentials
|
||||
TELEGRAM_CHAT_ID=your_telegram_chat_id
|
||||
```
|
||||
|
||||
**Note:** `TRADINGVIEW_WEBHOOK_SECRET` is only needed if using `n8n-workflow-complete.json`
|
||||
|
||||
### Step 3: Configure Telegram Credentials
|
||||
|
||||
1. In n8n, click **"Telegram - Send Notification"** node
|
||||
2. Click **"Create New Credential"**
|
||||
3. Enter your **Telegram Bot Token**
|
||||
4. Save credential
|
||||
|
||||
### Step 4: Get Webhook URL
|
||||
|
||||
1. Click **"Webhook - TradingView Alert"** node
|
||||
2. Click **"Test URL"** or **"Production URL"**
|
||||
3. Copy the webhook URL (should be: `https://flow.egonetix.de/webhook/tradingview-webhook`)
|
||||
4. Save this for TradingView setup
|
||||
|
||||
### Step 5: Activate Workflow
|
||||
|
||||
1. Toggle **"Active"** switch at top right
|
||||
2. Workflow is now listening for webhooks!
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Detailed Configuration
|
||||
|
||||
### Webhook Node Configuration
|
||||
|
||||
**Node:** `Webhook - TradingView Alert`
|
||||
|
||||
- **HTTP Method:** POST
|
||||
- **Path:** `tradingview-webhook` (or customize)
|
||||
- **Response:** Return on Last Node
|
||||
- **Raw Body:** Enabled
|
||||
|
||||
**What it does:** Receives TradingView alerts directly via webhook
|
||||
|
||||
**Your webhook URL:** `https://flow.egonetix.de/webhook/tradingview-webhook`
|
||||
|
||||
### Parse TradingView Signal Node
|
||||
|
||||
**Node:** `Parse TradingView Signal`
|
||||
|
||||
- **Type:** Code (Function)
|
||||
- **Language:** JavaScript
|
||||
|
||||
**What it does:**
|
||||
- Extracts symbol, action, timeframe from TradingView alert
|
||||
- Normalizes data for v4 API (SOL→SOLUSDT, buy→long, etc.)
|
||||
- Supports various TradingView alert formats
|
||||
|
||||
**Supported formats:**
|
||||
```json
|
||||
{
|
||||
"symbol": "SOLUSDT",
|
||||
"action": "buy",
|
||||
"timeframe": "5",
|
||||
"price": "140.25",
|
||||
"timestamp": "2025-10-23T10:00:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
Or:
|
||||
```json
|
||||
{
|
||||
"ticker": "SOL-PERP",
|
||||
"signal_type": "long",
|
||||
"interval": "5m",
|
||||
"close": "140.25"
|
||||
}
|
||||
```
|
||||
|
||||
### Check Risk Limits Node
|
||||
|
||||
**Node:** `Check Risk Limits`
|
||||
|
||||
- **URL:** `{{$env.TRADING_BOT_API_URL}}/api/trading/check-risk`
|
||||
- **Method:** POST
|
||||
- **Headers:**
|
||||
- `Authorization: Bearer {{$env.API_SECRET_KEY}}`
|
||||
- `Content-Type: application/json`
|
||||
- **Body:**
|
||||
```json
|
||||
{
|
||||
"symbol": "{{$json.apiPayload.symbol}}",
|
||||
"direction": "{{$json.apiPayload.direction}}"
|
||||
}
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Checks daily drawdown limits
|
||||
- Validates trades per hour
|
||||
- Ensures cooldown period passed
|
||||
|
||||
### Execute Trade Node
|
||||
|
||||
**Node:** `Execute Trade on Drift`
|
||||
|
||||
- **URL:** `{{$env.TRADING_BOT_API_URL}}/api/trading/execute`
|
||||
- **Method:** POST
|
||||
- **Timeout:** 30000ms (30 seconds)
|
||||
- **Headers:**
|
||||
- `Authorization: Bearer {{$env.API_SECRET_KEY}}`
|
||||
- `Content-Type: application/json`
|
||||
- **Body:**
|
||||
```json
|
||||
{
|
||||
"symbol": "SOLUSDT",
|
||||
"direction": "long",
|
||||
"timeframe": "5",
|
||||
"signalStrength": "strong",
|
||||
"signalPrice": 140.25
|
||||
}
|
||||
```
|
||||
|
||||
**What it does:**
|
||||
- Opens position on Drift Protocol
|
||||
- Starts automatic monitoring
|
||||
- Returns trade details (entry price, TP/SL levels)
|
||||
|
||||
### Format Message Nodes
|
||||
|
||||
**Three formatting nodes for different scenarios:**
|
||||
|
||||
1. **Format Success Message** - Trade executed successfully
|
||||
2. **Format Error Message** - Trade execution failed
|
||||
3. **Format Risk Blocked Message** - Trade blocked by risk limits
|
||||
|
||||
**Output example (Success):**
|
||||
```
|
||||
🟢 TRADE EXECUTED
|
||||
|
||||
📊 Symbol: SOL-PERP
|
||||
📈 Direction: LONG
|
||||
💰 Entry Price: $140.2350
|
||||
💵 Position Size: $500.00
|
||||
⚡ Leverage: 10x
|
||||
|
||||
🎯 Targets:
|
||||
Stop Loss: $137.90 (-1.5%)
|
||||
TP1: $140.98 (+0.7%)
|
||||
TP2: $142.10 (+1.5%)
|
||||
|
||||
📊 Slippage: 0.015%
|
||||
⏰ Time: 10/23/2025, 10:00:00 AM
|
||||
|
||||
✅ Position is now being monitored automatically.
|
||||
Auto-exit at TP/SL levels.
|
||||
```
|
||||
|
||||
### Telegram Node
|
||||
|
||||
**Node:** `Telegram - Send Notification`
|
||||
|
||||
- **Chat ID:** `{{$env.TELEGRAM_CHAT_ID}}`
|
||||
- **Text:** `{{$json.message}}`
|
||||
- **Parse Mode:** Markdown
|
||||
|
||||
**What it does:** Sends formatted notification to your Telegram
|
||||
|
||||
---
|
||||
|
||||
## 🎯 TradingView Alert Setup
|
||||
|
||||
### Alert Configuration
|
||||
|
||||
1. **In TradingView:** Right-click chart → Add Alert
|
||||
2. **Condition:** Your indicator/strategy (e.g., Green Dot appears)
|
||||
3. **Alert Name:** "SOL Long Signal" (or similar)
|
||||
4. **Webhook URL:**
|
||||
```
|
||||
https://flow.egonetix.de/webhook/tradingview-webhook
|
||||
```
|
||||
|
||||
**No secret parameter needed!** Just the direct webhook URL.
|
||||
|
||||
### Alert Message (JSON)
|
||||
|
||||
Use this format in the **Message** field:
|
||||
|
||||
```json
|
||||
{
|
||||
"symbol": "{{ticker}}",
|
||||
"action": "{{strategy.order.action}}",
|
||||
"timeframe": "{{interval}}",
|
||||
"price": "{{close}}",
|
||||
"timestamp": "{{timenow}}",
|
||||
"strategy": "5min_scalp",
|
||||
"strength": "strong"
|
||||
}
|
||||
```
|
||||
|
||||
**Important fields:**
|
||||
- `symbol`: Stock/crypto symbol (SOLUSDT, BTCUSD, etc.)
|
||||
- `action`: "buy"/"sell" or "long"/"short"
|
||||
- `timeframe`: Chart interval (5, 15, 60, etc.)
|
||||
- `price`: Current price from TradingView
|
||||
|
||||
### Notification Settings
|
||||
|
||||
✅ **Enable:**
|
||||
- Webhook URL
|
||||
- Notify on app
|
||||
- Play sound (optional)
|
||||
|
||||
❌ **Disable:**
|
||||
- Send email (n8n handles notifications)
|
||||
|
||||
---
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
### Test 1: Webhook Connection
|
||||
|
||||
```bash
|
||||
# Send test webhook from command line
|
||||
curl -X POST https://flow.egonetix.de/webhook/tradingview-webhook \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"symbol": "SOLUSDT",
|
||||
"action": "buy",
|
||||
"timeframe": "5",
|
||||
"price": "140.25",
|
||||
"timestamp": "2025-10-23T10:00:00Z"
|
||||
}'
|
||||
```
|
||||
|
||||
### Test 2: Check n8n Executions
|
||||
|
||||
1. In n8n, click **"Executions"** tab
|
||||
2. Find your test execution
|
||||
3. Click to view detailed flow
|
||||
4. Check each node for errors
|
||||
|
||||
### Test 3: Verify API Response
|
||||
|
||||
Expected response from `/api/trading/execute`:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"trade": {
|
||||
"id": "trade-1234567890",
|
||||
"symbol": "SOL-PERP",
|
||||
"direction": "long",
|
||||
"entryPrice": 140.235,
|
||||
"positionSize": 500,
|
||||
"leverage": 10,
|
||||
"stopLoss": 137.90,
|
||||
"takeProfit1": 140.98,
|
||||
"takeProfit2": 142.10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Test 4: Telegram Message
|
||||
|
||||
You should receive a formatted Telegram message with trade details.
|
||||
|
||||
---
|
||||
|
||||
## 🔍 Troubleshooting
|
||||
|
||||
### Webhook Not Receiving Data
|
||||
|
||||
**Problem:** n8n workflow not triggering
|
||||
|
||||
**Solutions:**
|
||||
1. Check webhook is **Active** (toggle at top)
|
||||
2. Verify webhook URL in TradingView matches n8n: `https://flow.egonetix.de/webhook/tradingview-webhook`
|
||||
3. Test with curl command (see Testing section)
|
||||
4. Check n8n logs for errors
|
||||
|
||||
### Invalid Secret Error
|
||||
|
||||
**Problem:** "Unauthorized Webhook" message
|
||||
|
||||
**Solutions:**
|
||||
- **Only applies if using `n8n-workflow-complete.json`**
|
||||
- If using `n8n-workflow-simple.json`, this error won't occur
|
||||
|
||||
### API Authentication Failed
|
||||
|
||||
**Problem:** "401 Unauthorized" from trading bot
|
||||
|
||||
**Solutions:**
|
||||
1. Verify `API_SECRET_KEY` in n8n matches v4 `.env`
|
||||
2. Check `Authorization` header format: `Bearer YOUR_KEY`
|
||||
3. Regenerate key if needed: `openssl rand -hex 32`
|
||||
|
||||
### Trade Not Executing
|
||||
|
||||
**Problem:** Risk check passed but no position opened
|
||||
|
||||
**Solutions:**
|
||||
1. Check v4 API logs: `docker-compose logs -f trading-bot`
|
||||
2. Verify Drift wallet has sufficient collateral
|
||||
3. Check SOLANA_RPC_URL is working
|
||||
4. Ensure DRIFT_WALLET_PRIVATE_KEY is correct
|
||||
5. Test with curl:
|
||||
```bash
|
||||
curl -X POST http://localhost:3000/api/trading/execute \
|
||||
-H "Authorization: Bearer YOUR_API_KEY" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"symbol":"SOLUSDT","direction":"long","timeframe":"5"}'
|
||||
```
|
||||
|
||||
### Telegram Not Sending
|
||||
|
||||
**Problem:** No Telegram notifications
|
||||
|
||||
**Solutions:**
|
||||
1. Verify Telegram Bot Token in credentials
|
||||
2. Check TELEGRAM_CHAT_ID is correct
|
||||
3. Ensure bot is started (send /start to your bot)
|
||||
4. Test Telegram node individually in n8n
|
||||
|
||||
---
|
||||
|
||||
## 📊 Monitoring
|
||||
|
||||
### View Executions
|
||||
|
||||
In n8n:
|
||||
1. Click **"Executions"** tab
|
||||
2. Filter by **"Success"** or **"Error"**
|
||||
3. Click execution to see detailed flow
|
||||
|
||||
### Check Active Positions
|
||||
|
||||
Query via API:
|
||||
```bash
|
||||
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
||||
http://localhost:3000/api/trading/positions
|
||||
```
|
||||
|
||||
Or check Drift UI: https://drift.trade
|
||||
|
||||
### View Bot Logs
|
||||
|
||||
```bash
|
||||
# Docker logs
|
||||
docker-compose logs -f trading-bot
|
||||
|
||||
# Or if using scripts
|
||||
cd v4 && ./docker-logs.sh
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔐 Security Best Practices
|
||||
|
||||
1. **Use Strong Secrets**
|
||||
```bash
|
||||
# Generate secure random secrets
|
||||
openssl rand -hex 32 # For API keys
|
||||
openssl rand -hex 16 # For webhook secrets
|
||||
```
|
||||
|
||||
2. **Protect Environment Variables**
|
||||
- Never commit `.env` files
|
||||
- Use n8n's environment variable encryption
|
||||
- Rotate secrets regularly
|
||||
|
||||
3. **IP Whitelisting** (optional)
|
||||
- Restrict webhook access to TradingView IPs
|
||||
- Use n8n's IP filtering if available
|
||||
|
||||
4. **Monitor Failed Attempts**
|
||||
- Set up alerts for unauthorized webhook attempts
|
||||
- Review n8n execution logs regularly
|
||||
|
||||
---
|
||||
|
||||
## 🎓 Advanced Configuration
|
||||
|
||||
### Custom Risk Parameters
|
||||
|
||||
Modify `Check Risk Limits` node to send additional parameters:
|
||||
|
||||
```json
|
||||
{
|
||||
"symbol": "SOL-PERP",
|
||||
"direction": "long",
|
||||
"customPositionSize": 100,
|
||||
"customLeverage": 5
|
||||
}
|
||||
```
|
||||
|
||||
### Multiple Strategies
|
||||
|
||||
Clone the workflow for different strategies:
|
||||
1. Duplicate workflow
|
||||
2. Change webhook path: `/webhook/tradingview-5min` vs `/webhook/tradingview-15min`
|
||||
3. Use different risk parameters per timeframe
|
||||
|
||||
### Advanced Notifications
|
||||
|
||||
Add Discord/Email nodes in parallel with Telegram:
|
||||
1. Add Discord webhook node
|
||||
2. Add SMTP email node
|
||||
3. Connect all to message formatter nodes
|
||||
|
||||
---
|
||||
|
||||
## 📞 Support
|
||||
|
||||
**Workflow Issues:**
|
||||
- Check n8n documentation: https://docs.n8n.io
|
||||
- Review execution logs in n8n
|
||||
|
||||
**API Issues:**
|
||||
- See `v4/TESTING.md` for API testing
|
||||
- Check `v4/DOCKER.md` for container logs
|
||||
|
||||
**Trading Issues:**
|
||||
- See `TRADING_BOT_V4_MANUAL.md` for complete guide
|
||||
- Check Drift Protocol status
|
||||
|
||||
---
|
||||
|
||||
## ✅ Checklist
|
||||
|
||||
Before going live:
|
||||
|
||||
- [ ] Import workflow to n8n
|
||||
- [ ] Configure all environment variables
|
||||
- [ ] Add Telegram credentials
|
||||
- [ ] Copy webhook URL
|
||||
- [ ] Configure TradingView alert with webhook
|
||||
- [ ] Test with small position size ($10-50)
|
||||
- [ ] Verify Telegram notification received
|
||||
- [ ] Check position opened on Drift
|
||||
- [ ] Monitor first 5-10 trades closely
|
||||
- [ ] Gradually increase position size
|
||||
|
||||
---
|
||||
|
||||
**Your automated trading system is now complete! 🎉**
|
||||
|
||||
When TradingView fires an alert → n8n executes the trade → You get a Telegram notification → Bot monitors and auto-exits at TP/SL!
|
||||
421
docs/guides/TESTING.md
Normal file
421
docs/guides/TESTING.md
Normal file
@@ -0,0 +1,421 @@
|
||||
# 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! 🚀**
|
||||
166
docs/guides/WORKFLOW_VERIFICATION.md
Normal file
166
docs/guides/WORKFLOW_VERIFICATION.md
Normal file
@@ -0,0 +1,166 @@
|
||||
# n8n Workflow Verification Report
|
||||
|
||||
## ✅ All Nodes Tested & Verified
|
||||
|
||||
### 1. Webhook Node
|
||||
- **Type**: `n8n-nodes-base.webhook` (v1)
|
||||
- **Method**: POST
|
||||
- **Path**: `tradingview-bot-v4`
|
||||
- **Status**: ✅ Working (standard n8n webhook)
|
||||
|
||||
### 2. Parse Signal Node ✓
|
||||
- **Type**: `n8n-nodes-base.set` (v3.2) - Same as working trader workflow
|
||||
- **Fields**:
|
||||
- `rawMessage`: Captures full body
|
||||
- `symbol`: Regex match for SOL/BTC/ETH → Maps to Drift perps
|
||||
- `direction`: Regex match for buy/sell/long/short
|
||||
- `timeframe`: Fixed to "5"
|
||||
- **Status**: ✅ Working (uses proven Edit Fields node)
|
||||
|
||||
### 3. Check Risk Node ✓
|
||||
- **Type**: `n8n-nodes-base.httpRequest` (v4)
|
||||
- **Method**: ✅ POST (FIXED - was missing)
|
||||
- **URL**: `http://10.0.0.48:3001/api/trading/check-risk`
|
||||
- **Headers**:
|
||||
- ✅ Authorization: Bearer token
|
||||
- ✅ Content-Type: application/json
|
||||
- **Body**: JSON with symbol and direction
|
||||
- **API Test**: ✅ PASSED
|
||||
```bash
|
||||
curl -X POST http://10.0.0.48:3001/api/trading/check-risk \
|
||||
-H "Authorization: Bearer 2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"symbol":"SOL-PERP","direction":"long"}'
|
||||
# Response: {"allowed":true,"details":"All risk checks passed"}
|
||||
```
|
||||
|
||||
### 4. Risk Passed? Node ✓
|
||||
- **Type**: `n8n-nodes-base.if` (v1)
|
||||
- **Condition**: `$json.allowed === true`
|
||||
- **Status**: ✅ Working (standard IF node)
|
||||
|
||||
### 5. Execute Trade Node ✓
|
||||
- **Type**: `n8n-nodes-base.httpRequest` (v4)
|
||||
- **Method POST**:
|
||||
- **URL**: `http://10.0.0.48:3001/api/trading/execute`
|
||||
- **Headers**:
|
||||
- ✅ Authorization: Bearer token
|
||||
- ✅ Content-Type: application/json
|
||||
- **Body**: JSON with symbol, direction, timeframe, signalStrength
|
||||
- **Timeout**: 30000ms (30 seconds)
|
||||
- **Status**: ✅ Configured correctly
|
||||
|
||||
### 6. Trade Success? Node ✓
|
||||
- **Type**: `n8n-nodes-base.if` (v1)
|
||||
- **Condition**: `$json.success === true`
|
||||
- **Status**: ✅ Working (standard IF node)
|
||||
|
||||
### 7. Format Success Node ✓
|
||||
- **Type**: `n8n-nodes-base.set` (v3.2)
|
||||
- **Message Format**:
|
||||
```
|
||||
🟢 TRADE OPENED
|
||||
|
||||
[raw message]
|
||||
|
||||
📊 Symbol: [symbol]
|
||||
📈 Direction: [direction]
|
||||
⏰ [time]
|
||||
|
||||
✅ Position monitored automatically
|
||||
```
|
||||
- **Status**: ✅ Working (uses proven Edit Fields)
|
||||
|
||||
### 8. Format Error Node ✓
|
||||
- **Type**: `n8n-nodes-base.set` (v3.2)
|
||||
- **Message Format**:
|
||||
```
|
||||
🔴 TRADE FAILED
|
||||
|
||||
[raw message]
|
||||
|
||||
❌ Error: [error]
|
||||
⏰ [time]
|
||||
```
|
||||
- **Status**: ✅ Working (uses proven Edit Fields)
|
||||
|
||||
### 9. Format Risk Node ✓
|
||||
- **Type**: `n8n-nodes-base.set` (v3.2)
|
||||
- **Message Format**:
|
||||
```
|
||||
⚠️ TRADE BLOCKED
|
||||
|
||||
[raw message]
|
||||
|
||||
🛑 Risk limits exceeded
|
||||
⏰ [time]
|
||||
```
|
||||
- **Status**: ✅ Working (uses proven Edit Fields)
|
||||
|
||||
### 10-12. Telegram Nodes ✓
|
||||
- **Type**: `n8n-nodes-base.telegram` (v1.1) - Same as working trader workflow
|
||||
- **Chat ID**: 579304651
|
||||
- **Credential**: Using existing "Telegram account" credential
|
||||
- **Status**: ✅ Working (same config as proven workflow)
|
||||
|
||||
## Workflow Flow Verification ✓
|
||||
|
||||
```
|
||||
Webhook
|
||||
↓
|
||||
Parse Signal (Edit Fields)
|
||||
↓
|
||||
Check Risk (HTTP POST) ← API tested ✅
|
||||
↓
|
||||
Risk Passed? (IF condition)
|
||||
↓ YES ↓ NO
|
||||
Execute Trade Format Risk
|
||||
(HTTP POST) ↓
|
||||
↓ Telegram Risk
|
||||
Trade Success?
|
||||
↓ YES ↓ NO
|
||||
Format Format
|
||||
Success Error
|
||||
↓ ↓
|
||||
Telegram Telegram
|
||||
Success Error
|
||||
```
|
||||
|
||||
## JSON Validation ✓
|
||||
- **Status**: ✅ Valid JSON structure
|
||||
- **File**: `/home/icke/traderv4/n8n-complete-workflow.json`
|
||||
|
||||
## API Endpoints Verified ✓
|
||||
|
||||
### Check Risk Endpoint
|
||||
```bash
|
||||
curl -X POST http://10.0.0.48:3001/api/trading/check-risk \
|
||||
-H "Authorization: Bearer 2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"symbol":"SOL-PERP","direction":"long"}'
|
||||
|
||||
Response: {"allowed":true,"details":"All risk checks passed"}
|
||||
```
|
||||
|
||||
### Execute Trade Endpoint
|
||||
- Endpoint exists and is protected by same auth
|
||||
- Will execute trades when Drift wallet is configured
|
||||
- Returns `success: true/false` with trade details
|
||||
|
||||
## Known Issues: NONE ✅
|
||||
|
||||
All nodes use working, proven node types from your existing n8n-trader-workflow.json
|
||||
|
||||
## Import Instructions
|
||||
|
||||
1. Delete old broken workflow from n8n (if imported)
|
||||
2. Import fresh: `/home/icke/traderv4/n8n-complete-workflow.json`
|
||||
3. Update Telegram credential reference if needed
|
||||
4. Activate workflow
|
||||
5. Test with: `curl -X POST [your-n8n-webhook-url] -H "Content-Type: application/json" -d '{"body":"Buy SOL | Entry: 140.50"}'`
|
||||
|
||||
## Webhook URL Format
|
||||
After activation: `https://[your-n8n-domain]/webhook/tradingview-bot-v4`
|
||||
|
||||
---
|
||||
**VERIFICATION COMPLETE - ALL SYSTEMS GO!**
|
||||
Reference in New Issue
Block a user