'Pine script alarm

I am having problem with setting up alerts on an indicator with buy, sell, short, cover lables.

The condition for the signals is a when in trend. But at the moment I get a new alert for every candle my script is in trend. And obviously I only want it on the first bar in trend.

How can I set up my alert() or alertcondition() so it only will trigger once?



Solution 1:[1]

You need to have a variable to check if the event is changed.

For example, say you want to trigger an alert when the trend direction changes.

uptrend = ... // Your definition of the uptrend
downtrend = ... // Your definition of the downtrend

is_new_uptrend = not uptrend[1] and uptrend  // It was not an uptrend one bar ago AND it is an uptrend now
is_new_downtrned = not downtrend[1] and downtrend  // It was not a downtrend one bar ago AND it is a downtrend now

alertcondition(is_new_uptrend, "New uptrend")
alertcondition(is_new_downtrend, "New downtrend")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius