How to Fill Gaps in AddCloud function

shalinar

New member
Hello, part of the indicator that I'm writing includes a background via AddCloud that colors itself based on the strength of the trend (roughly: "weak bullish", "strong bullish", "weak bearish", "strong bearish", or "none").

The cloud takes into account whether MACD is > 0 or not, the MACD diff, and what the ADX is.

The issue I'm trying to solve is: when the trend changes (most commonly from the MACD diff flipping +/-), AddCloud leaves a gap between the previous cloud and the new cloud. Why is that? What's the best way to fill the gap? I've tried a few things unsuccessfully, so I'm asking for help here.

Lr4J5En.png


If you look closely, the gap is between candles, when one of the conditions changes (easiest to see when MACD line crosses the signal line).

Here's the relevant code:
Code:
declare lower;

input OverboughtOversold = 0.8;

def macd = MACD(fast_length = 12, slow_length = 26, macd_length = 9);
def adx = ADX(14);

def bulltrend = macd().Diff < 0 and macd > 0 and adx > 20;
def beartrend = macd().Diff > 0 and macd < 0 and adx > 20;
def strongbulltrend = macd().Diff > 0 and macd > 0 and adx > 20;
def strongbeartrend = macd().Diff < 0 and macd < 0 and adx > 20;

def bullcloudL = if bulltrend then OverboughtOversold else Double.NaN;
def bullcloudH = if bulltrend then -OverboughtOversold else Double.NaN;
AddCloud(bullcloudL, bullcloudH, Color.DARK_GREEN, Color.DARK_RED, yes);

def bearcloudL = if beartrend then OverboughtOversold else Double.NaN;
def bearcloudH = if beartrend then -OverboughtOversold else Double.NaN;
AddCloud(bearcloudL, bearcloudH, Color.DARK_RED, Color.DARK_GREEN, yes);

def strongbullcloudL = if strongbulltrend then OverboughtOversold else Double.NaN;
def strongbullcloudH = if strongbulltrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbullcloudL, strongbullcloudH, Color.GREEN, Color.RED, yes);

def strongbearcloudL = if strongbeartrend then OverboughtOversold else Double.NaN;
def strongbearcloudH = if strongbeartrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbearcloudL, strongbearcloudH, Color.RED, Color.GREEN, yes);

Related: I've also seen similar uses of AddCloud where the transition between the different values has a "triangular" look to it instead of just cutting off abruptly:
S39B8dk.png

I cannot figure out how to replicate this either.

Any help would be much appreciated!
 
Solution
Hello, part of the indicator that I'm writing includes a background via AddCloud that colors itself based on the strength of the trend (roughly: "weak bullish", "strong bullish", "weak bearish", "strong bearish", or "none").

The cloud takes into account whether MACD is > 0 or not, the MACD diff, and what the ADX is.

The issue I'm trying to solve is: when the trend changes (most commonly from the MACD diff flipping +/-), AddCloud leaves a gap between the previous cloud and the new cloud. Why is that? What's the best way to fill the gap? I've tried a few things unsuccessfully, so I'm asking for help here.



If you look closely, the gap is between candles, when one of the conditions changes (easiest to see when MACD line...
Hello, part of the indicator that I'm writing includes a background via AddCloud that colors itself based on the strength of the trend (roughly: "weak bullish", "strong bullish", "weak bearish", "strong bearish", or "none").

The cloud takes into account whether MACD is > 0 or not, the MACD diff, and what the ADX is.

The issue I'm trying to solve is: when the trend changes (most commonly from the MACD diff flipping +/-), AddCloud leaves a gap between the previous cloud and the new cloud. Why is that? What's the best way to fill the gap? I've tried a few things unsuccessfully, so I'm asking for help here.

Lr4J5En.png


If you look closely, the gap is between candles, when one of the conditions changes (easiest to see when MACD line crosses the signal line).

Here's the relevant code:
Code:
declare lower;

input OverboughtOversold = 0.8;

def macd = MACD(fast_length = 12, slow_length = 26, macd_length = 9);
def adx = ADX(14);

def bulltrend = macd().Diff < 0 and macd > 0 and adx > 20;
def beartrend = macd().Diff > 0 and macd < 0 and adx > 20;
def strongbulltrend = macd().Diff > 0 and macd > 0 and adx > 20;
def strongbeartrend = macd().Diff < 0 and macd < 0 and adx > 20;

def bullcloudL = if bulltrend then OverboughtOversold else Double.NaN;
def bullcloudH = if bulltrend then -OverboughtOversold else Double.NaN;
AddCloud(bullcloudL, bullcloudH, Color.DARK_GREEN, Color.DARK_RED, yes);

def bearcloudL = if beartrend then OverboughtOversold else Double.NaN;
def bearcloudH = if beartrend then -OverboughtOversold else Double.NaN;
AddCloud(bearcloudL, bearcloudH, Color.DARK_RED, Color.DARK_GREEN, yes);

def strongbullcloudL = if strongbulltrend then OverboughtOversold else Double.NaN;
def strongbullcloudH = if strongbulltrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbullcloudL, strongbullcloudH, Color.GREEN, Color.RED, yes);

