· 10 min read

How to Automate TradingView Alerts to Any Broker

Step-by-step: connecting TradingView webhook alerts to live broker execution across NSE, crypto, and forex markets — without writing a trading engine from scratch.

Bhavin Javia
Bhavin Javia
Founder, BotJockie · 22+ yrs software · 5+ yrs algo dev

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

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:

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:

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.

BotJockie builds this for you

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.