TradingView Strategy & Webhooks

Connect your TradingView Pine Script strategies to your automated trading backend using secure webhooks.

1. Strategy Configuration

To automate trades, your Pine Script strategy must generate alerts. Use the `alert()` function or the UI-based "Add Alert" dialog. The key is to send a structured JSON payload that your backend can parse.

Keep alerts single-purpose: ONE alert for entry, ONE for exit (or use strategy.entry/strategy.close specific alerts).

Webhook Payload Structure

{
  "alert_id": "{{strategy.order.id}}",
  "symbol": "{{ticker}}",
  "side": "{{strategy.order.action}}",   // BUY or SELL
  "qty": "{{strategy.order.contracts}}",
  "price": "{{close}}",
  "signature": "{{strategy.order.comment}}" // precomputed HMAC
}

Key Fields

  • alert_id: Use `{{strategy.order.id}}` to uniquely identify the trade.
  • symbol: `{{ticker}}` usually sends "NIFTY", "RELIANCE", etc. You may need to map this to "NIFTY24FEBFUT" in your backend.
  • side: "buy" or "sell". Case-insensitive handling recommended.
  • qty: Number of contracts/shares. Ensure this matches your risk management rules.
  • signature: A security token or HMAC signature to verify the alert came from TradingView.

2. Setting Up the Alert

In TradingView:

  1. Open your Strategy Tester panel.
  2. Click the "Add Alert" icon (clock).
  3. Condition: Select your strategy.
  4. Message: Paste the JSON payload from above.
  5. Webhook URL: Enter your server's endpoint (e.g., `https://api.yourdomain.com/webhook/tradingview`).

3. Troubleshooting & Common Issues

Even with perfect code, alerts can sometimes fail or behave unexpectedly. Here is how to diagnose common problems.

Why didn't my alert fire?

  • Repainting Strategies: If your strategy uses `calc_on_every_tick=true` or look-ahead bias (referencing future bars), the backtest result might not match real-time behavior. Ensure your strategy logic relies on confirmed closed candles (`barstate.isconfirmed`).
  • Webhook Log: Check the "Alerts Log" in TradingView. If it shows "Failed: 404", your URL is wrong. If "Failed: 500", your server crashed. If "Success" but no order, your server logic rejected it (check your server logs).
  • Subscription Level: Free TradingView plans have limitations on the number of active webhooks and alert speed (e.g., once per bar vs. every tick).

Security Best Practices

  • Signature Verification: Always verify the `signature` field. Without it, anyone who guesses your URL can trigger fake trades on your account.
  • IP Allowlisting: TradingView publishes a list of IP addresses they send alerts from. Configure your firewall (AWS Security Group / DigitalOcean UFW) to only accept POST requests from these IPs.

4. Frequently Asked Questions (FAQ)

Q: Can I trade Options with this setup?

Yes. You just need to send the correct symbol name (e.g., "NIFTY24DEC19000CE") in the payload. Many users calculate the ATM strike dynamically in their Python code based on the spot price received from the alert.

Q: What is the latency?

TradingView -> Webhook is usually < 1 second. Webhook -> Zerodha API is approx 200-500ms. Total latency is typically under 1.5 seconds, which is sufficient for non-HFT retail algo trading.

Q: Do I need a VPS?

For reliability, yes. A cloud server (AWS EC2 free tier, DigitalOcean Droplet, or Google Cloud Run) ensures your bot is always running, unlike a laptop which might sleep or lose internet.