# Alert message templates for TradingView

> Four copy-ready Pine Script v6 templates with the matching ZB Connector alert message: crossover entries, a strategy, close-all, and stops.

Source: https://zb-connector.com/docs/tradingview/templates/

Each template below is a small Pine Script v6 script and the alert message it sends to ZB Connector. Copy the script into the Pine Editor, create the alert, replace `YOUR_LICENSE_KEY`, and check the result with the [playground](https://zb-connector.com/docs/tools/playground/).

> **Use a demo account first.** Run every step on a MetaTrader 5 demo account. Switch to a live account only after the go-live checklist is done.

These are automation examples to show how the pieces fit. They are not trading advice and not evidence that any setup is profitable. Change the numbers (moving-average lengths, `0.1` lots) to your own plan, and test on a demo account.

**Verified on 2026-10-05** against TradingView’s Pine Script v6 documentation.

## How every template works

1.  Open TradingView’s **Pine Editor**, paste the script, and add it to the chart.
    
2.  Create an alert (see [create a TradingView alert](https://zb-connector.com/docs/tradingview/alert-basics/)). For an indicator, choose the script and the alert condition named in the template. For the strategy, choose the strategy.
    
3.  Tick **Webhook URL** and enter the address from the alert guide.
    
4.  Check that the **Message** box holds the message shown in the template, with your license key in place of `YOUR_LICENSE_KEY`.
    
    > **What you should see:** The playground says Format is valid for the message. After the alert fires, TradingView’s alert log shows a successful Webhook status and Command History has a row.
    

## 1\. Open long and short on a moving-average cross

An indicator with two alert conditions. When the fast average crosses above the slow one it sends `long`, and when it crosses below it sends `short`.

**Pine Script v6**

```text
//@version=6
indicator("ZB crossover alerts", overlay = true)

fast = ta.sma(close, 14)
slow = ta.sma(close, 28)
plot(fast, "Fast", color = color.teal)
plot(slow, "Slow", color = color.orange)

alertcondition(ta.crossover(fast, slow), "ZB long", '{"license": "YOUR_LICENSE_KEY", "action": "long", "symbol": "{{ticker}}", "volume": 0.1}')
alertcondition(ta.crossunder(fast, slow), "ZB short", '{"license": "YOUR_LICENSE_KEY", "action": "short", "symbol": "{{ticker}}", "volume": 0.1}')
```

Create **two alerts**: one for the condition `ZB long` and one for `ZB short`. TradingView fills the message box with the text from the script, which you can edit. This is the long message:

**Alert message for ZB long**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "long",
  "symbol": "{{ticker}}",
  "volume": 0.1
}
```

-   `{{ticker}}` is replaced by TradingView with the chart’s symbol. It must match your broker’s MT5 symbol exactly.
-   `volume` is in **lots**.
-   Check the EA input **Close on reverse** (options Off, Hedging, Netting; default Hedging) before you go live: it is the EA’s setting for signals that reverse the direction.

## 2\. A strategy that sends its own message

A strategy builds the message in the script and passes it with `alert_message`. In the alert dialog, the message box must contain `{{strategy.order.alert_message}}`.

**Pine Script v6**

```text
//@version=6
strategy("ZB strategy alerts", overlay = true)

fast = ta.sma(close, 14)
slow = ta.sma(close, 28)

buyMessage = '{"license": "YOUR_LICENSE_KEY", "action": "long", "symbol": "' + syminfo.ticker + '", "volume": 0.1, "positionID": "ma_long"}'
sellMessage = '{"license": "YOUR_LICENSE_KEY", "action": "short", "symbol": "' + syminfo.ticker + '", "volume": 0.1, "positionID": "ma_short"}'

if ta.crossover(fast, slow)
    strategy.entry("Buy", strategy.long, alert_message = buyMessage)

if ta.crossunder(fast, slow)
    strategy.entry("Sell", strategy.short, alert_message = sellMessage)
```

**Alert message box**

```text
{{strategy.order.alert_message}}
```

The script sends this message when the fast average crosses above the slow one:

**Message sent for the buy**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "long",
  "symbol": "{{ticker}}",
  "volume": 0.1,
  "positionID": "ma_long"
}
```

-   `syminfo.ticker` is the chart symbol without the exchange prefix. It plays the part of `{{ticker}}` here, because the message is built inside the script.
-   `positionID` is your own name for the position. Later commands such as `closeByID` use it to find the position.
-   Do not use `{{strategy.order.action}}` as `action`: it returns `buy` or `sell`, which are not command names.

## 3\. Close everything when a condition happens

An indicator that sends `closeAll` when the RSI crosses below 50.

**Pine Script v6**

```text
//@version=6
indicator("ZB close-all alert")

r = ta.rsi(close, 20)
plot(r, "RSI")
hline(50)

alertcondition(ta.crossunder(r, 50), "ZB close all", '{"license": "YOUR_LICENSE_KEY", "action": "closeAll"}')
```

**Alert message for ZB close all**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "closeAll"
}
```

`closeAll` closes every position on every symbol in the EA’s trade scope. To close one symbol only, send [closeSymbol](https://zb-connector.com/docs/commands/closesymbol/) with `"symbol": "{{ticker}}"` instead.

## 4\. Stop loss and take profit from your script

An indicator that sends a `long` with a stop loss at the low of the signal bar and a take profit calculated in the script. The take profit travels as a plot, so the message can use `{{plot("TP")}}`.

**Pine Script v6**

```text
//@version=6
indicator("ZB entry with stops", overlay = true)

fast = ta.sma(close, 14)
slow = ta.sma(close, 28)
tpLevel = close + 2 * (close - low)
plot(tpLevel, "TP")

alertcondition(ta.crossover(fast, slow), "ZB long with stops", '{"license": "YOUR_LICENSE_KEY", "action": "long", "symbol": "{{ticker}}", "volume": 0.1, "tp": {{plot("TP")}}, "sl": {{low}}, "positionID": "ma_long"}')
```

**Alert message for ZB long with stops**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "long",
  "symbol": "{{ticker}}",
  "volume": 0.1,
  "tp": {{plot("TP")}},
  "sl": {{low}},
  "positionID": "ma_long"
}
```

-   A message that contains `{{plot("TitleOfPlot")}}` has to be wrapped in **single quotes** in the Pine code, because the placeholder itself contains double quotes. The script above does that.
-   `sl` and `tp` are **price levels** while the EA input **Stop level mode** is price (the default). With pips or ticks they are distances, and these values would be wrong.
-   `{{low}}` is the low of the bar that triggered the alert. For a long, the stop loss must be below the entry price and the take profit above it, or the broker refuses it ([error 10016](https://zb-connector.com/docs/troubleshooting/errors/10016/)).
-   The JSON above is not valid until TradingView has replaced the placeholders. The playground replaces them with sample values before it checks.

## Next step

Paste your own message into the [playground](https://zb-connector.com/docs/tools/playground/), then run a [test alert on a demo account](https://zb-connector.com/docs/get-started/send-a-test-alert/).
