How to add clouds between moving averages in the upper chart?

Wardo21

New member
Hello,

Can someone assist me with a code to add a cloud between two moving averages in the upper chart?

For example, take the 50 day and the 200 day moving averages. When the 50 day is below the 200 day, the space between those two moving averages would be green (in anticipation of the 50 day rising above the 200 day moving average). When the 50 day was above the 200 day, the space between those two averages would be red (in anticipation of the 50 day falling below the 200 day moving average).

I hope this makes sense.

Thanks!
 
Solution
Hello,

Can someone assist me with a code to add a cloud between two moving averages in the upper chart?

For example, take the 50 day and the 200 day moving averages. When the 50 day is below the 200 day, the space between those two moving averages would be green (in anticipation of the 50 day rising above the 200 day moving average). When the 50 day was above the 200 day, the space between those two averages would be red (in anticipation of the 50 day falling below the 200 day moving average).

I hope this makes sense.

Thanks!

Here is a mod to a script I just finished here https://usethinkscript.com/threads/mtf-sma.12233/post-104900



Capture.jpg
Ruby:
input showonexpansion = no;
def SMA50D   = if IsNaN(close)...
Hello,

Can someone assist me with a code to add a cloud between two moving averages in the upper chart?

For example, take the 50 day and the 200 day moving averages. When the 50 day is below the 200 day, the space between those two moving averages would be green (in anticipation of the 50 day rising above the 200 day moving average). When the 50 day was above the 200 day, the space between those two averages would be red (in anticipation of the 50 day falling below the 200 day moving average).

I hope this makes sense.

Thanks!

Here is a mod to a script I just finished here https://usethinkscript.com/threads/mtf-sma.12233/post-104900



Capture.jpg
Ruby:
input showonexpansion = no;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA50D ;

def SMA200D  = if IsNaN(close)
               then SMA200D[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.DAY), 200) ;
plot SMAD200 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA200D ;

#Clouds
input showclouds  = yes;
addcloud(if showclouds then SMAD50 else double.nan, SMAD200, color.green, color.red);


#Bubbles
input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD50[bm], "SMA50 D",
               SMAD50.TakeValueColor());
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD200[bm], "SMA200 D",
               SMAD200.TakeValueColor());
 
Solution
Excellent, thank you!

Ps. Is there a way to change the color gradient for clouds like you can for moving average lines? Assuming there isn't, but figured I'd ask.
 
Excellent, thank you!

Ps. Is there a way to change the color gradient for clouds like you can for moving average lines? Assuming there isn't, but figured I'd ask.

You can use rgb coloring

CreateColor​

CreateColor ( double red, double green, double blue);

Description​

Generates a color based on its rgb code.

Input parameters​

ParameterDefault valueDescription
red-Defines color value on red scale.
green-Defines color value on green scale.
blue-Defines color value on blue scale.

Example​

plot Price = close;
Price.SetDefaultColor(CreateColor(255, 220, 210));
This example paints the Price chart in color that has the 255, 220, 210 rgb code.
 
Hello,

Can someone assist me with a code to add a cloud between two moving averages in the upper chart?

For example, take the 50 day and the 200 day moving averages. When the 50 day is below the 200 day, the space between those two moving averages would be green (in anticipation of the 50 day rising above the 200 day moving average). When the 50 day was above the 200 day, the space between those two averages would be red (in anticipation of the 50 day falling below the 200 day moving average).

I hope this makes sense.

Thanks!
Put you moving average numbers in this, see if it works for you http://tos.mx/0UoMHZi
 
You can use rgb coloring

CreateColor​

CreateColor ( double red, double green, double blue);

Description​

Generates a color based on its rgb code.

Input parameters​

ParameterDefault valueDescription
red-Defines color value on red scale.
green-Defines color value on green scale.
blue-Defines color value on blue scale.

Example​

plot Price = close;
Price.SetDefaultColor(CreateColor(255, 220, 210));
This example paints the Price chart in color that has the 255, 220, 210 rgb code.


Last question: I use a code for extensions on my moving averages (see code below). However, the code you provided is not filling the area between the moving average extensions. Any chance you can add clouds to the extension code below as well? Thanks!

Simple moving average extension

input MAlength = 30;
input AvgType = AverageType.SIMPLE;
input price = close;
plot moveavg = MovingAverage(AvgType, price, MAlength);
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);
def inertline = inertiaall(moveavg,2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);
 
Last question: I use a code for extensions on my moving averages (see code below). However, the code you provided is not filling the area between the moving average extensions. Any chance you can add clouds to the extension code below as well? Thanks!

Simple moving average extension

