Help In Devising "Early Warning System" For Pending EMA Crossover

jhorton56

Member
VIP
I have worked all day today to try to build upon the scans posted earlier to create one that will identify stocks whose 8 EMA is rising and is within 2% of crossing its 21 EMA from below, and vice versa. I took some code from before and came up with this:

def a = ExpAverage(close, 8);
def b = ExpAverage(close, 21);
def d = a - b;
plot p = (d / b) * 100;

input center_line = 0;
plot CenterLine = center_line;

input upper_line = .2;
plot UpperLine = upper_line;

input lower_line = -.2;
plot LowerLine = lower_line;

That generates this study, with arrows pointing at the points in the price action where I would like my scan to I.D. the stocks having that characteristic (in two separate scans, one going up and v.v.).

1BVbV6U.png


My intent is to get a jump on the time when the 8 EMA is about to cross the 21 EMA from either direction, depending on the scan I'm using. I just can't figure out after struggling for about six hours how to code it. Another way to describe it is I would like the scan to pick up on what stocks have the blue line crossing the yellow -2% line from below or the blue line crossing the white +2% line from above. The red line is where the 8 and the 21 cross. I put the study into a scan and got this, but I'm not sure if it is working how I want.

4HgBKtT.png
 
Solution
@jhorton56
Sounds like you just need to see 2% above and below the 21 EMA in your upper chart.

plot a = ExpAverage(close, 8);
plot b = ExpAverage(close, 21);
plot p1 = ((b*.2)/100)+b;
plot p2 = b-((b*.2)/100);

addcloud(p1,p2,color.gray);
@jhorton56
Sounds like you just need to see 2% above and below the 21 EMA in your upper chart.

plot a = ExpAverage(close, 8);
plot b = ExpAverage(close, 21);
plot p1 = ((b*.2)/100)+b;
plot p2 = b-((b*.2)/100);

addcloud(p1,p2,color.gray);
 
Solution
Actually the study up above is just for illustrative/explanatory purposes, and I do mean to use .2 for 2%, not 2 for 20%. To say in plain words, my successful scan would return a hit for a rising price EMA 8 that reached a point equal to or less than 2% away from the 21 EMA (rising blue line crossing the yellow line from below), and conversely would return a hit for a falling price 21 EMA that reached a point equal to or less than 2% away from the 8 EMA (falling blue line crossing the white line from above). See the arrows in the bottom section of my study pointing at those two cases. My objective is to become aware from alerts or through refreshing a live watchlist that one or the other of these arrow points had been reached. I understand how to scan for a price within x% of an MA, but I can't figure out how to scan for one MA being within x% of another MA. It's beyond my ability, and for all I know it can't be done.
 
@jhorton56
Use this code:
Code:
#EMA8_Within_EMA21
plot EMA8 = ExpAverage(close, 8);
plot EMA21 = ExpAverage(close, 21);

plot upper_EMA8 = ((EMA8*.2)/100)+EMA8;
plot lower_EMA8 = EMA8-((EMA8*.2)/100);

plot upper_EMA21 = ((EMA21*.2)/100)+EMA21;
plot lower_EMA21 = EMA21-((EMA21*.2)/100);

addcloud(upper_EMA8,lower_EMA8,color.Light_gray);
addcloud(upper_EMA21,lower_EMA21,color.dark_gray);

AddChartBubble(IsNaN(close[-1]),EMA8,"EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),EMA21,"EMA21",color.white);
AddChartBubble(IsNaN(close[-1]),upper_EMA8,"2% above EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),lower_EMA8,"2% below EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),upper_EMA21,"2% above EMA21",color.white);
AddChartBubble(IsNaN(close[-1]),lower_EMA21,"2% below EMA21",color.white);

and this scan: (pay attention to aggregation period and make sure "any" is selected)
9OMMqHj.png


will give you hits at either yellow oval:

mxw0qye.png
 
Last edited by a moderator:
I'm trying to learn how to work on a strategy of buying debit spreads based on an 8EMA-21EMA cross. Buy a put debit spread after confirming the 8 crossing down past the 21, placing one side of the spread a bit ahead of that point, going 5 to 10 wide. Opposite for a call debit spread. It occurred to me that if I can get a heads up of an imminent cross, I can watch those particular underlyings on a 60-minute chart for the cross to occur instead of scattershooting around hoping I run across a developing setup.
 
@jhorton56
Use this code:
Code:
#EMA8_Within_EMA21
plot EMA8 = ExpAverage(close, 8);
plot EMA21 = ExpAverage(close, 21);

plot upper_EMA8 = ((EMA8*.2)/100)+EMA8;
plot lower_EMA8 = EMA8-((EMA8*.2)/100);

plot upper_EMA21 = ((EMA21*.2)/100)+EMA21;
plot lower_EMA21 = EMA21-((EMA21*.2)/100);

addcloud(upper_EMA8,lower_EMA8,color.Light_gray);
addcloud(upper_EMA21,lower_EMA21,color.dark_gray);

AddChartBubble(IsNaN(close[-1]),EMA8,"EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),EMA21,"EMA21",color.white);
AddChartBubble(IsNaN(close[-1]),upper_EMA8,"2% above EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),lower_EMA8,"2% below EMA8",color.white);
AddChartBubble(IsNaN(close[-1]),upper_EMA21,"2% above EMA21",color.white);
AddChartBubble(IsNaN(close[-1]),lower_EMA21,"2% below EMA21",color.white);

and this scan: (pay attention to aggregation period and make sure "any" is selected)
9OMMqHj.png


will give you hits at either yellow oval:

mxw0qye.png
Cam someone share their final version of chart / scan using the TOS links? Appreciate it.
Shared chart link: http://tos.mx/LpndSA5 Click here for --> Easiest way to load shared links
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
521 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