Skip to main content
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

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.
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.
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:
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.

alert and alertcondition side by side

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

Alerts

the contract of both alert functions.

Webhooks for algo trading

where a raised alert can be delivered.

Text and markers

the shapes, sizes and positions a mark can take.