input MAlength = 30;
input AvgType = AverageType.SIMPLE;
input price = close;
plot moveavg = MovingAverage(AvgType, price, MAlength);
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);
def inertline = inertiaall(moveavg,2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);

Sure, this seems to work.

I added another option for you, named input aggperiod. It is defaulted to manual, which will use the period at input aggregation, currently set to DAY. If you choose input aggperiod as current, then whatever chart aggregation you are viewing, the SMAs will use that chart's aggregation. So if you are on a 15m chart it will use 15m agg, a daily chart will use a daily agg, etc... This way you can see the effect of the SMA 50/200 chart periods rather than just a daily agg.



Capture.jpg
Ruby:
input aggperiod       = {current, default manual} ;
input aggregation     = aggregationPeriod.DAY;
input showonexpansion = no;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else SimpleMovingAvg (close (period = if aggperiod==aggperiod.current
                                                     then getAggregationperiod()
                                                     else aggregation), 50) ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA50D ;


def inertline = inertiaall(SMAD50,2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);

def SMA200D  = if IsNaN(close)
               then SMA200D[1]
               else SimpleMovingAvg (close (period = if aggperiod==aggperiod.current
                                                     then getAggregationperiod()
                                                     else aggregation), 200) ;
plot SMAD200 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA200D ;

def inertline1 = inertiaall(SMAD200,2);
def EXT_MA1 = if !IsNaN(close()) then inertline1 else EXT_MA1[1] + ((EXT_MA1[1] - EXT_MA1[2]) / (2 - 1));
plot extension1 = EXT_MA1;
extension1.SetDefaultColor(CreateColor(0, 255, 255));
extension1.SetLineWeight(1);

#Clouds
input showclouds  = yes;
addcloud(if showclouds then SMAD50 else double.nan, SMAD200, color.green, color.red);
addcloud(if showclouds then extension else double.nan, extension1, color.green, color.red);


#Bubbles
input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD50[bm], "SMA50 D",
               SMAD50.TakeValueColor());
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD200[bm], "SMA200 D",
               SMAD200.TakeValueColor());
 
The daily SMAs are very important to my trading. Right now I can only have the Daily chart open in order to see what the value of the daily (period) SMAs because I have added the stock SMA study (indicator) to my daily chart.

Does anyone know how to write a custom script that shows the Daily period SMA values as live/moving price lines on the lower time frame charts?
 
The daily SMAs are very important to my trading. Right now I can only have the Daily chart open in order to see what the value of the daily (period) SMAs because I have added the stock SMA study (indicator) to my daily chart.

Does anyone know how to write a custom script that shows the Daily period SMA values as live/moving price lines on the lower time frame charts?

Here is one I did that I found through this site's search engine https://usethinkscript.com/threads/...averages-in-the-upper-chart.12234/post-104933
 
This is great! Do you know how I would modify it so it only plots for 2-3 days? The current day and the last 1-2 days before? So if my chart's timeframe is set to display the last 10 days, it only plots back the last 2-3 days and then stops?
 
This is great! Do you know how I would modify it so it only plots for 2-3 days? The current day and the last 1-2 days before? So if my chart's timeframe is set to display the last 10 days, it only plots back the last 2-3 days and then stops?

Sure, the input daysdisplayed includes the current/last day.

Capture.jpg
Ruby:
input daysdisplayed   = 3;
input aggperiod       = {current, default manual} ;
input aggregation     = AggregationPeriod.DAY;
input showonexpansion = no;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else if Between(GetDay(), GetLastDay() - daysdisplayed + 1, GetLastDay())
               then SimpleMovingAvg (close (period = if aggperiod == aggperiod.current
                                                     then GetAggregationPeriod()
                                                     else aggregation), 50)
               else Double.NaN ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA50D ;


def inertline = InertiaAll(SMAD50, 2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);

def SMA200D  = if IsNaN(close)
               then SMA200D[1]
               else if Between(GetDay(), GetLastDay() - daysdisplayed + 1, GetLastDay())
               then SimpleMovingAvg (close (period = if aggperiod == aggperiod.current
                                                     then GetAggregationPeriod()
                                                     else aggregation), 200)
               else double.nan ;
plot SMAD200 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA200D ;

def inertline1 = InertiaAll(SMAD200, 2);
def EXT_MA1 = if !IsNaN(close()) then inertline1 else EXT_MA1[1] + ((EXT_MA1[1] - EXT_MA1[2]) / (2 - 1));
plot extension1 = EXT_MA1;
extension1.SetDefaultColor(CreateColor(0, 255, 255));
extension1.SetLineWeight(1);

