Webhook Reception Flow
Overview
This flow shows how TradingView alerts are received via webhook, parsed from multiple formats, and routed to the appropriate handler for processing.
Webhook Reception
1. Receive Webhook
Endpoint: POST /webhook/alert
Location: server.js:1154
- Webhooks do NOT require authentication (TradingView needs access)
- Raw body is captured by middleware for flexible parsing
- Location:
server.js:86-express.rawmiddleware
2. Data Source Priority
TradingView can send alerts in multiple formats. Priority order:
-
Query Parameters (
req.query.message)- Used when TradingView sends
{{message}}in URL - Location:
server.js:1185
- Used when TradingView sends
-
Request Body (
req.bodyorreq.rawBody)- JSON object or JSON string
- Plain text
- Location:
server.js:1196
-
Headers (
x-alert-message)- Fallback if body is empty
- Location:
server.js:1203
3. Parse Alert Data
Multiple Format Support:
-
JSON String: Parse with
JSON.parse()- Location:
server.js:1216
- Location:
-
Plain Text: Parse with
parseAlertText()function- Extracts key-value pairs from text
- Location:
server.js:1242
-
Object: Use directly if already parsed
- Location:
server.js:1254
- Location:
Message Field Extraction:
- If data is in
messagefield, extract it first - Location:
server.js:1226
4. Normalize Alert Data
Extract key fields:
action: Buy/Sell → Converted to LONG/SHORTsymbol: MES1!, MNQ1!, etc.entryPrice,sl(stop loss),tp(take profit)indicator: Indicator nameuser_id: User identifierleverage: Contract leverage
5. Flux Charts Detection
Location: server.js:1299
Check if alert is from Flux Charts premium indicator:
FLUX CHARTS SFXFLUX CHARTS PREMIUM- Explicit
isFluxTestflag
Routing:
- If Flux Charts → Route to test handler (
isFluxTest = true) - Otherwise → Route to live trading handler
- Location:
server.js:1346
6. Result Alert Processing
Location: server.js:1350
If alert contains result field (TP/SL/Exit):
- Match trade by symbol
- Find open trade in database
- Update trade status:
result: TP/SL/Session Endexit_price: Exit price from alertpnl: Calculate PnL
7. Entry Alert Processing
Location: server.js:1486
-
Validate User ID:
- Extract
user_idfrom payload - If missing → Return 400 error
- Extract
-
Query Users with Live Trading:
SELECT user_id, live_trading_settings
FROM users u
JOIN trading_settings ts ON u.user_id = ts.user_id
WHERE ts.setting_key = 'live_trading' -
For Each User:
- Check if live trading enabled
- If enabled → Create alert record
- If disabled → Skip user
-
Create Alert Record:
INSERT INTO alerts (user_id, symbol, action, entry_price, ...)
VALUES ($1, $2, $3, $4, ...)- Store raw data in
raw_dataJSONB field for debugging - Location:
server.js:1565
- Store raw data in
-
Continue to Live Entry Flow:
- See 01-live-entry.md for order placement
Supported Alert Formats
Format 1: JSON Object
{
"action": "Buy",
"symbol": "MES1!",
"entryPrice": 5000.00,
"sl": 4995.00,
"tp": 5010.00,
"user_id": "123456789"
}
Format 2: JSON String in Message
{
"message": "{\"action\":\"Buy\",\"symbol\":\"MES1!\",...}"
}
Format 3: Query Parameter
POST /webhook/alert?message={"action":"Buy","symbol":"MES1!",...}
Format 4: Plain Text
Action: Buy
Symbol: MES1!
Entry Price: 5000.00
SL: 4995.00
TP: 5010.00
Error Handling
Missing User ID
- Error: 400 Bad Request
- Message: "user_id required"
- Alert is not processed
No Users with Live Trading
- Response: 200 OK (successful webhook receipt)
- Log: "No users with live trading enabled"
- Alert is logged but not processed
Parsing Failures
- Raw data is stored in
raw_datafield for debugging - Alert record is still created with
_rawflag - Allows manual review and debugging
Security Notes
- Webhooks do NOT require authentication (by design)
- User ID validation ensures alerts only go to valid users
- Raw data storage helps debug malformed alerts
- Flux Charts detection prevents test trades from going live
Integration Points
- Live Entry Flow: Continues to order placement (01-live-entry.md)
- Result Processing: Updates existing trades directly
- User Management: Queries users with live trading enabled
- Settings: Checks live trading settings per user