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];