Pine Script examples
Ready-to-use Pine Script snippets using the exact webhook fields accepted by TheConnector.
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.
Market buy with SL / TP
{"action":"buy","symbol":"XAUUSD","risk":1,"sl":2325.50,"tp":2352.00,"source":"TradingView","id":"tv-xauusd-001","account":"MY_ACCOUNT","platform":"MT5"}
//@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)
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"}
Close a position
{"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.
Configure the alert in TradingView
Retrieve your webhook URL in your TheConnector workspace and paste it into the Webhook URL field when creating a TradingView alert.
The message must be a valid JSON object. TradingView supports dynamic variables such as {{ticker}}, {{close}} and {{time}}.