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
 

Similar threads

Top