# Writing an alert

> The condition, the message, and when it may repeat.

Source: https://algobarsx.com/docs/writing-an-alert/

```algobarsx
alert "RSI bullish divergence"
    check: every 15m
    repeat: once per bar
    show_on_chart: true

input rsi_length = 14
input lookback = 30

momentum_now = rsi(close, rsi_length)
lower_low = low < lowest(low, lookback)[1]
higher_rsi = momentum_now > lowest(momentum_now, lookback)[1]

when lower_low and higher_rsi and was(momentum_now < 30, within: 5 bars):
    notify "Bullish RSI divergence on {market.symbol}: RSI {momentum_now:0.0}"
```

| Setting | What it does |
| --- | --- |
| `check` | How often the condition is checked, such as `every 1m`. |
| `repeat` | `once`, `once per bar` (the default), `once per bar close` or `every time`. |
| `cooldown` | The shortest gap between two messages. |
| `expires` | When the alert stops watching. |
| `show_on_chart` | Marks the chart where it fired. |

[`notify`](https://algobarsx.com/docs/ref-cmd-alerts/#ref-notify) sends the message. In version 1 it is delivered in the app and in AI chat. Testing an alert shows you the messages it would have sent and the ones its own settings held back.

## Alerts that watch the account, not the chart

```algobarsx
alert "Margin and exposure"
    check: every 5m
    repeat: once
    expires: 30d

when account.free_margin < 20% of account.equity or positions.len >= 8:
    notify "Free margin {account.free_margin:$0} with {positions.len} open positions"
```

More: [Daily risk guard](https://algobarsx.com/docs/ex-06-daily-risk-guard/), [Intrabar spike](https://algobarsx.com/docs/ex-41-intrabar-spike-alert/), [Tokyo open gap](https://algobarsx.com/docs/ex-45-tokyo-open-gap-alert/), [Distribution shift](https://algobarsx.com/docs/ex-50-distribution-shift-alert/).
