Clouds with If-Then-Else For ThinkOrSwim

PT_Scalper

Member
VIP
I'm wondering if someone knows if there is a way to add a conditional cloud color change with thinkscript.

So typically from what I have seen you can add a cloud with something like this;

AddCloud(OpenPrice, ClosePrice, color.RED, color.GREEN, yes);

This is pretty static where if it's A, then color is red and if B color is green.

Is there a way to change the color of the cloud based on a criteria? Meaning if say price is going up, green if going down red? More of a dynamic setting than a static setting.

I hope this makes sense.
 
I'm wondering if someone knows if there is a way to add a conditional cloud color change with thinkscript.

So typically from what I have seen you can add a cloud with something like this;
AddCloud(OpenPrice, ClosePrice, color.RED, color.GREEN, yes);

This is pretty static where if it's A, then color is red and if B color is green.

Is there a way to change the color of the cloud based on a criteria? Meaning if say price is going up, green if going down red? More of a dynamic setting than a static setting.

I hope this makes sense.
Technically, @halcyonguy is correct.
However, there is a workaround.
You can have a series of AddCloud statements for every color that you want to show.

iiAzzbA.png

Rich (BB code):
def BBupper = reference BollingerBands()."UpperBand" ;
def BBlower = reference BollingerBands()."LowerBand" ;


AddCloud(if close>=close[1] then BBlower else double.NaN, BBupper, color.cyan, color.cyan);
AddCloud(if open<=open[1] then BBlower else double.NaN, BBupper, color.orange, color.orange);
AddCloud(if close<close[1] then BBlower else double.NaN, BBupper, color.orange, color.orange);
AddCloud(if open>open[1] then BBlower else double.NaN, BBupper, color.cyan, color.cyan);

# this is the same thing, but an abbreviated way to say if-then-else:
AddCloud(if(close>=close[1], BBlower, double.NaN), BBupper, color.cyan, color.cyan);
AddCloud(if(open<=open[1], BBlower, double.NaN), BBupper, color.orange, color.orange);
AddCloud(if(close<close[1], BBlower, double.NaN), BBupper, color.orange, color.orange);
AddCloud(if(open>open[1], BBlower, double.NaN), BBupper, color.cyan, color.cyan);

Your question is: How to find the syntax for a plotted element in a native ToS Indicator?
  1. Click on your script
  2. Click on Inspector on the right-hand side of the box
  3. The Inspector is the same as the Condition Wizard in Scan Hacker.
  4. Here is a tutorial: https://usethinkscript.com/threads/how-to-use-thinkorswim-stock-hacker-scans.284/
GRJv6BX.png
 
Last edited:
Hi @halcyonguy,

Thanks for the additional info. Ok so this might help post my question the right way.

See the code below and I'll explain what's not working for me;

#INPUTS
#==============================================================================================

input iAggregationPeriod = AggregationPeriod.four_hours;
input iHideMidLinePrimary = no;
input iDisplacePrimary = 0;
input iFactorPrimary = 1.5;
input iLengthPrimary = 20;
input iPricePrimary = {OPEN, HIGH, LOW, default CLOSE, HL2, HLC3, OHLC4};
input iAvgTypePrimary = AverageType.SIMPLE;
input iTrueRangeAvgTypePrimary = AverageType.SIMPLE;

input iDisplace = 0;
input iFactor = 1.8;
input iLength = 20;
input iPrice = {OPEN, HIGH, LOW, default CLOSE, HL2, HLC3, OHLC4};
input iAvgType = AverageType.SIMPLE;
input iTrueRangeAvgType = AverageType.SIMPLE;

#VARS & LOGIC
#==============================================================================================

def vShiftPrimary = iFactorPrimary * MovingAverage(iTrueRangeAvgTypePrimary,
TrueRange(high(period = iAggregationPeriod),
close(period = iAggregationPeriod),
low(period = iAggregationPeriod)),
iLengthPrimary);

def vAveragePrimary;
if iPricePrimary == iPricePrimary.OPEN
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, open(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.HIGH
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, high(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.LOW
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, low(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.CLOSE
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, close(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.HL2
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, hl2(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.HLC3
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, hlc3(period = iAggregationPeriod), iLengthPrimary);
}
else if iPricePrimary == iPricePrimary.OHLC4
then {
vAveragePrimary = MovingAverage(iAvgTypePrimary, ohlc4(period = iAggregationPeriod), iLengthPrimary);
}
else {
vAveragePrimary = Double.NaN;
}

#MACD Primer Cloud
#==============================================================================================

def c = close(period = iAggregationPeriod);

input fastLength0 = 12;
input slowLength0 = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

plot ZeroLine = 0;
zeroline.hide();

def Value = MovingAverage(averageType, c, fastLength0) - MovingAverage(averageType, c, slowLength0);
def Avg0 = MovingAverage(averageType, Value, MACDLength);

def Diff = Value - Avg0;

#PLOTS
#==============================================================================================

plot pAvgPrimary = vAveragePrimary[-iDisplacePrimary];
#pAvgPrimary.hide();
plot pUpperBandPrimary = vAveragePrimary[-iDisplacePrimary] + vShiftPrimary[-iDisplacePrimary];
pUpperBandPrimary.SetDefaultColor(color.gray);
plot pLowerBandPrimary = vAveragePrimary[-iDisplacePrimary] - vShiftPrimary[-iDisplacePrimary];
pLowerBandPrimary.SetDefaultColor(color.gray);

pAvgPrimary.AssignValueColor(color.white);
pAvgPrimary.SetLineWeight(1);
pAvgPrimary.SetStyle(Curve.short_DASH);

#CLOUDS
#==============================================================================================

Addcloud(if value > zeroline then double.nan else plowerBandPrimary, pavgPrimary, color.black, color.black);
AddCloud(if value > Zeroline and value > avg0 then Double.NaN else plowerBandPrimary, pavgprimary, color.red, color.red);
AddCloud(if value > Zeroline and value < avg0 then Double.NaN else plowerBandPrimary, pavgprimary, color.green, color.green);

Addcloud(if value < zeroline then double.nan else pavgPrimary, pupperBandPrimary, color.black, color.black);
AddCloud(if value < Zeroline and value < avg0 then Double.NaN else pavgprimary, pupperBandPrimary, color.green, color.green);
AddCloud(if value < Zeroline and value > avg0 then Double.NaN else pavgprimary, pupperBandPrimary, color.red, color.red);

This will color the clouds based on MACD above or below signal, and above or below zero using the Keltner Channels top and bottom bands, and Keltner Midline... This is a MTF study.

So anything Green you see is MACD above the signal line and anything Red is MACD below the signal line. If MACD is above the Zeroline, the cloud is between the Kelt mid and Kelt bottom, and if MACD is below the Zeroline, the cloud is between the Kelt mid and Kelt top.

I just cannot get the non-Green and non-Red areas to say show black or not fill the cloud. Not sure if this is possible.

The Red vertical line shows the above and below Zeroline to help with my illustration.

V1uN0Bg.png


I hope this explains it better and I'm curious how to set the variables.

Regards,
PT
 
I would like to add a color cloud to this custom script such that if the current close is > Price (as defined-which is the -21 displaced High)) then add a green color cloud between the values of current close and Price (as defined). Below is the code:

input price = high;
input price2 = low;
input displace = -21;
def Displaced_High = high;

plot HighDP = price[-displace];
plot HighLP = price2[-displace];

Similarly I would like to create a red color cloud if current close < Price2 between the values of the current close and Price2
 

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