#Clouds
input showclouds  = yes;
AddCloud(if showclouds then SMAD50 else Double.NaN, SMAD200, Color.GREEN, Color.RED);
AddCloud(if showclouds then extension else Double.NaN, extension1, Color.GREEN, Color.RED);


#Bubbles
input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD50[bm], "SMA50 D",
               SMAD50.TakeValueColor());
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD200[bm], "SMA200 D",
               SMAD200.TakeValueColor());
 
You are a genius. At the risk of asking too much of you, would you know how I can make the bubble dynamically display the current price level of the SMA? For example, instead of it saying "SMA200 D", it says "298.35"?
 
You are a genius. At the risk of asking too much of you, would you know how I can make the bubble dynamically display the current price level of the SMA? For example, instead of it saying "SMA200 D", it says "298.35"?

This has 2 new inputs to display the current bubble SMA text and/or the SMA price in the bubble.

The image shows yes to both inputs

Capture.jpg
Ruby:
input daysdisplayed      = 3;
input SMAprice_in_bubble = yes;
input SMAtext_in_bubble  = yes;
input aggperiod          = {current, default manual} ;
input aggregation        = AggregationPeriod.DAY;
input showonexpansion    = no;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else if Between(GetDay(), GetLastDay() - daysdisplayed + 1, GetLastDay())
               then SimpleMovingAvg (close (period = if aggperiod == aggperiod.current
                                                     then GetAggregationPeriod()
                                                     else aggregation), 50)
               else Double.NaN ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA50D ;
SMAD50.hidebubble();

def inertline = InertiaAll(SMAD50, 2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);
extension.hidebubble();

def SMA200D  = if IsNaN(close)
               then SMA200D[1]
               else if Between(GetDay(), GetLastDay() - daysdisplayed + 1, GetLastDay())
               then SimpleMovingAvg (close (period = if aggperiod == aggperiod.current
                                                     then GetAggregationPeriod()
                                                     else aggregation), 200)
               else double.nan ;

plot SMAD200 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA200D ;
SMAD200.hidebubble();

def inertline1 = InertiaAll(SMAD200, 2);
def EXT_MA1 = if !IsNaN(close()) then inertline1 else EXT_MA1[1] + ((EXT_MA1[1] - EXT_MA1[2]) / (2 - 1));
plot extension1 = EXT_MA1;
extension1.SetDefaultColor(CreateColor(0, 255, 255));
extension1.SetLineWeight(1);
extension1.hidebubble();

#Clouds
input showclouds  = yes;
AddCloud(if showclouds then SMAD50 else Double.NaN, SMAD200, Color.GREEN, Color.RED);
AddCloud(if showclouds then extension else Double.NaN, extension1, Color.GREEN, Color.RED);


#Bubbles
input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;

AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD50[bm], (if SMAtext_in_bubble then "SMA50 D\n" else "") +
                           (if SMAprice_in_bubble then astext(SMAD50[bm]) else ""),
               SMAD50.TakeValueColor(),
               if SMAD50[bm] > SMAD200[bm] then yes else no);
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD200[bm],(if SMAtext_in_bubble then "SMA200 D\n" else "") +
                           (if SMAprice_in_bubble then astext(SMAD200[bm]) else ""),
               SMAD200.TakeValueColor(),
               if SMAD200[bm] > SMAD50[bm] then yes else no);
;
 
Last question... I hope. Is it possible to decrease the line to hours instead of days? There probably isn't a "hoursdisplayed". Or does this Priceline need to extend to the beginning of the day at a minimum?

Ok maybe one more.... How do I add Curve.FIRM so line follows a firm curve rather than a stair-stepped?
 
Last edited by a moderator:
Last question... I hope. Is it possible to decrease the line to hours instead of days? There probably isn't a "hoursdisplayed". Or does this Priceline need to extend to the beginning of the day at a minimum?

Ok maybe one more.... How do I add Curve.FIRM so line follows a firm curve rather than a stair-stepped?

Modifications to the script now allow you to choose lengths and averagetypes for 2 moving averages, and periods displayed to coincide with whatever aggregation is used less than or equal to daily aggregations. The bubbles will now display the averagetype and aggregation period.

Added also is setstyle(curve.firm). However, this does not smooth the higher aggregations lines showing on a lower aggregation. TOS does not have any built-in function to do this. I have done some workaround ones that I have not thoroughly tested that smooth the lines.