def strongbearcloudL = if strongbeartrend then OverboughtOversold else Double.NaN;
def strongbearcloudH = if strongbeartrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbearcloudL, strongbearcloudH, Color.RED, Color.GREEN, yes);

Related: I've also seen similar uses of AddCloud where the transition between the different values has a "triangular" look to it instead of just cutting off abruptly:
S39B8dk.png

I cannot figure out how to replicate this either.

Any help would be much appreciated!
Cloud gaps occur when using multiple addcloud statements.
The gap is based on the type of logic.
The "cutting off abruptly" occurs with boolean logic which are abrupt. Yes/No, true/false, 1/0...
Triangular results are not boolean-based.
 
Last edited:

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

Hello, part of the indicator that I'm writing includes a background via AddCloud that colors itself based on the strength of the trend (roughly: "weak bullish", "strong bullish", "weak bearish", "strong bearish", or "none").

The cloud takes into account whether MACD is > 0 or not, the MACD diff, and what the ADX is.

The issue I'm trying to solve is: when the trend changes (most commonly from the MACD diff flipping +/-), AddCloud leaves a gap between the previous cloud and the new cloud. Why is that? What's the best way to fill the gap? I've tried a few things unsuccessfully, so I'm asking for help here.



If you look closely, the gap is between candles, when one of the conditions changes (easiest to see when MACD line crosses the signal line).

Here's the relevant code:
Code:
declare lower;

input OverboughtOversold = 0.8;

def macd = MACD(fast_length = 12, slow_length = 26, macd_length = 9);
def adx = ADX(14);

def bulltrend = macd().Diff < 0 and macd > 0 and adx > 20;
def beartrend = macd().Diff > 0 and macd < 0 and adx > 20;
def strongbulltrend = macd().Diff > 0 and macd > 0 and adx > 20;
def strongbeartrend = macd().Diff < 0 and macd < 0 and adx > 20;

def bullcloudL = if bulltrend then OverboughtOversold else Double.NaN;
def bullcloudH = if bulltrend then -OverboughtOversold else Double.NaN;
AddCloud(bullcloudL, bullcloudH, Color.DARK_GREEN, Color.DARK_RED, yes);

def bearcloudL = if beartrend then OverboughtOversold else Double.NaN;
def bearcloudH = if beartrend then -OverboughtOversold else Double.NaN;
AddCloud(bearcloudL, bearcloudH, Color.DARK_RED, Color.DARK_GREEN, yes);

def strongbullcloudL = if strongbulltrend then OverboughtOversold else Double.NaN;
def strongbullcloudH = if strongbulltrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbullcloudL, strongbullcloudH, Color.GREEN, Color.RED, yes);

def strongbearcloudL = if strongbeartrend then OverboughtOversold else Double.NaN;
def strongbearcloudH = if strongbeartrend then -OverboughtOversold else Double.NaN;
AddCloud(strongbearcloudL, strongbearcloudH, Color.RED, Color.GREEN, yes);

Related: I've also seen similar uses of AddCloud where the transition between the different values has a "triangular" look to it instead of just cutting off abruptly:

I cannot figure out how to replicate this either.

Any help would be much appreciated!

there are gaps, because you are using 2+ addclouds.
when 1 cloud stops and another starts, there will be a gap between 2 bars because there is 2 different color signals on the 2 bars, green, red, red green,...

to get rid of the gap, you can add a condition to check if the next bar has the desired signal.
. def bullcloudL = if bulltrend or bulltrend[-1] then ...

