input bars = 1200;
input filterLength = 3;
def length8 = 8;
def length21 = 21;
def length34 = 34;
def length55 = 55;
def length89 = 89;
def length144 = 144;
def data = close;
###################################################
def avg8 = ExpAverage(data, length8);
def avg21 = ExpAverage(data, length21);
def avg34 = ExpAverage(data, length34);
def avg55 = ExpAverage(data, length55);
def avg89 = ExpAverage(data, length89);
def avg144 = ExpAverage(data, length144);
def xBelow8 = close crosses below avg8;
def xBelow21 = close crosses below avg21;
def xBelow34 = close crosses below avg34;
def xBelow55 = close crosses below avg55;
def xBelow89 = close crosses below avg89;
def xBelow144 = close crosses below avg144;
def pctHeld21 = Sum(xBelow21, bars) / Sum(xBelow8, bars);
def pctHeld34 = Sum(xBelow34, bars) / Sum(xBelow21, bars);
def pctHeld55 = Sum(xBelow55, bars) / Sum(xBelow34, bars);
def pctHeld89 = Sum(xBelow89, bars) / Sum(xBelow55, bars);
def pctHeld144 = Sum(xBelow144, bars) / Sum(xBelow89, bars);
def filtered8 = xBelow8 and !(xBelow8[1] within filterLength bars);
def filtered21 = xBelow21 and !(xBelow21[1] within filterLength bars);
def filtered34 = xBelow34 and !(xBelow34[1] within filterLength bars);
def filtered55 = xBelow55 and !(xBelow55[1] within filterLength bars);
def filtered89 = xBelow89 and !(xBelow89[1] within filterLength bars);
def filtered144 = xBelow144 and !(xBelow144[1] within filterLength bars);
def pctHeldFiltered8 = Sum(filtered21, bars) / Sum(filtered8, bars);
def pctHeldFiltered21 = Sum(filtered34, bars) / Sum(filtered21, bars);
def pctHeldFiltered34 = Sum(filtered55, bars) / Sum(filtered34, bars);
def pctHeldFiltered55 = Sum(filtered89, bars) / Sum(filtered55, bars);
def pctHeldFiltered89 = Sum(filtered144, bars) / Sum(filtered89, bars);
def timesHeld21 = Sum(xBelow8, bars) - Sum(xBelow21, bars);
def timesHeld34 = Sum(xBelow21, bars) - Sum(xBelow34, bars);
def timesHeld55 = Sum(xBelow34, bars) - Sum(xBelow55, bars);
def timesHeld89 = Sum(xBelow55, bars) - Sum(xBelow89, bars);
def timesHeld144 = Sum(xBelow89, bars) - Sum(xBelow144, bars);
#AddLabel(1, AsPercent(pctHeldFiltered), Color.Yellow);
AddLabel(1, ">8<21 EMA: " + timesHeld21 + " (" + AsPercent(pctHeldFiltered8) + ")", Color.YELLOW);
AddLabel(1, ">21<34 EMA: " + timesHeld34 + " (" + AsPercent(pctHeldFiltered21) + ")", Color.WHITE);
AddLabel(1, ">34<55 EMA: " + timesHeld55 + " (" + AsPercent(pctHeldFiltered34) + ")", Color.MAGENTA);
AddLabel(1, ">55<89 EMA: " + timesHeld89 + " (" + AsPercent(pctHeldFiltered55) + ")", Color.CYAN);
AddLabel(1, ">55<89 EMA: " + timesHeld144 + " (" + AsPercent(pctHeldFiltered89) + ")", Color.RED);