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
 

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

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: 377
  • Moving Crossover averages.txt
    2.2 KB · Views: 318
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: 162
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;
 
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.

Just looking at this thread and saw your request

1, Added optional bubbles for all arrows. Stacked = Green/Red, ArrowUpDn only = Light Green/ Pink, All Otheres = Violet/Gray
2. Add Vertical Clouds at Stacked = Light Green/ Light Red. Clouds do not alighn exactly over the bar. TOS does not usually print clouds for just one bar. So this uses one workaround method.

Screenshot 2024-04-04 105126.png
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 = 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));


#Bubbles

input showbubbles   = yes;
input bubble_offset = 2;

AddChartBubble(if showbubbles and ArrowUp == Aup then ArrowUp else Double.NaN, low - TickSize() * bubble_offset, AsText(low), ArrowUp.TakeValueColor(), no);
AddChartBubble(if showbubbles and ArrowDN == Adn then ArrowDN else Double.NaN, high + TickSize() * bubble_offset, AsText(high), ArrowDN.TakeValueColor());

AddChartBubble(if showbubbles and ArrowUp and IsNaN(Aup) then ArrowUp else Double.NaN, low - TickSize() * bubble_offset, AsText(low), Color.LIME) ;
AddChartBubble(if showbubbles and ArrowDN and IsNaN(Adn) then ArrowDN else Double.NaN, high + TickSize() * bubble_offset, AsText(high), Color.PINK) ;

AddChartBubble(if showbubbles and IsNaN(ArrowUp) and Aup then Aup else Double.NaN, low - TickSize() * bubble_offset, AsText(low), CreateColor(102, 102, 255), no);
AddChartBubble(if showbubbles and IsNaN(ArrowDN) and Adn then Adn else Double.NaN, high + TickSize() * bubble_offset, AsText(high), Color.GRAY);

#Price Color

input pricecolor = yes;
AssignPriceColor(if !pricecolor then Color.CURRENT else if ArrowDN == Adn then Color.BLACK else Color.CURRENT);
AssignPriceColor(if !pricecolor then Color.CURRENT else if ArrowUp == Aup then Color.BLUE else Color.CURRENT);

#Clouds for Red/Green Arrows

plot top1;
plot bot1;
if ArrowUp[-1] == Aup[-1]
then {
# green , 1st bar
    top1 = Double.POSITIVE_INFINITY;
    bot1 = Double.NEGATIVE_INFINITY;
} else if ArrowUp[0]
then {
# green , 2nd bar
    top1 = Double.POSITIVE_INFINITY;
    bot1 = Double.NEGATIVE_INFINITY;
} else {
    top1 = Double.NaN;
    bot1 = Double.NaN;
}

AddCloud(top1, bot1, Color.LIGHT_GREEN, Color.LIGHT_GREEN);

plot top;
plot bot;
if ArrowDN[-1] == Adn[-1]
then {
# green , 1st bar
    top = Double.POSITIVE_INFINITY;
    bot = Double.NEGATIVE_INFINITY;
} else if ArrowDN[0]
then {
# green , 2nd bar
    top = Double.POSITIVE_INFINITY;
    bot = Double.NEGATIVE_INFINITY;
} else {
    top = Double.NaN;
    bot = Double.NaN;
}

AddCloud(top, bot, Color.LIGHT_RED, Color.LIGHT_RED);

#
 
Try this TOS status at a glance column script to visually aid trader with real time highlights of potential swings and close opportunities directly on the Monitor tab.
This script flags stocks based on their current trading status, helping you quickly identify and act upon possible entry or exit points.
By integrating this script into your TOS setup, you can streamline your trading decisions and maintain better control over your portfolio with clear, visually indicated suggestions for swing trades or closing positions.

# Define your SMA fast and slow
declare lower;
input fastLength = 12;
input slowLength = 26;

# Calculate the SMA values
def SMAFast = Average(close, fastLength);
def SMASlow = Average(close, slowLength);

# Determine the trading signal
def BuySignal = SMAFast crosses above SMASlow;
def SellSignal = SMAFast crosses below SMASlow;
def HoldSignal = SMAFast > SMASlow and !SellSignal;

# Logic for Check Indicators - when there's no clear signal
def CheckIndicators = !(BuySignal or SellSignal or HoldSignal);

# Assign colors to signals
AddLabel(yes,
if BuySignal then "BUY"
else if SellSignal then "SELL"
else if HoldSignal then "HOLD"
else if CheckIndicators then "CHECK INDICATORS"
else "WAITING FOR SIGNAL",
if BuySignal then Color.GREEN
else if SellSignal then Color.RED
else if HoldSignal then Color.YELLOW
else if CheckIndicators then Color.GRAY
else Color.DARK_GRAY);
 

