TradingView Integration Examples
How to send TradingView alerts to an OptAlgo bot.
Webhook URL
In the TradingView alert, open Notifications, enable Webhook URL and enter:
https://api.optalgo.com/signal-listener/signal
Older versions of these docs used https://api.optalgo.com/signal, which does not exist. Alerts sent there never reach OptAlgo. Update them to the address above.
Rules for alert messages
- Paste your bot's real strategy key.
{{strategy_key}}is not a TradingView placeholder; TradingView would send the text as it is, and OptAlgo would reject the key. - Always include
exchangeandticker, on exits too. For a single-symbol bot,tickermust be the bot's symbol.{{ticker}}gives the chart's symbol, including the.Psuffix on perpetual futures charts (for exampleBTCUSDT.P). - Use upper case for
position_sideandaction(LONG,BUY). TradingView's{{strategy.order.action}}produces lower case (buy) and is rejected asaction. Use it only in free text such asreason. - Futures entries need
leverage. - Leave out
trade_idunless you need several trades per bot. If you use it, it must be identical on the entry and its exit, so never build it from{{timenow}}. - Set the alert to fire once per bar close to avoid duplicate signals. Every delivery is processed.
Single-symbol bot: one alert per action
Create one alert per action, each with its own message.
Long entry
{
"strategy_key": "PASTE_YOUR_STRATEGY_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "LONG",
"action": "BUY",
"leverage": 5,
"entry_price": {{close}},
"stop_loss_price": {{plot("Long_SL")}},
"take_profit_price": {{plot("Long_TP")}}
}
Short entry
{
"strategy_key": "PASTE_YOUR_STRATEGY_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "SHORT",
"action": "SELL",
"leverage": 5,
"entry_price": {{close}},
"stop_loss_price": {{plot("Short_SL")}},
"take_profit_price": {{plot("Short_TP")}}
}
{{plot("Long_SL")}} reads the value of the plot with that title in your script. Remove the stop-loss and take-profit lines if your script has no such plots.
Close
{
"strategy_key": "PASTE_YOUR_STRATEGY_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "CLOSE",
"reason": "Exit signal from TradingView"
}
Close half
{
"strategy_key": "PASTE_YOUR_STRATEGY_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "CLOSE",
"close_ratio": 0.5
}
Move stop loss
MOVE_STOP_LOSS replaces both the stop-loss and the take-profit, so send both levels:
{
"strategy_key": "PASTE_YOUR_STRATEGY_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "MOVE_STOP_LOSS",
"stop_loss_price": {{plot("Trailing_SL")}},
"take_profit_price": {{plot("Long_TP")}}
}
Single-symbol bot: one alert for a strategy script
With a Pine strategy, you can let the script build the message for each order. Pass the JSON as alert_message on every strategy.entry / strategy.close call, then create one alert on the strategy whose message is only:
{{strategy.order.alert_message}}
//@version=5
strategy("OptAlgo strategy", overlay=true)
strategyKey = input.string("", "OptAlgo strategy key")
exchangeName = input.string("BINANCE", "Exchange")
leverage = input.int(5, "Leverage")
fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 30)
atrValue = ta.atr(14)
base = '{"strategy_key":"' + strategyKey + '","exchange":"' + exchangeName + '","ticker":"' + syminfo.ticker + '"'
longMsg = base + ',"position_side":"LONG","action":"BUY","leverage":' + str.tostring(leverage) + ',"entry_price":' + str.tostring(close) + ',"stop_loss_price":' + str.tostring(close - 2 * atrValue) + '}'
closeMsg = base + ',"position_side":"CLOSE"}'
if ta.crossover(fastMA, slowMA)
strategy.entry("Long", strategy.long, alert_message=longMsg)
if ta.crossunder(fastMA, slowMA)
strategy.close("Long", alert_message=closeMsg)
syminfo.ticker gives the chart's symbol, for example BTCUSDT.P on a Binance perpetual chart.
Multi-symbol bot
A multi-symbol bot uses one key for every coin. Use the same message on every chart, with {{ticker}} (or syminfo.ticker in Pine), and TradingView fills in each chart's symbol:
{
"strategy_key": "PASTE_YOUR_MULTI_SYMBOL_BOT_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "LONG",
"action": "BUY",
"leverage": 3,
"tradable_ratio": 1
}
{
"strategy_key": "PASTE_YOUR_MULTI_SYMBOL_BOT_KEY",
"exchange": "BINANCE",
"ticker": "{{ticker}}",
"position_side": "CLOSE"
}
- Every coin must be on the bot's exchange, market type and quote currency, for example all
...USDT.Pfutures on Binance. - Each entry is sized on one slot (budget divided by max positions).
tradable_ratioscales inside the slot and is capped at 1. - A close or partial close acts only on the coin in
ticker. - When all slots are taken, new coins are rejected until one closes.
Setting up the alert
- Create the alert on the chart: select your strategy or indicator and the condition.
- Message: paste one of the JSON messages above, with your real strategy key.
- Notifications: enable Webhook URL and enter
https://api.optalgo.com/signal-listener/signal. - Options: "Once Per Bar Close" is recommended.
- Check: after the alert fires, open the bot's logs in OptAlgo. The webhook answers
204even when a signal is rejected, so the logs are where you see the result.
Optional fields
{
"reason": "TradingView {{strategy.order.action}} signal",
"pinescript_code_version": "opt_v6"
}
reason is free text that is shown in the logs. A reason containing "error" or "warning" also sends you a notification.