Moving Average Cross plot as a line

agent

New member
Hi, can anyone point me to, or possibly create an indicator which plots the 9 EMA and the 21 EMS into a single line and have the color change when the crossover happens and back again on the reverse.

Thanks in advance for the help.
 
Solution
Hi, can anyone point me to, or possibly create an indicator which plots the 9 EMA and the 21 EMS into a single line and have the color change when the crossover happens and back again on the reverse.

Thanks in advance for the help.

Try this

Screenshot 2024-01-15 114038.png
Code:
input length1 = 9;
input length2 = 21;
def ema1 = expaverage(close, length1);
def ema2 = expaverage(close, length2);

plot ema = (ema1 + ema2) / 2;
ema.AssignValueColor(if ema > ema[1] then Color.GREEN else Color.RED);

plot up = if ema1 crosses above ema2 then ema else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up.SetLineWeight(5);

plot dn = if ema1 crosses below ema2 then ema else Double.NaN;
dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN)...
Hi, can anyone point me to, or possibly create an indicator which plots the 9 EMA and the 21 EMS into a single line and have the color change when the crossover happens and back again on the reverse.

Thanks in advance for the help.

Try this

Screenshot 2024-01-15 114038.png
Code:
input length1 = 9;
input length2 = 21;
def ema1 = expaverage(close, length1);
def ema2 = expaverage(close, length2);

plot ema = (ema1 + ema2) / 2;
ema.AssignValueColor(if ema > ema[1] then Color.GREEN else Color.RED);

plot up = if ema1 crosses above ema2 then ema else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up.SetLineWeight(5);

plot dn = if ema1 crosses below ema2 then ema else Double.NaN;
dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn.SetLineWeight(5);

#
 
Solution

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

Hi, not sure if I am not reading it correctly but the plot line is changing but not at the 9/21 cross. Let me know what I am not seeing. thank you View attachment 20722

The line color change is not based upon a cross occuring, but whether the combined ema is different than the combined ema 1 bar prior. The crossing arrows are based upon when the actual ema9 and ema21 crosses that you requested. This would possibly give you an idea of a change in direction, which then could possibly be confirmed by the arrow at the actual cross.

So if you want the combined line color to match the change of the arrow, this was substituted for assignvaluecolor in the above code to do that.

Code:
def emacolor = if isnan(up[1]) and !isnan(up) then 1 else if !isnan(dn) then 0 else emacolor[1];
ema.AssignValueColor(if emacolor[1] == 1 then Color.GREEN else Color.RED);
.
Here is the revised code with the new assignvaluecolor change.
Screenshot 2024-01-16 054156.png
Code:
input length1 = 9;
input length2 = 21;
def ema1 = expaverage(close, length1);
def ema2 = expaverage(close, length2);

plot ema = (ema1 + ema2) / 2;

plot up = if ema1 crosses above ema2 then ema else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up.SetLineWeight(5);
up.setdefaultColor(color.green);

plot dn = if ema1 crosses below ema2 then ema else Double.NaN;
dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn.SetLineWeight(5);
dn.setdefaultColor(color.red);

def emacolor = if isnan(up[1]) and !isnan(up) then 1 else if !isnan(dn) then 0 else emacolor[1];
ema.AssignValueColor(if emacolor[1] == 1 then Color.GREEN else Color.RED);

#
 
Last edited:
The line color change is not based upon a cross occuring, but whether the combined ema is different than the combined ema 1 bar prior. The crossing arrows are based upon when the actual ema9 and ema21 crosses than you requested. This would possibly give you an idea of a change in direction, which then could possibly be confirmed by the arrow at the actual cross.

So if you want the combined line color to match the change of the arrow, this was substituted for assignvaluecolor in the above code to do that.


.
Here is the revised code with the new assignvaluecolor change.
Its Perfect, sorry my original request wasn't as clear. This is what I was looking for. Have a great day. Thank you.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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