How to Connect TradingView Alerts to a Broker

This is the most common way traders automate a Pine Script strategy. It works, but it adds a server you must keep running and secure.

Last verified

Quick answer

TradingView has no trading API, so connect it with webhooks: create an alert with a JSON message, point its webhook URL at an HTTPS server you control, verify a shared secret, and have that server call your broker’s API (for example Alpaca, OANDA or Binance). Webhooks need a paid TradingView plan and should be handled idempotently.

Side-by-side comparison

APIMarketsAuthPaper / sandboxPricingIn AlgoGP
TradingViewStocks, Crypto, Forex, FuturesNo public API—Webhooks need a paid planGuide
AlpacaStocks, ETFs, CryptoREST + API keyPaper trading account with simulated fillsFree account; free IEX market data planBuilt into AlgoGP
OANDAForex, CFDs, IndicesREST + API keyFree demo (fxPractice) accountFree API with an accountBuilt into AlgoGPPro
BinanceCryptoREST + API keySpot testnet with test fundsFree API; trading fees applyBuilt into AlgoGP
TradeStationStocks, ETFs, Options, FuturesOAuth loginSimulated (SIM) trading accountAPI free; funded account needed for a keyGuide

Which one should you pick?

A minimal webhook relay

from flask import Flask, request, abort
import os, requests
app = Flask(__name__)

@app.post("/tv")
def tv():
    alert = request.get_json(force=True)
    if alert.get("secret") != os.environ["TV_SECRET"]:
        abort(403)
    requests.post("https://paper-api.alpaca.markets/v2/orders", json={
        "symbol": alert["symbol"], "qty": alert.get("qty", 1),
        "side": alert["side"], "type": "market", "time_in_force": "day",
    }, headers={"APCA-API-KEY-ID": os.environ["ALPACA_API_KEY_ID"],
                "APCA-API-SECRET-KEY": os.environ["ALPACA_API_SECRET_KEY"]}, timeout=10)
    return "ok"
Flask relay forwarding a TradingView alert to Alpaca paper trading.

Frequently asked questions

Can TradingView send orders directly to my broker?

Only for brokers integrated into TradingView’s trading panel, and that is manual trading. Automation uses webhooks to your own server.

Test the strategy before you pick the API

Describe your idea to AlgoGP’s copilot, backtest it on years of stock, crypto or forex data with realistic costs, then export Python wired to Alpaca or any built-in broker. Swap the broker later without touching the strategy.

More API comparisons

AlgoGP is not affiliated with any provider listed. Details change; check each provider’s documentation. Nothing here is investment advice.