> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Alert on your own condition

> Build a condition for a close crossing above a moving average, mark it on the chart, raise it with alert() on a closed bar, and offer it with alertcondition().

In this tutorial we work out where the close crosses above a moving average, mark those bars on the chart, and raise an alert on
them. Lipi has two alert functions, `alert()` and `alertcondition()`, and we use both so that you can see the difference. Every step
is a whole script: replace what is in the editor, and check it.

### 1. draw the average

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cross above the average", "cross", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float avg = talib.sma(close, length)
plot(avg, title="Average", color=color.gray)
```

A grey line is drawn over the price bars, and Length is a setting.

### 2. work out the condition

`talib.crossover(a, b)` is true on the bar where `a` moves from at or below `b` to above it. Here `a` is the close and `b` the average.

```lipi highlight={5} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cross above the average", "cross", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float avg = talib.sma(close, length)
bool crossedUp = talib.crossover(close, avg)
plot(avg, title="Average", color=color.gray)
```

The chart has not changed. `crossedUp` is a `bool` for every bar and nothing reads it yet.

### 3. see the condition on the chart

Before alerting on a condition, look at it. `plotshape` takes a condition as its first argument and marks the bars where it holds.

```lipi wrap highlight={7} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cross above the average", "cross", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float avg = talib.sma(close, length)
bool crossedUp = talib.crossover(close, avg)
plot(avg, title="Average", color=color.gray)
plotshape(crossedUp, title="Cross up", shape=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
```

A green triangle appears below each bar where the close crossed above the grey line. Change Length and the marks move. Do not go on
until the marks are where you expect them: this is the last step with a visible result.

### 4. raise the alert, on a closed bar

`alert(message)` raises an alert from your own logic, so it can sit inside an `if`. The message can be built from values, because it is
a `series string`.

On a bar that is still forming, the script runs again with every tick. The close can be above the average on one tick and below it at
the end of the bar, and `alert()` fires at most once per bar, so an alert raised on the first tick cannot be taken back. Add
`barstate.isconfirmed`, which is true only once the bar has closed (and on historical bars), to alert on the finished bar:

```lipi wrap highlight={6-8} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cross above the average", "cross", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float avg = talib.sma(close, length)
bool crossedUp = talib.crossover(close, avg)
if crossedUp and barstate.isconfirmed {
    alert("Close crossed above the " + str.tostring(length) + "-bar average")
}
plot(avg, title="Average", color=color.gray)
plotshape(crossedUp, title="Cross up", shape=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
```

Check it. The script validates, the marks are still there, and nothing else visibly happens, which is expected: `alert()` does
nothing on historical bars. It acts only on realtime bars and only while the script is running as an alert, and which of those it is
set outside the language. The marks from step 3 show where it would have fired.

### 5. also offer the condition with alertcondition

`alertcondition(condition, title, message)` raises nothing by itself. It declares a condition that appears in the alert settings for
the chart user to pick. Place it at the top level; `title` and `message` are fixed text.

```lipi wrap highlight={9} theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Cross above the average", "cross", overlay=true)
length = input.int(20, "Length", minval=1, maxval=200)
float avg = talib.sma(close, length)
bool crossedUp = talib.crossover(close, avg)
if crossedUp and barstate.isconfirmed {
    alert("Close crossed above the " + str.tostring(length) + "-bar average")
}
alertcondition(crossedUp, "Cross above", "Close crossed above the average")
plot(avg, title="Average", color=color.gray)
plotshape(crossedUp, title="Cross up", shape=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
```

## alert and alertcondition side by side

| | `alert(message)` | `alertcondition(condition, title, message)` |
| - | - | - |
| Where it may be called | Anywhere: top level, `if`, loops, functions | Top level only |
| The message | A `series string`: it can change from bar to bar | `title` and `message` are `const`: fixed text |
| What it does | Raises the alert when it runs, on a realtime bar, at most once per bar | Declares a condition for the chart user to pick; raises nothing itself |

Inside an `if`, `alertcondition` is refused: `` `alertcondition()` must be called in the global scope. `` A message built from a value is
refused too: ``Cannot call `alertcondition` with argument `message`=`"Close is "+str.tostring(close)`. An argument of `series string` type was used but a `const string` is expected.``

## What we built

A moving average, a marker on every cross above it, an alert raised on the closed bar with a message that names the length, and a
condition the chart user can pick in the alert settings.

## Next

> [card] [Alerts](https://gocharting.com/docs/scripting/automation/alerts.md)
>
> the contract of both alert functions.

> [card] [Webhooks for algo trading](https://gocharting.com/docs/scripting/automation/webhooks.md)
>
> where a raised alert can be delivered.

> [card] [Text and markers](https://gocharting.com/docs/scripting/chart-output/text-and-markers.md)
>
> the shapes, sizes and positions a mark can take.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.