- 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
4.9 KiB
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
- Open your Money Machine workflow in n8n
- Click the "⋮" (three dots) menu at the top
- Select "Import from File"
- Upload
market_data_handler.json - 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:
- Delete the duplicate Webhook node (keep your existing one)
- Connect your existing Webhook → Is Market Data? node
- 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
- Click on the canvas in your Money Machine workflow
- Add node → Search for "IF"
- Place it right after your "Webhook" node
- 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):
- Add node → "HTTP Request"
- Connect from the TRUE output of the IF node
- 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 }}
- Name:
Step 4: Add Respond to Webhook (After HTTP Request)
- Add node → "Respond to Webhook"
- Connect from HTTP Request node
- 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):
- Connect to your existing "Parse Signal Enhanced" node
- 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
- Click "Executions" tab in n8n
- You should see executions showing:
- Webhook triggered
- IS Market Data? = TRUE
- Forward to Bot = Success
Step 4: Verify Bot Cache
curl http://localhost:3001/api/trading/market-data
Should show:
{
"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:
- ✅ All TradingView alerts go to same webhook
- ✅ Market data alerts (with
"action": "market_data") → Forward to bot cache - ✅ Trading signals (without
"action": "market_data") → Normal trading flow - ✅ No need for separate webhooks
- ✅ 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! 🚀