EMA price crossover counter

KG3344

New member
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.
deepstats.png
 
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);
AddLabel(1, AsPercent(pctHeld), Color.LIGHT_GRAY);

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

def pctHeldFiltered = Sum(filtered21, bars) / Sum(filtered8, bars);
AddLabel(1, AsPercent(pctHeldFiltered), Color.LIGHT_GRAY);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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[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);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
335 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top