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.
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.crossedUp is a bool for every bar and nothing reads it yet.see the condition on the chart
Before alerting on a condition, look at it. 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.
plotshape takes a condition as its first argument and marks the bars where it holds.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: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.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.