Start free
Resources

Pine Script examples

Ready-to-use Pine Script snippets using the exact webhook fields accepted by TheConnector.

Principle

How to send a webhook from TradingView

In TradingView, an alert can trigger an HTTP request to TheConnector. The alert message must be a JSON object with action, symbol, risk or volume, optional SL/TP, and the target account when the webhook URL is shared by several accounts.

The full documentation uses the same field names as this page: action, symbol, risk or volume, sl, tp, price for pending orders, source, id, account/accountCode and platform.

Example 1

Market buy with SL / TP

TradingView alert message
{"action":"buy","symbol":"XAUUSD","risk":1,"sl":2325.50,"tp":2352.00,"source":"TradingView","id":"tv-xauusd-001","account":"MY_ACCOUNT","platform":"MT5"}
Associated Pine script
//@version=5
strategy("TheConnector example", overlay=true)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if longCondition
    strategy.entry("Long", strategy.long)
    atr = ta.atr(14)
    slPrice = close - atr * 2
    tpPrice = close + atr * 3
    message = "{\"action\":\"buy\",\"symbol\":\"" + syminfo.ticker + "\",\"risk\":1,\"sl\":" + str.tostring(slPrice, format.mintick) + ",\"tp\":" + str.tostring(tpPrice, format.mintick) + ",\"source\":\"TradingView\",\"id\":\"" + syminfo.ticker + "-" + str.tostring(time) + "\",\"account\":\"MY_ACCOUNT\",\"platform\":\"MT5\"}"
    alert(message, alert.freq_once_per_bar_close)
Fixed lot alternative

Use volume, lot or lots only when you want to bypass risk sizing and force an exact lot size.

{"action":"buy","symbol":"XAUUSD","volume":"0.10","sl":2325.50,"tp":2352.00,"source":"TradingView","id":"tv-xauusd-lot-001","account":"MY_ACCOUNT","platform":"MT5"}
Example 2

Close a position

Alert message to close long and short positions
{"action":"closelongshort","symbol":"XAUUSD","source":"TradingView","id":"tv-close-xauusd-001","account":"MY_ACCOUNT","platform":"MT5"}

Read the documentation for command aliases: buy/long, sell/short, buystop/buy_stop, buylimit/buy_limit, sellstop/sell_stop, selllimit/sell_limit, closelong/close_long, closeshort/close_short, closelongshort/close_long_short, cancellong/cancel_long, cancelshort/cancel_short, newsltplong/newsltp_long and newsltpshort/newsltp_short.

Configuration

Configure the alert in TradingView

Webhook URL

Retrieve your webhook URL in your TheConnector workspace and paste it into the Webhook URL field when creating a TradingView alert.

Message format

The message must be a valid JSON object. TradingView supports dynamic variables such as {{ticker}}, {{close}} and {{time}}.