Attachments

  • TOS SMA Indicator.png
    TOS SMA Indicator.png
    39.8 KB · Views: 142
Last edited:
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
Hi BenTen,

Is there a way to modify the above code to work with moving averages from two different time frames? I've tried adding the aggregationperiod line in but it isn't working.
 
Thanks for replying @BenTen. I am looking for a way to make the code below work for a crossover between the 50EMA on the 10 min chart and the 50EMA on the 30 min chart. Do you know if it's even possible? The linked you shared doesn't have code for this kind of crossover indicator.

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 = 25;
input slowLength = 50;
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.Bell);
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
 
Last edited by a moderator:
Thanks for replying @BenTen. I am looking for a way to make the code below work for a crossover between the 50EMA on the 10 min chart and the 50EMA on the 30 min chart. Do you know if it's even possible? The linked you shared doesn't have code for this kind of crossover indicator.

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 = 25;
input slowLength = 50;
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.Bell);
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

Here you go:

Code:
# MTF Moving Average
input Period1 = aggregationPeriod.TEN_MIN;
input Period2 = aggregationPeriod.THIRTY_MIN;
input AvgType = averageType.ExPONENTIAL;
input Length = 50;
plot AVG1 = MovingAverage(AvgType, close(period = Period1), Length);
plot AVG2 = MovingAverage(AvgType, close(period = Period2), Length);
 
Here you go:

Code:
# MTF Moving Average
input Period1 = aggregationPeriod.TEN_MIN;
input Period2 = aggregationPeriod.THIRTY_MIN;
input AvgType = averageType.ExPONENTIAL;
input Length = 50;
plot AVG1 = MovingAverage(AvgType, close(period = Period1), Length);
plot AVG2 = MovingAverage(AvgType, close(period = Period2), Length);
Thank you BenTen!
 
What’s the best way to avoid getting “faked out” when MA’s cross? There’s a lot of times when they cross and then cross back the other direction or just barely cross and when I enter a trade, I end up losing money because price ends up turning the opposite direction? What indicator/strategy does everyone use in combination with MA’s?
 
What’s the best way to avoid getting “faked out” when MA’s cross? There’s a lot of times when they cross and then cross back the other direction or just barely cross and when I enter a trade, I end up losing money because price ends up turning the opposite direction? What indicator/strategy does everyone use in combination with MA’s?
Ripster uses moving averages.
Read some of the recommendations on this VIP version of the Ripster setup:
https://usethinkscript.com/threads/ripster-clouds-strategy-companion-guide.19109/
 
Last edited:
Attached is an update the my moving average crossover study, that increased the moving averages to three, added bubbles and cloud. Renamed it to JM_Ribbon you will see why.

I use this chart for the SPY as my base chart for day trading SPY Options. I intend to share all the studies I use in a separate post.

but here is the code:

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
# Further Modified by Jmaxfl
# fastLength Bubble is crossover of fastLength and midLength, slowLength Bubble is crossover of midLength and SlowLength
# cloud paints between the midLength and slowLength  averages
# bubble colors crossupFast lighter green , crossupSlow darker green, crossDnFast orange, crossDNSlow Red

input price = close;
input showCloud = yes;
input fastLength = 5;
input midLength = 10;
input slowLength = 20;
input fastEMACross = yes;
input slowEMACross = yes;
input averageType = AverageType.EXPONENTIAL;
input bubble_offset = .1;

# Plot Averages
plot FastMA = MovingAverage(averageType, price, fastLength);
plot MidMA = MovingAverage(averageType, price, midLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);

FastMA.SetDefaultColor(CreateColor(255, 153, 0));
FastMA.SetLineWeight(2);
MidMA.SetDefaultColor(CreateColor(255,0,50));
MidMA.SetLineWeight(2);
SlowMA.SetDefaultColor(CreateColor(0, 197,49));
SlowMA.SetLineWeight(2);


# Crossover Up (Fast)
plot CrossUpFast = if FastMA crosses above MidMA and fastEMACross
               then low
               else Double.NaN;
AddChartBubble(CrossUpFast, low - bubble_offset, +fastLength, CreateColor(153,255,153), no);

# Crossover Up (Slow)
plot CrossUpSlow = if MidMA crosses above SlowMA and slowEMACross
               then low
               else Double.NaN;
AddChartBubble(CrossUpSlow, low - bubble_offset,+midLength, CreateColor(0,197,49), no);


# Crossover Down (Fast)
plot CrossDnFast = if FastMA crosses below MidMA and fastEMACross
               then high
               else Double.NaN;

AddChartBubble(CrossDnFast, high + bubble_offset,+fastLength, CreateColor(255,126,0), yes);

