Alert for first bar of stacked moving averages


0
0

Hello, was wondering if it’s possible to have a script when you’re alerted when, for instance the 10 ema goes above the 15 ema but only show triggered when it first occurs and not any preceding days, even if the condition is still true.

I know I could do 10 ema crosses above 15 ema but I’m doing multiple ema’s and they dont all cross at the same time. I am wanting the notification only when it first occurs.

 

Thanks for any help

Marked as spam
Posted by (Questions: 11, Answers: 16)
Asked on June 27, 2020 8:47 pm
136 views
0
To clarify not necessarily when they cross but when all conditions are true and want the notificantion turned off on preceding days, even if still true 10 > 15 8 > 10 5 > 8....etc Thanks
( at June 27, 2020 8:54 pm)
0
Private answer

You posted this in the "Watchlist" topic but you are requesting an alert. So I moved this out of the "Watchlist" topic and into the "Alerts and Notifications" topic. I also updated the title of your question because folks searching for this solution would never find it with your original title: "MA Alert only when conditions are first met"

Your request lacked any mention of whether you wanted to plot these signals on a chart so the code below is used for a Study Filter of a custom scan or for the Study Alert.

You mentioned several different moving averages but you did not provide a complete list. So this solution is based on the bare minimum of three moving averages. You can add to this as needed once you understand the logic. The two plot statements at the bottom can only be used one at a time. So move the "#" symbol to the one you want to turn off.

User inputs are provided so that you can independently adjust any of the moving averages to whatever length, price or type you require. The default values are 8, 10, and 15 period Exponential moving averages of the Close.

input maLengthOne = 8;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
input maLengthTwo = 10;
input maTypeTwo = AverageType.EXPONENTIAL;
input maPriceTwo = close;
input maLengthThree = 15;
input maTypeThree = AverageType.EXPONENTIAL;
input maPriceThree = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def maTwo = MovingAverage(maTypeTwo, maPriceTwo, maLengthTwo);
def maThree = MovingAverage(maTypeThree, maPriceThree, maLengthThree);
def movingAveragesStackedAbove = maOne > maTwo and maTwo > maThree;
def movingAveragesStackedBelow = maOne < maTwo and maTwo < maThree;
# use this to scan/alert when first bar shows moving averages stacked above
plot scan = movingAveragesStackedAbove and !movingAveragesStackedAbove[1];
# use this to scan/alert when first bar shows moving averages stacked below
#plot scan = movingAveragesStackedBelow and !movingAveragesStackedBelow[1];

The code uses a two staged approach. Stage one simply checks if the moving averages are stacked in the correct order:

def movingAveragesStackedAbove = maOne > maTwo and maTwo > maThree;

Stage two then checks if stage one is true for the current bar while false for the previous bar:

plot scan = movingAveragesStackedAbove and !movingAveragesStackedAbove[1];

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 28, 2020 8:30 am