• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Cloud conditions based on MACD and CCI?

Whats going on, I am writing a script right now that includes clouds. I am wondering how I can write the script to allow for the clouds to fill with a color when specific criteria is met. This is the script I want the criteria to be macd on 4hr to be above zero and cci above 100, and vise versa for the bottom cloud

Code:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input timeframe = AggregationPeriod.DAY;
input timeframe1 = aggregationperiod.day;
input length1 = 21;
input num_dev_dn1 = -2;
input num_dev_up1 = 2;

def sDev = stdev(data = close(period= timeframe) [-displace], length = length);
def sdev1 = stdev(data = close(period= timeframe1) [-displace], length = length1);


#upper
plot MidLine = MovingAverage(averageType, data = close (period=timeframe)[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;


LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

plot MidLine1 = MovingAverage(averageType, data = close (period=timeframe)[-displace], length = length);
plot LowerBand1 = MidLine + num_Dev_Dn1 * sDev1;
plot UpperBand1 = MidLine + num_Dev_Up1 * sDev1;
plot mid = (lowerband1 + lowerband)/2;
plot mid1 = (upperband1 + upperband)/2;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

addcloud(lowerband,lowerband1,color.gray);
addcloud(upperband1,upperband,color.gray);
 
This is the new script with the macd and cci added

Code:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input timeframe = AggregationPeriod.DAY;
input timeframe1 = aggregationperiod.day;
input length1 = 21;
input num_dev_dn1 = -2;
input num_dev_up1 = 2;

def sDev = stdev(data = close(period= timeframe) [-displace], length = length);
def sdev1 = stdev(data = close(period= timeframe1) [-displace], length = length1);

plot MidLine = MovingAverage(averageType, data = close (period=timeframe)[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;


LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

plot MidLine1 = MovingAverage(averageType, data = close (period=timeframe)[-displace], length = length);
plot LowerBand1 = MidLine + num_Dev_Dn1 * sDev1;
plot UpperBand1 = MidLine + num_Dev_Up1 * sDev1;
plot mid = (lowerband1 + lowerband)/2;
plot mid1 = (upperband1 + upperband)/2;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

addcloud(lowerband,lowerband1,color.gray);
addcloud(upperband1,upperband,color.gray);

#cci
input aggregationPeriod = AggregationPeriod.HOUR;
def high = high(period = aggregationPeriod);
def low = low(period = aggregationPeriod);
def close = close(period = aggregationPeriod);
input length2 = 14;
input over_sold = -100;
input over_bought = 100;
input showBreakoutSignals = no;

def price1 = close + low + high;
def linDev = lindev(price1, length2);
plot CCI = if linDev == 0 then 0 else (price - Average(price1, length2)) / linDev / 0.015;
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;
plot UpSignal = if CCI crosses above ZeroLine then ZeroLine else Double.Nan;
plot DownSignal = if CCI crosses below ZeroLine then ZeroLine else Double.Nan;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

CCI.setDefaultColor(GetColor(9));
OverBought.setDefaultColor(GetColor(5));
ZeroLine.setDefaultColor(GetColor(5));
OverSold.setDefaultColor(GetColor(5));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


#macd
input aggregationPeriod1 = AggregationPeriod.HOUR;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType1 = AverageType.EXPONENTIAL;
input showBreakoutSignals1 = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine1 = 0;

plot UpSignal1 = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal1 = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
I removed the unnecessary code on the macd and cci but I do not know how to do the code for the clouds have been stuck at this part for some time.

Would be greatly appreciated if I could get some assistance :)
 
when I type in cci it says expected double bottom and no such variable as cci. I am also guessing once that def is complete the plot would look something like this? @generic

plot upperband2 = if cloud_on then addcloud(upperband1,upperband,color.gray);
 

generic

Active member
@aceboogie_11 You're adding too many lines of code without testing them and using different agg for everything makes it hard to test. I tested this using 1 min agg on AAL.
Code:
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input timeframe = AggregationPeriod.DAY;
input timeframe1 = aggregationperiod.day;
input length1 = 21;
input num_dev_dn1 = -2.0;
input num_dev_up1 = 2.0;

def nan = Double.NaN;
def sDev = StDev(data = close(period = timeframe), length = length);
def sdev1 = stdev(data = close(period = timeframe1), length = length1);

plot MidLine = MovingAverage(averageType, data = close(period = timeframe), length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

plot MidLine1 = MovingAverage(averageType, data = close (period=timeframe1), length = length1);
plot LowerBand1 = MidLine + num_Dev_Dn1 * sDev1;
plot UpperBand1 = MidLine + num_Dev_Up1 * sDev1;
plot mid = (lowerband1 + lowerband)/2;
plot mid1 = (upperband1 + upperband)/2;
LowerBand1.SetDefaultColor(GetColor(0));
MidLine1.SetDefaultColor(GetColor(1));
UpperBand1.SetDefaultColor(GetColor(5));

#macd
input aggregationPeriod1 = AggregationPeriod.HOUR;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypemacd = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageTypemacd, close(period = aggregationPeriod1), fastLength) - MovingAverage(averageTypemacd, close(period = aggregationPeriod1), slowLength);
def Avg = MovingAverage(averageTypemacd, Value, MACDLength);

def Diff = Value - Avg;

#cci
input aggregationPeriod = AggregationPeriod.HOUR;
input lengthcci = 14;
input over_sold = -100;
input over_bought = 100;

def pricecci = close(period = aggregationPeriod) + low(period = aggregationPeriod) + high(period = aggregationPeriod);
def linDev = lindev(pricecci, lengthcci);
def CCI = if linDev == 0 then 0 else (pricecci - Average(pricecci, lengthcci)) / linDev / 0.015;

#cloud
def cloud_on = Diff > 0 and CCI > 100;

plot lowercloud = if cloud_on then lowerband else nan;
plot lowercloud1 = if cloud_on then lowerband1 else nan;
plot uppercloud = if cloud_on then upperband else nan;
plot uppercloud1 = if cloud_on then upperband1 else nan;

lowercloud.Hide();
lowercloud1.Hide();
uppercloud.Hide();
uppercloud1.Hide();

AddCloud(lowercloud, lowercloud1, color.GREEN, color.RED);
AddCloud(uppercloud, uppercloud1, color.GREEN, color.RED);
 
Hey I tried out the code and the code is splotchy on the filling because of the different aggregation periods. I have the bollinger bands set to four hours and I tried to have the CCI and Macd also set to those times but for some reason it still isnt plotting it correctly. Wondering how I can fix the script so that those are all on the 4 hour, because I want it to be plotted on a 15 min chart. Thanks! @generic
 

btran99

New member
I have a similar question regarding RSI. How do I add cloud to the overbought and oversold areas only. I was playing around with the coding but it was like a plumber being toss into an MBA program.
Thank you.
 

MerryDay

Well-known member
VIP
@btran There are sooooo many examples of RSI ob/os clouds on this forum. I should give you the spiel that you need to search this forum, attempt to cut&paste the cloud snippet from one of the many many scripts and if still having problems. Post an image showing the error messages and we can help you at that point.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/

However, I have been that plumber in an MBA program! I think your sense of humor earns you a buy this time. Here is the code:
Ruby:
declare lower ;
input ob = 70 ;
input os = 30;
plot RSI = RSI("length" = 14, "over bought" = ob, "over sold" = os)."RSI" ;

DefineGlobalColor("obcloud", CreateColor(217,61,255)) ;
DefineGlobalColor("oscloud", CreateColor(255,216,61)) ;
AddCloud(0, os, GlobalColor("oscloud"), GlobalColor("oscloud"));
AddCloud(100, ob, GlobalColor("obcloud"), GlobalColor("obcloud"));
UbWdRMC.png

To Change The Cloud Colors:
  1. Click on the gear next to the study
  2. Click on global colors at the bottom of the box
  3. Click on the color, you want to change.
EywvbrG.png
 
Last edited:

btran99

New member
@btran There are sooooo many examples of RSI ob/os clouds on this forum. I should give you the spiel that you need to search this forum, attempt to cut&paste the cloud snippet from one of the many many scripts and if still having problems. Post an image showing the error messages and we can help you at that point.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/

However, I have been that plumber in an MBA program! I think your sense of humor earns you a buy this time. Here is the code:
Ruby:
declare lower ;
input ob = 70 ;
input os = 30;
plot RSI = RSI("length" = 14, "over bought" = ob, "over sold" = os)."RSI" ;

DefineGlobalColor("obcloud", CreateColor(217,61,255)) ;
DefineGlobalColor("oscloud", CreateColor(255,216,61)) ;
AddCloud(0, os, GlobalColor("oscloud"), GlobalColor("oscloud"));
AddCloud(100, ob, GlobalColor("obcloud"), GlobalColor("obcloud"));
UbWdRMC.png

To Change The Cloud Colors:
  1. Click on the gear next to the study
  2. Click on global colors at the bottom of the box
  3. Click on the color, you want to change.
EywvbrG.png
@MerryDay. Thank you for this. I used the original RSI script from TOS and played around with "addcloud" code. I was interested in adding cloud to area overbought => 70 or oversold =< 30 only. Will do more research.
 

MerryDay

Well-known member
VIP
@btran99 You are not being very clear at all. So here is my 2nd attempt at guessing what you want.
These clouds are created in the areas defined by the RSI and the OB/OS.
This is my last attempt. No one will be able to help you further unless you can give a better explanation. It would be of great assistance if you color in where you want the clouds on a chart and post that image.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/

My guess:
XQEj21V.png

Ruby:
declare lower ;
input ob = 70 ;
input os = 30;
plot RSI = RSI("length" = 14, "over bought" = ob, "over sold" = os)."RSI" ;
RSI.AssignValueColor(
if RSI < os then color.dark_red else
if RSI > ob then color.dark_green else color.dark_orange);

plot overbought = ob ;
plot oversold = os ;

AddCloud(RSI, os, color.light_gray, color.dark_red );
AddCloud(RSI, ob, color.green, color.light_gray);
 
Last edited:

Similar threads

Top