# Crossover Down (Slow)
plot CrossDNSlow = if MidMA crosses below SlowMA and slowEMACross
               then high
               else Double.NaN;

AddChartBubble(CrossDNSlow, high + bubble_offset  ,+ midLength, CreateColor(255,0,50), yes);

Alert(CrossUpSlow, midlength+"X"+slowlength+" Dn", Alert.BAR, Sound.Ding);
Alert(CrossUpFast, fastlength+" X"+midlength+" Up", Alert.BAR, Sound.Ding);
Alert(CrossDNSlow, midlength+"X"+slowLength+" Dn", Alert.BAR, Sound.Ding);
Alert(CrossDnFast,  fastlength+" X"+midlength+" Dn", Alert.BAR, Sound.Ding);

AddCloud( MidMA, SlowMA, Color.GREEN, Color.Red);

# End Code
 

Attachments

  • 1M_Options_Chart.JPG
    1M_Options_Chart.JPG
    249 KB · Views: 113
Last edited by a moderator:
Attached is an update the my moving average crossover study, that increased the moving averages to three, added bubbles and cloud. Renamed it to JM_Ribbon you will see why.

I use this chart for the SPY as my base chart for day trading SPY Options. I intend to share all the studies I use in a separate post.

but here is the code:

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
# Further Modified by Jmaxfl
# fastLength Bubble is crossover of fastLength and midLength, slowLength Bubble is crossover of midLength and SlowLength
# cloud paints between the midLength and slowLength  averages
# bubble colors crossupFast lighter green , crossupSlow darker green, crossDnFast orange, crossDNSlow Red

input price = close;
input showCloud = yes;
input fastLength = 5;
input midLength = 10;
input slowLength = 20;
input fastEMACross = yes;
input slowEMACross = yes;
input averageType = AverageType.EXPONENTIAL;
input bubble_offset = .1;

# Plot Averages
plot FastMA = MovingAverage(averageType, price, fastLength);
plot MidMA = MovingAverage(averageType, price, midLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);

FastMA.SetDefaultColor(CreateColor(255, 153, 0));
FastMA.SetLineWeight(2);
MidMA.SetDefaultColor(CreateColor(255,0,50));
MidMA.SetLineWeight(2);
SlowMA.SetDefaultColor(CreateColor(0, 197,49));
SlowMA.SetLineWeight(2);


# Crossover Up (Fast)
plot CrossUpFast = if FastMA crosses above MidMA and fastEMACross
               then low
               else Double.NaN;
AddChartBubble(CrossUpFast, low - bubble_offset, +fastLength, CreateColor(153,255,153), no);

# Crossover Up (Slow)
plot CrossUpSlow = if MidMA crosses above SlowMA and slowEMACross
               then low
               else Double.NaN;
AddChartBubble(CrossUpSlow, low - bubble_offset,+midLength, CreateColor(0,197,49), no);


# Crossover Down (Fast)
plot CrossDnFast = if FastMA crosses below MidMA and fastEMACross
               then high
               else Double.NaN;

AddChartBubble(CrossDnFast, high + bubble_offset,+fastLength, CreateColor(255,126,0), yes);

# Crossover Down (Slow)
plot CrossDNSlow = if MidMA crosses below SlowMA and slowEMACross
               then high
               else Double.NaN;

AddChartBubble(CrossDNSlow, high + bubble_offset  ,+ midLength, CreateColor(255,0,50), yes);

Alert(CrossUpSlow, midlength+"X"+slowlength+" Dn", Alert.BAR, Sound.Ding);
Alert(CrossUpFast, fastlength+" X"+midlength+" Up", Alert.BAR, Sound.Ding);
Alert(CrossDNSlow, midlength+"X"+slowLength+" Dn", Alert.BAR, Sound.Ding);
Alert(CrossDnFast,  fastlength+" X"+midlength+" Dn", Alert.BAR, Sound.Ding);

AddCloud( MidMA, SlowMA, Color.GREEN, Color.Red);

# End Code
What are the colored numbers? I see a light green 5 (is this 5 min chart?), an orange 5, a darker green 10, a red 10.
 
First, thank you for your service.

This is a one minute chart but it works on any intra-day timeframe or daily for that matter. I have not tested above that. Those bubbles show when the 5 bar moving average crosses the 10 bar moving average and when the 10 crosses the 20. Sometimes it is hard to see the actual crossover when they are very close to each other. I use to have 5x10 and 10x20 for the text in the bubbles but prefer to have them take up less space on the chart. The number are set based upon what values you use for the averages. Warmer colors are used for crosses down and the light green and slightly darker green are for crosses up. The averages and colors are completely customizable.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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