Moving Average Crossovers For ThinkOrSwim

@ben
This is just a simple indicator for moving average crossover but added scanner, cloud, and alerts for additional visual effect and enhancement.

For example, if 5/10 EMA crossover is your strategy, then this indicator plot an up arrow on the golden cross and down arrow on the death cross. You can also use the scanner to scan for stocks with EMA crossover and the built-in alerts to let you know as it happens.

View attachment 4629

thinkScript Code

Rich (BB code):
# Moving Average Crossover With Arrows, Alerts, Crossing Count and Bubble at Cross
# Mobius
# Chat Room Request 01.25.2017
# Modified a bit by BenTen

input price = close;
input fastLength = 8;
input slowLength = 21;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

plot ArrowUp = if FastMA crosses above SlowMA
               then low
               else double.nan;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = if FastMA crosses below SlowMA
               then high
               else double.nan;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);
Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

DefineGlobalColor("Bullish", color.green);
DefineGlobalColor("Bearish", color.red);
input seeClouds = yes ;
AddCloud(if(seeClouds,FastMA,double.NaN), SlowMA, GlobalColor("Bullish"), GlobalColor("Bearish"));
# End Code

Shareable Link

https://tos.mx/2ZED6i
Can I edit this code to make the crossover at 10sma over 50sma ? I created new study , and edited the 9/20 for 10/50. Also , how can I eliminate the clouds and only have the moving average lines ? Tia
 
Need help with
https://usethinkscript.com/threads/moving-average-crossovers-for-thinkorswim.229/#post-1352

Below is the code and it works fine but i would like to add the price on the chart at which the crossover happened. please see below for the code.(also attached).
as you can see in the snapshot.. i would like the price to display on the chart above black and red arrow and below for green and blue arrow. Thanks in advance.


# Moving Average Crossover With Arrows, Alerts, Crossing Count and Bubble at Cross
# Mobius
# Chat Room Request 01.25.2017
# Modified a bit by BenTen

input price = close;
input fastLength = 9;
input slowLength = 21;
input averageType = AverageType.EXPONENTIAL;


plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

plot ArrowUp = if FastMA crosses above SlowMA
then low
else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetLineWeight(3);
ArrowUp.SetDefaultColor(Color.GREEN);
plot ArrowDN = if FastMA crosses below SlowMA
then high
else Double.NaN;
ArrowDN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.RED);
# Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
# Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

AddCloud(FastMA, SlowMA, Color.GREEN, Color.RED);
# End Code

# - Added code to include 5 EMA and an arrow when 5 crosses 9 and viceversa
input fivelength = 5;
plot FiveMA = MovingAverage(averageType, price, fivelength);
FiveMA.setStyle(curve.short_dash);
FiveMA.SetDefaultColor(Color.BLACK);

input twohundredlength = 200;
plot TwohunMA = MovingAverage(averageType, price, twohundredlength);
TwohunMA.setStyle(curve.short_dash);
TwohunMA.SetDefaultColor(Color.PLUM);

plot Aup = if FiveMA crosses above FastMA
then low
else Double.NaN;
Aup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Aup.SetLineWeight(3);
Aup.SetDefaultColor(Color.BLUE);
plot Adn = if FiveMA crosses below FastMA
then high
else Double.NaN;
Adn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Adn.SetLineWeight(3);
Adn.SetDefaultColor(Color.BLACK);


# - added code for 34 and 50 EMA from Ripster
input ema2low = 34;
input ema2high = 50;

def ema34 = ExpAverage(hl2, ema2low);
def ema50 = ExpAverage(hl2, ema2high);
AddCloud(ema34, ema50, CreateColor(33, 150, 243), CreateColor(255, 183, 77));
 

Attachments

  • Crossover.png
    Crossover.png
    29.4 KB · Views: 162
  • Moving Crossover averages.txt
    2.2 KB · Views: 142
Last edited by a moderator:
Need help with
https://usethinkscript.com/threads/moving-average-crossovers-for-thinkorswim.229/#post-1352

Below is the code and it works fine but i would like to add the price on the chart at which the crossover happened. please see below for the code.(also attached).
as you can see in the snapshot.. i would like the price to display on the chart above black and red arrow and below for green and blue arrow. Thanks in advance.


# Moving Average Crossover With Arrows, Alerts, Crossing Count and Bubble at Cross
# Mobius
# Chat Room Request 01.25.2017
# Modified a bit by BenTen

input price = close;
input fastLength = 9;
input slowLength = 21;
input averageType = AverageType.EXPONENTIAL;


plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

plot ArrowUp = if FastMA crosses above SlowMA
then low
else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetLineWeight(3);
ArrowUp.SetDefaultColor(Color.GREEN);
plot ArrowDN = if FastMA crosses below SlowMA
then high
else Double.NaN;
ArrowDN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.RED);
# Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
# Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

AddCloud(FastMA, SlowMA, Color.GREEN, Color.RED);
# End Code

# - Added code to include 5 EMA and an arrow when 5 crosses 9 and viceversa
input fivelength = 5;
plot FiveMA = MovingAverage(averageType, price, fivelength);
FiveMA.setStyle(curve.short_dash);
FiveMA.SetDefaultColor(Color.BLACK);

input twohundredlength = 200;
plot TwohunMA = MovingAverage(averageType, price, twohundredlength);
TwohunMA.setStyle(curve.short_dash);
TwohunMA.SetDefaultColor(Color.PLUM);

