Here is the code for that:
input numberOfDays = 63;
input maLengthOne = 8;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
input maLengthTwo = 21;
input maTypeTwo = AverageType.EXPONENTIAL;
input maPriceTwo = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def maTwo = MovingAverage(maTypeTwo, maPriceTwo, maLengthTwo);
def undesirableCondition = maOne > maTwo;
plot scan = Highest(undesirableCondition, numberOfDays) < 1;
Be sure to apply this to a daily time frame. I'll take a few minutes to explain some of this.
- User input numberOfDays defaults to 63 because on average there are 21 trading days in any giving month
- undesriableCondition is the fast ema above the slow ema (the condition we do NOT want to appear in the previous x number of bars). Its either a 1 (fast ema above slow ema) or a zero (fast ema below the slow ema).
- The scan signal checks for the highest value of that undesirable condition in the previous x number of bars. So as long as highest value for that number of bars is less than 1, we know that the undesirable condition has not been true during that span of bars.