Ruby:
input periodsdisplayed  = 3;
input MAprice_in_bubble = yes;
input MAtext_in_bubble  = yes;
input aggperiod          = {current, default manual} ;
input aggregation        = AggregationPeriod.HOUR;
input length1            = 50;
input length2            = 200;
input avgtype1           = AverageType.SIMPLE;
input avgtype2           = AverageType.SIMPLE;
input showonexpansion    = no;

def bn       = BarNumber();
def lastbar  = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then bn else Double.NaN);
def aggbars  = (if aggperiod == aggperiod.current
                then GetAggregationPeriod()
                else aggregation) / 60000 / (GetAggregationPeriod() / 60000) * periodsdisplayed;

def MA1_     = if IsNaN(close)
               then MA1_[1]
               else if if aggperiod == aggperiod.manual and
                          aggregation == AggregationPeriod.DAY
                       then Between(GetDay(),
                                    GetLastDay() - periodsdisplayed + 1,
                                    GetLastDay())
                       else Between(bn, lastbar - aggbars, lastbar)
               then MovingAverage (avgtype1, close (period =
                                             if aggperiod == aggperiod.current
                                             then GetAggregationPeriod()
                                             else aggregation), length1)
               else Double.NaN ;



plot MA1     = if showonexpansion and !IsNaN(close)
               then Double.NaN
                else MA1_ ;
MA1.SetStyle(Curve.FIRM);
MA1.HideBubble();

def inertline = InertiaAll(MA1, 2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);
extension.HideBubble();

def MA2_     = if IsNaN(close)
               then MA2_[1]
               else if if aggperiod == aggperiod.manual and
                          aggregation == AggregationPeriod.DAY
                          then Between(GetDay(),
                                       GetLastDay() - periodsdisplayed + 1,
                                       GetLastDay())
                          else Between(bn, lastbar - aggbars, lastbar)
               then MovingAverage (avgtype2, close (period = if aggperiod == aggperiod.current
                                                     then GetAggregationPeriod()
                                                     else aggregation), length2)
               else Double.NaN ;

plot MA2     = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else MA2_ ;
MA2.SetStyle(Curve.FIRM);
MA2.HideBubble();

def inertline1 = InertiaAll(MA2, 2);
def EXT_MA1 = if !IsNaN(close()) then inertline1 else EXT_MA1[1] + ((EXT_MA1[1] - EXT_MA1[2]) / (2 - 1));
plot extension1 = EXT_MA1;
extension1.SetDefaultColor(CreateColor(0, 255, 255));
extension1.SetLineWeight(1);
extension1.HideBubble();

#Clouds
input showclouds  = yes;
AddCloud(if showclouds then MA1 else Double.NaN, MA2, Color.GREEN, Color.RED);
AddCloud(if showclouds then extension else Double.NaN, extension1, Color.GREEN, Color.RED);


#Bubbles
input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;

AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
                  MA1[bm], (if MAtext_in_bubble
                            then
                           (if avgtype1 == AverageType.EXPONENTIAL
                            then "E"
                            else if avgtype1 == AverageType.HULL
                            then "H"
                            else if avgtype1 == AverageType.SIMPLE
                            then "S"
                            else if avgtype1 == AverageType.WEIGHTED
                            then "WGT"
                            else "W"
                            ) + "MA " + length1 +
                           (if aggperiod == aggperiod.manual
                            then if aggregation == AggregationPeriod.DAY
                                 then " D\n"
                                 else if aggregation == AggregationPeriod.HOUR
                                 then " H\n"
                                 else (" " + aggregation / 60000 + "m\n")   
                             else " " )                         
                             else "") +
                           (if MAprice_in_bubble then AsText(MA1[bm]) else ""),
               MA1.TakeValueColor(),
               if MA1[bm] > MA2[bm] then yes else no);

AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
                  MA2[bm], (if MAtext_in_bubble
                            then
                           (if avgtype2 == AverageType.EXPONENTIAL
                            then "E"
                            else if avgtype2 == AverageType.HULL
                            then "H"
                            else if avgtype2 == AverageType.SIMPLE
                            then "S"
                            else if avgtype2 == AverageType.WEIGHTED
                            then "WGT"
                            else "W"
                            ) + "MA " + "" + length2 +
                           (if aggperiod == aggperiod.manual
                            then if aggregation == AggregationPeriod.DAY
                                 then " D\n"
                                 else if aggregation == AggregationPeriod.HOUR
                                 then " H\n"
                                 else (" " + aggregation / 60000 + "m\n")   
                             else " " )                         
                             else " ")  +
                           (if MAprice_in_bubble then AsText(MA2[bm]) else ""),
               MA2.TakeValueColor(),
               if MA2[bm] > MA1[bm] then yes else no);
;
Capture.jpg
 
Last edited:

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