Fix P&L calculation and signal flip detection
- Fix external closure P&L using tp1Hit flag instead of currentSize - Add direction change detection to prevent false TP1 on signal flips - Signal flips now recorded with accurate P&L as 'manual' exits - Add retry logic with exponential backoff for Solana RPC rate limits - Create /api/trading/cancel-orders endpoint for manual cleanup - Improves data integrity for win/loss statistics
This commit is contained in:
191
N8N_MARKET_DATA_SETUP.md
Normal file
191
N8N_MARKET_DATA_SETUP.md
Normal file
@@ -0,0 +1,191 @@
|
||||
# How to Add Market Data Handler to Your n8n Workflow
|
||||
|
||||
## 🎯 Goal
|
||||
Add logic to detect market data alerts and forward them to your bot, while keeping trading signals working normally.
|
||||
|
||||
---
|
||||
|
||||
## 📥 Method 1: Import the Pre-Built Nodes (Easier)
|
||||
|
||||
### Step 1: Download the File
|
||||
The file is saved at: `/home/icke/traderv4/workflows/trading/market_data_handler.json`
|
||||
|
||||
### Step 2: Import into n8n
|
||||
1. Open your **Money Machine** workflow in n8n
|
||||
2. Click the **"⋮"** (three dots) menu at the top
|
||||
3. Select **"Import from File"**
|
||||
4. Upload `market_data_handler.json`
|
||||
5. This will add the nodes to your canvas
|
||||
|
||||
### Step 3: Connect to Your Existing Flow
|
||||
The imported nodes include:
|
||||
- **Webhook** (same as your existing one)
|
||||
- **Is Market Data?** (new IF node to check if it's market data)
|
||||
- **Forward to Bot** (HTTP Request to your bot)
|
||||
- **Respond Success** (sends 200 OK back)
|
||||
- **Parse Trading Signal** (your existing logic for trading signals)
|
||||
|
||||
You'll need to:
|
||||
1. **Delete** the duplicate Webhook node (keep your existing one)
|
||||
2. **Connect** your existing Webhook → **Is Market Data?** node
|
||||
3. The rest should flow automatically
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Method 2: Add Manually (Step-by-Step)
|
||||
|
||||
If import doesn't work, add these nodes manually:
|
||||
|
||||
### Step 1: Add "IF" Node After Webhook
|
||||
|
||||
1. Click on the canvas in your Money Machine workflow
|
||||
2. **Add node** → Search for **"IF"**
|
||||
3. **Place it** right after your "Webhook" node
|
||||
4. **Connect:** Webhook → IF node
|
||||
|
||||
### Step 2: Configure the IF Node
|
||||
|
||||
**Name:** `Is Market Data?`
|
||||
|
||||
**Condition:**
|
||||
- **Value 1:** `={{ $json.body.action }}`
|
||||
- **Operation:** equals
|
||||
- **Value 2:** `market_data`
|
||||
|
||||
This checks if the incoming alert has `"action": "market_data"` in the JSON.
|
||||
|
||||
### Step 3: Add HTTP Request Node (True Branch)
|
||||
|
||||
When condition is TRUE (it IS market data):
|
||||
|
||||
1. **Add node** → **"HTTP Request"**
|
||||
2. **Connect** from the **TRUE** output of the IF node
|
||||
3. **Configure:**
|
||||
- **Name:** `Forward to Bot`
|
||||
- **Method:** POST
|
||||
- **URL:** `http://trading-bot-v4:3000/api/trading/market-data`
|
||||
- **Send Body:** Yes ✅
|
||||
- **Body Content Type:** JSON
|
||||
- **JSON Body:** `={{ $json.body }}`
|
||||
|
||||
### Step 4: Add Respond to Webhook (After HTTP Request)
|
||||
|
||||
1. **Add node** → **"Respond to Webhook"**
|
||||
2. **Connect** from HTTP Request node
|
||||
3. **Configure:**
|
||||
- **Response Code:** 200
|
||||
- **Response Body:** `{"success": true, "cached": true}`
|
||||
|
||||
### Step 5: Connect False Branch to Your Existing Flow
|
||||
|
||||
From the **FALSE** output of the IF node (NOT market data):
|
||||
|
||||
1. **Connect** to your existing **"Parse Signal Enhanced"** node
|
||||
2. This is where your normal trading signals flow
|
||||
|
||||
---
|
||||
|
||||
## 📊 Final Flow Diagram
|
||||
|
||||
```
|
||||
Webhook (receives all TradingView alerts)
|
||||
↓
|
||||
Is Market Data? (IF node)
|
||||
↓ ↓
|
||||
TRUE FALSE
|
||||
↓ ↓
|
||||
Forward to Bot Parse Signal Enhanced
|
||||
↓ ↓
|
||||
Respond Success (your existing trading flow...)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ Testing
|
||||
|
||||
### Step 1: Update TradingView Alert
|
||||
|
||||
Change your market data alert webhook URL to:
|
||||
```
|
||||
https://flow.egonetix.de/webhook/tradingview-bot-v4
|
||||
```
|
||||
|
||||
(This is your MAIN webhook that's already working)
|
||||
|
||||
### Step 2: Wait 5 Minutes
|
||||
|
||||
Wait for the next bar close (5 minutes max).
|
||||
|
||||
### Step 3: Check n8n Executions
|
||||
|
||||
1. Click **"Executions"** tab in n8n
|
||||
2. You should see executions showing:
|
||||
- Webhook triggered
|
||||
- IS Market Data? = TRUE
|
||||
- Forward to Bot = Success
|
||||
|
||||
### Step 4: Verify Bot Cache
|
||||
|
||||
```bash
|
||||
curl http://localhost:3001/api/trading/market-data
|
||||
```
|
||||
|
||||
Should show:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"availableSymbols": ["SOL-PERP"],
|
||||
"count": 1,
|
||||
"cache": {
|
||||
"SOL-PERP": {
|
||||
"atr": 0.26,
|
||||
"adx": 15.4,
|
||||
"rsi": 47.3,
|
||||
...
|
||||
"ageSeconds": 23
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐛 Troubleshooting
|
||||
|
||||
**Problem: IF node always goes to FALSE**
|
||||
|
||||
Check the condition syntax:
|
||||
- Make sure it's `={{ $json.body.action }}` (with double equals and curly braces)
|
||||
- NOT `{ $json.body.action }` (single braces won't work)
|
||||
|
||||
**Problem: HTTP Request fails**
|
||||
|
||||
- Check URL is `http://trading-bot-v4:3000/api/trading/market-data`
|
||||
- NOT `http://10.0.0.48:3001/...` (use Docker internal network)
|
||||
- Make sure body is `={{ $json.body }}` to forward the entire JSON
|
||||
|
||||
**Problem: Still getting empty cache**
|
||||
|
||||
- Check n8n Executions tab to see if workflow is running
|
||||
- Look for errors in the execution log
|
||||
- Verify your TradingView alert is using the correct webhook URL
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Summary
|
||||
|
||||
**What this does:**
|
||||
1. ✅ All TradingView alerts go to same webhook
|
||||
2. ✅ Market data alerts (with `"action": "market_data"`) → Forward to bot cache
|
||||
3. ✅ Trading signals (without `"action": "market_data"`) → Normal trading flow
|
||||
4. ✅ No need for separate webhooks
|
||||
5. ✅ Uses your existing working webhook infrastructure
|
||||
|
||||
**After setup:**
|
||||
- Trading signals continue to work normally
|
||||
- Market data flows to bot cache every 5 minutes
|
||||
- Manual Telegram trades get fresh data
|
||||
|
||||
---
|
||||
|
||||
**Import the JSON file or add the nodes manually, then test!** 🚀
|
||||
Reference in New Issue
Block a user