 # EMA price crossover counter

#### KG3344

##### New member
VIP
Hello,
I am trying to understand how to code a label that would count the number of times price "holds" above a moving average. Holding above would be a cross below an MA then a cross back above that MA without crossing below a secondary MA. So for example, price closing below the 8 EMA, then crossing back above it without closing below the 21 EMA. I am trying to display the number of times that this condition is met, along with the percent of times the EMA "held' versus the times it didn't. The bottom labels in the picture that are formatted >8<21 EMA: 47 (43%) is what I am interested in. Any advice would be greatly appreciated. #### Slippage

##### Well-known member
The advice I would give for this is that an indirect approach to this code will probably end up being a lot simpler than a direct approach and, though maybe a little less precise, likely close enough. For instance, a direct approach might be detecting the cross below 8 and then tracking state while watching for a cross above 8 or below 21, then updating a counter for whichever happened. Then resetting everything and repeating. An indirect approach may be as simple as keeping separate counts of crosses and doing math on them.

For the signal to be more meaningful you might want to limit how many crosses are counted within some number of bars. That way if price is sideways, with the close crossing back and forth over the ema several times within a few bars, it doesn't skew your stats much.

The example I'm going to give only tracks for long. You might want to track short also as a separate or combined percentage.

Ruby:
``````input bars = 220;
input filterLength = 3;

def ema8 = ExpAverage(close, 8);
def ema21 = ExpAverage(close, 21);

def xBelow8 = close crosses below ema8;
def xBelow21 = close crosses below ema21;

def pctHeld = Sum(xBelow21, bars) / Sum(xBelow8, bars);

def filtered8 = xBelow8 and !(xBelow8 within filterLength bars);
def filtered21 = xBelow21 and !(xBelow21 within filterLength bars);

def pctHeldFiltered = Sum(filtered21, bars) / Sum(filtered8, bars);

#### KG3344

##### New member
VIP
The advice I would give for this is that an indirect approach to this code will probably end up being a lot simpler than a direct approach and, though maybe a little less precise, likely close enough. For instance, a direct approach might be detecting the cross below 8 and then tracking state while watching for a cross above 8 or below 21, then updating a counter for whichever happened. Then resetting everything and repeating. An indirect approach may be as simple as keeping separate counts of crosses and doing math on them.

For the signal to be more meaningful you might want to limit how many crosses are counted within some number of bars. That way if price is sideways, with the close crossing back and forth over the ema several times within a few bars, it doesn't skew your stats much.
I was also thinking that counting the crosses separately was the more appropriate method. I added the other average labels and formatted them a bit differently. I feel like it needs to be backtested a bit to make sure it's working correctly, but it's definitely a great start. Thank you.

Code:
``````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 within filterLength bars);
def filtered21 = xBelow21 and !(xBelow21 within filterLength bars);
def filtered34 = xBelow34 and !(xBelow34 within filterLength bars);
def filtered55 = xBelow55 and !(xBelow55 within filterLength bars);
def filtered89 = xBelow89 and !(xBelow89 within filterLength bars);
def filtered144 = xBelow144 and !(xBelow144 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);