clouds can draw shading between 2 price levels, at 2 colors.
if the first price is bigger than the 2nd price , then the 1st color is used. if the 2nd price is bigger, then the 2nd color is used.
when a transition happens, the price levels swap, and cause the x in between the 2 colors.
to get rid of this, use 2+ addclouds and just 1 color in each

change the numbers in this test code, to see what happens
addcloud(2,3,color.green, color.red);


Code:
# cloud_gaps

#https://usethinkscript.com/threads/how-to-fill-gaps-in-addcloud-function.17946/
#How to Fill Gaps in AddCloud function
#shalinar  2/27

#Hello, part of the indicator that I'm writing includes a background via AddCloud that colors itself based on the strength of the trend (roughly: "weak bullish", "strong bullish", "weak bearish", "strong bearish", or "none").

#The cloud takes into account whether MACD is > 0 or not, the MACD diff, and what the ADX is.

#The issue I'm trying to solve is: when the trend changes (most commonly from the MACD diff flipping +/-), AddCloud leaves a gap between the previous cloud and the new cloud. Why is that? What's the best way to fill the gap? I've tried a few things unsuccessfully, so I'm asking for help here.


#If you look closely, the gap is between candles, when one of the conditions changes (easiest to see when MACD line crosses the signal line).


declare lower;

input OverboughtOversold = 0.8;

def macd = MACD(fast_length = 12, slow_length = 26, macd_length = 9);
def adx = ADX(14);

def bulltrend = macd().Diff < 0 and macd > 0 and adx > 20;
def beartrend = macd().Diff > 0 and macd < 0 and adx > 20;
def strongbulltrend = macd().Diff > 0 and macd > 0 and adx > 20;
def strongbeartrend = macd().Diff < 0 and macd < 0 and adx > 20;

# add xx[-1] variable to start cloud 1 bar earlier , to remove gaps
def bullcloudL = if bulltrend or bulltrend[-1] then OverboughtOversold else Double.NaN;
def bullcloudH = if bulltrend or bulltrend[-1] then -OverboughtOversold else Double.NaN;
AddCloud(bullcloudL, bullcloudH, Color.DARK_GREEN, Color.DARK_RED, yes);

def bearcloudL = if beartrend or beartrend[-1] then OverboughtOversold else Double.NaN;
def bearcloudH = if beartrend or beartrend[-1] then -OverboughtOversold else Double.NaN;
AddCloud(bearcloudL, bearcloudH, Color.DARK_RED, Color.DARK_GREEN, yes);

def strongbullcloudL = if strongbulltrend or strongbulltrend[-1] then OverboughtOversold else Double.NaN;
def strongbullcloudH = if strongbulltrend or strongbulltrend[-1] then -OverboughtOversold else Double.NaN;
AddCloud(strongbullcloudL, strongbullcloudH, Color.GREEN, Color.RED, yes);

def strongbearcloudL = if strongbeartrend or strongbeartrend[-1] then OverboughtOversold else Double.NaN;
def strongbearcloudH = if strongbeartrend or strongbeartrend[-1] then -OverboughtOversold else Double.NaN;
AddCloud(strongbearcloudL, strongbearcloudH, Color.RED, Color.GREEN, yes);


# test code , change the numbers to see what happens
#addcloud(2,3,color.green, color.red);
#
 
Last edited:
Solution
The "cutting off abruptly" occurs with boolean logic which are abrupt. Yes/No, true/false, 1/0...
Triangular results are not boolean-based.

With all due respect I actually don't think this part is correct. The screenshot I provided of the triangular/"X" transition shape is directly from Simpler Trading's "Ready Fire Aim" indicator, which uses boolean logic:
IF4oLX0.png


With some experimentation, it seems to come down entirely to what @halcyonguy said. Basically, the triangular/X transition shape only occurs when the same AddCloud paints both shapes. Since each of my colors were being painted by a separate AddCloud call, none of them could transition to each other.

I've since consolidated down to two clouds and I can confirm that the X transition shape shows up within the two conditions painted by the same AddCloud function.

The gap remains between where the colors are painted by different AddCloud functions, but I can use the xx[-1] example to paint over those. Some overlapping occurs which I'll have to sort out but it's a solution.

Thank you both for your answers, with those and some extra testing I was able to bring this script to a place where I'm happy with it.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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