plot Aup = if FiveMA crosses above FastMA
then low
else Double.NaN;
Aup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Aup.SetLineWeight(3);
Aup.SetDefaultColor(Color.BLUE);
plot Adn = if FiveMA crosses below FastMA
then high
else Double.NaN;
Adn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Adn.SetLineWeight(3);
Adn.SetDefaultColor(Color.BLACK);


# - added code for 34 and 50 EMA from Ripster
input ema2low = 34;
input ema2high = 50;

def ema34 = ExpAverage(hl2, ema2low);
def ema50 = ExpAverage(hl2, ema2high);
AddCloud(ema34, ema50, CreateColor(33, 150, 243), CreateColor(255, 183, 77));

This assumes that when the red/black or green/blue occur at one bar (although the arrows plot over each other resulting in either a red or green arrow) is what you wanted to have that arrows price displayed in a bubble.

To do so, at the input bubble_display, choose 'stacked'. Otherwise, you can choose to have the price displayed for all arrows.

Add this to the bottom of your code

Screenshot 2024-01-10 094458.png
Code:
input bubble_offset = 4;
input bubble_display = {default stacked, all};
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowUp == Aup then ArrowUp else ArrowUp, low - TickSize() * bubble_offset, low, ArrowUp.TakeValueColor(), no);
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowDN == Adn then ArrowDN else ArrowDN, high + TickSize() * bubble_offset, high, ArrowDN.TakeValueColor());

AddChartBubble(if bubble_display == bubble_display.stacked and ArrowUp == Aup then Aup else Aup, low - TickSize() * bubble_offset, low, CreateColor(102, 102, 255), no);
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowDN == Adn then Adn else Adn, high + TickSize() * bubble_offset, high, Color.GRAY);

I let both stacked bubbles display, but you can eliminate the display of the Aup or Adn part of it by adding the following code instead of the above code.

Screenshot 2024-01-10 094325.png
Code:
input bubble_offset = 4;
input bubble_display = {default stacked, all};
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowUp == Aup then ArrowUp else ArrowUp, low - TickSize() * bubble_offset, low, ArrowUp.TakeValueColor(), no);
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowDN == Adn then ArrowDN else ArrowDN, high + TickSize() * bubble_offset, high, ArrowDN.TakeValueColor());

AddChartBubble(if bubble_display == bubble_display.stacked and ArrowUp == Aup then double.nan else Aup, low - TickSize() * bubble_offset, low, CreateColor(102, 102, 255), no);
AddChartBubble(if bubble_display == bubble_display.stacked and ArrowDN == Adn then double.nan else Adn, high + TickSize() * bubble_offset, high, Color.GRAY);
 
This assumes that when the red/black or green/blue occur at one bar (although the arrows plot over each other resulting in either a red or green arrow) is what you wanted to have that arrows price displayed in a bubble.

To do so, at the input bubble_display, choose 'stacked'. Otherwise, you can choose to have the price displayed for all arrows.

Add this to the bottom of your code



I let both stacked bubbles display, but you can eliminate the display of the Aup or Adn part of it by adding the following code instead of the above code.
Thanks SleepyZ. this is really helpful. if i want to show the price at each arrow how do i do it. right now the bubble is showing up when there is an overlap of red/black or green/blue arrow only. can the price bubble show at each arrow and at the overlap show only one price(as you provided in the second snapshot).

Not sure if i am asking alot :) one more request. can the candle color be changed at the cross over and run a vertical band with color making sure the candle is clearly visible with wicks.
Attached is a snapshot from Ninjatrader - when the crossover turned green candle color changed to BLUE and vertical band color to light GREEN. when the crossover turned red candle color changed to BLACK with vertical band color light RED.
Thanks in advance.
 

Attachments

  • vertical bar with candle color.png
    vertical bar with candle color.png
    18.5 KB · Views: 31
I have been playing with EMA 10, 20 Crossover and have come up with one that I think many of you will find interesting. I have modified the plot lines with two colors on up trends and both plots having red color on down trends. I removed the bubbles that overlay the candles, making it much more easy to read. The original script is not mine but I think the changes I have made will be helpful to some of you

Here is the code and I will follow up with some screen shots if there is a need.

Code:

# Moving Average Crossover With Alerts, and trend colors
# Original credit to Mobius modifications by JmaxFl
# Modification of Chat Room Request 01.25.2017
input price = close;
input fastLength = 10;
input slowLength = 20;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.AssignValueColor(if FastMA > SlowMA then color.LIGHT_ORANGE else color.red );
SlowMA.AssignValueColor(if FastMA > SlowMA then color.CYAN else color.red);

plot ArrowUp = if FastMA crosses above SlowMA
then low
else double.nan;
ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
ArrowUP.SetLineWeight(3);
ArrowUP.SetDefaultColor(Color.Light_ORANGE);
plot ArrowDN = if FastMA crosses below SlowMA
then high
else double.nan;
ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.Red);
Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);
def countUP = if FastMA crosses above SlowMA
then 1
else if FastMA > SlowMA
then countUP[1] + 1
else if ArrowDN
then 0
else countUP[1];
AddLabel(1, "Count UP = " + countUP, color.white);
def CrossBar = if FastMA crosses SlowMa
then barNumber()
else double.nan;
 

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
358 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