Skip to main content

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.raw middleware

2. Data Source Priority

TradingView can send alerts in multiple formats. Priority order:

  1. Query Parameters (req.query.message)

    • Used when TradingView sends {{message}} in URL
    • Location: server.js:1185
  2. Request Body (req.body or req.rawBody)

    • JSON object or JSON string
    • Plain text
    • Location: server.js:1196
  3. 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
  • 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

Message Field Extraction:

  • If data is in message field, extract it first
  • Location: server.js:1226

4. Normalize Alert Data

Extract key fields:

  • action: Buy/Sell → Converted to LONG/SHORT
  • symbol: MES1!, MNQ1!, etc.
  • entryPrice, sl (stop loss), tp (take profit)
  • indicator: Indicator name
  • user_id: User identifier
  • leverage: Contract leverage

5. Flux Charts Detection

Location: server.js:1299

Check if alert is from Flux Charts premium indicator:

  • FLUX CHARTS SFX
  • FLUX CHARTS PREMIUM
  • Explicit isFluxTest flag

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):

  1. Match trade by symbol
  2. Find open trade in database
  3. Update trade status:
    • result: TP/SL/Session End
    • exit_price: Exit price from alert
    • pnl: Calculate PnL

7. Entry Alert Processing

Location: server.js:1486

  1. Validate User ID:

    • Extract user_id from payload
    • If missing → Return 400 error
  2. 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'
  3. For Each User:

    • Check if live trading enabled
    • If enabled → Create alert record
    • If disabled → Skip user
  4. Create Alert Record:

    INSERT INTO alerts (user_id, symbol, action, entry_price, ...)
    VALUES ($1, $2, $3, $4, ...)
    • Store raw data in raw_data JSONB field for debugging
    • Location: server.js:1565
  5. Continue to Live Entry Flow:

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_data field for debugging
  • Alert record is still created with _raw flag
  • 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