TradingView is where most retail traders build and test their strategies. But TradingView itself doesn't execute trades — it fires alerts. The gap between an alert and a live order is where automation lives, and bridging it correctly is what separates paper trading from a real algo.
This guide covers the complete pipeline: from alert message format inside Pine Script, to webhook receiver setup, to broker API order placement. We'll cover NSE (Zerodha/Upstox/Angel), crypto (Binance/Bybit), and forex (MetaTrader-compatible brokers).
How TradingView Webhooks Work
When a TradingView alert fires, it can send an HTTP POST request to a URL you specify — this is the webhook. The body of that request is a JSON payload you define in your alert message. Your server receives the payload, parses it, and places the appropriate order via your broker's API.
The architecture looks like this:
TradingView Alert
↓ HTTP POST (JSON)
Your Webhook Server (VPS/Cloud Function)
↓ Broker API call
Exchange / Broker
↓ Order confirmation
Live Position
Step 1 — Format Your Pine Script Alert Message
Your alert message is a JSON string that your server will parse. Design it to contain everything needed to place the order: action, symbol, quantity, order type, and any additional parameters.
// In your Pine Script strategy
if strategy.position_size == 0 and longCondition
alert('{"action":"BUY","symbol":"NIFTY25JUNFUT","qty":1,"type":"MARKET","price":' + str.tostring(close) + '}', alert.freq_once_per_bar_close)
if strategy.position_size > 0 and exitCondition
alert('{"action":"SELL","symbol":"NIFTY25JUNFUT","qty":1,"type":"MARKET","price":' + str.tostring(close) + '}', alert.freq_once_per_bar_close)
Always use alert.freq_once_per_bar_close to prevent duplicate alerts on the same bar. For intraday strategies on 1m or 5m charts, also add a time filter to avoid overnight positions if your strategy is day-trading only.
Step 2 — Set Up Your Webhook Receiver
You need a publicly accessible URL that TradingView can POST to. Options in order of simplicity:
- Python Flask on a VPS — most control, cheapest long-term (DigitalOcean/Hetzner ~$5/month)
- AWS Lambda + API Gateway — serverless, pay-per-use, excellent for low-frequency strategies
- Middleware platforms — WunderTrading, Alertatron, 3Commas (easiest, but adds subscription cost and trust dependency)
A minimal Python Flask receiver looks like this:
from flask import Flask, request, jsonify
import hmac, hashlib, json
app = Flask(__name__)
SECRET = "your_shared_secret"
@app.route('/webhook', methods=['POST'])
def webhook():
# Verify the request is from TradingView (simple token check)
token = request.args.get('token')
if token != SECRET:
return jsonify({"error": "Unauthorized"}), 401
data = request.get_json()
action = data.get('action')
symbol = data.get('symbol')
qty = data.get('qty')
if action == 'BUY':
place_order(symbol, 'BUY', qty)
elif action == 'SELL':
place_order(symbol, 'SELL', qty)
return jsonify({"status": "ok"}), 200
Step 3 — Connect to Your Broker API
NSE — Zerodha (Kite API)
from kiteconnect import KiteConnect
kite = KiteConnect(api_key="your_api_key")
kite.set_access_token("your_access_token")
def place_order(symbol, action, qty):
kite.place_order(
tradingsymbol=symbol,
exchange=kite.EXCHANGE_NFO,
transaction_type=kite.TRANSACTION_TYPE_BUY if action=="BUY" else kite.TRANSACTION_TYPE_SELL,
quantity=qty,
order_type=kite.ORDER_TYPE_MARKET,
product=kite.PRODUCT_MIS
)
Crypto — Binance
from binance.client import Client
client = Client("api_key", "api_secret")
def place_order(symbol, action, qty):
side = Client.SIDE_BUY if action == "BUY" else Client.SIDE_SELL
client.create_order(
symbol=symbol,
side=side,
type=Client.ORDER_TYPE_MARKET,
quantity=qty
)
Step 4 — Critical Production Safeguards
A webhook receiver that blindly executes every payload it receives is a liability. You must implement:
- Secret token validation — only process requests that include your shared secret in the query string or header
- Idempotency checks — store the last signal and reject duplicates within a short time window
- Position state tracking — don't send a BUY if you're already long; maintain server-side position state
- Max position size limits — hard-code a maximum notional exposure that cannot be exceeded regardless of what the webhook says
- Logging — log every incoming webhook payload and every API response to a persistent store
Step 5 — Test Before You Go Live
Every broker with a good API offers a paper trading or sandbox environment. Use it. Paper trade your webhook integration for a minimum of 2 weeks, comparing the paper fills to what your TradingView strategy reports. Only go live once fill prices match within acceptable slippage tolerance.
If the above feels complex, that's because it is — when done properly. We handle the full pipeline: Pine Script alert formatting, webhook server setup, broker API integration, and production safeguards. Book a free call to discuss your setup.