# TS_MACD_BB
# By Eric Purdy, ThinkScripter LLC
# [URL]http://www.thinkscripter.com[/URL]
# [EMAIL]thinkscripter@gmail.com[/EMAIL]
# Last Update 07 Feb 2011
# modification to color scheme by Rick_K 12/26/20.
# Added Price Color option by Prerak V. 05/15/2023.
declare lower;
input price = close;
input BBlength = 10;
input BBNum_Dev = 1.0;
input MACDfastLength = 12;
input MACDslowLength = 26;
input MACDLength = 5;
[B]input paintbars = No;[/B]
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
plot MACD_Dots = MACD_Data;
plot MACD_Line = MACD_Data;
plot BB_Upper = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).UpperBand;
plot BB_Lower = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).Lowerband;
plot BB_Midline = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).MidLine;
BB_Upper.SetDefaultColor(Color.CYAN);
BB_Lower.SetDefaultColor(Color.CYAN);
#BB_Midline.SetDefaultColor(Color.GRAY);
#BB_Midline.SetStyle(Curve.SHORT_DASH);
#MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then color.green else color.red);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] and MACD_Line >= BB_Upper then Color.GREEN else if MACD_Line < MACD_Line[1] and MACD_Line >= BB_Upper then Color.DARK_GREEN else if MACD_Line < MACD_Line[1] and MACD_Line <= BB_Lower then Color.RED else if MACD_Line > MACD_Line[1] and MACD_Line <= BB_Lower then Color.DARK_RED else Color.GRAY);
MACD_Line.SetLineWeight(1);
#MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] then Color.green else Color.RED);
MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] and MACD_Line > BB_Upper then Color.GREEN else if MACD_Line < MACD_Line[1] and MACD_Line > BB_Upper then Color.DARK_GREEN else if MACD_Line < MACD_Line[1] and MACD_Line < BB_Lower then Color.RED else if MACD_Line > MACD_Line[1] and MACD_Line < BB_Lower then Color.DARK_RED else Color.WHITE);
MACD_Dots.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MACD_Dots.SetLineWeight(2);
plot zero = 0;
zero.SetDefaultColor(Color.WHITE);
zero.SetStyle(Curve.SHORT_DASH);
zero.SetLineWeight(1);
[B]AssignPriceColor(if paintbars and MACD_Line > MACD_Line[1] and MACD_Line > BB_Upper then Color.GREEN else if paintbars and MACD_Line < MACD_Line[1] and MACD_Line > BB_Upper then Color.DARK_GREEN else if paintbars and MACD_Line < MACD_Line[1] and MACD_Line < BB_Lower then Color.RED else if paintbars and MACD_Line > MACD_Line[1] and MACD_Line < BB_Lower then Color.DARK_RED else Color.GRAY);[/B]
## end original (modified) code
######Add Squeeze Cloud##########
###### Keltner Channels
input displace = 0;
input factor = 1.5;
input length = 20;
input averageType = AverageType.EXPONENTIAL;
input trueRangeAverageType = AverageType.EXPONENTIAL;
def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
def Avg = average[-displace];
def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];
######## Bollinger Bands
input BBLength2 = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input bb_averageType = AverageType.SIMPLE;
def sDev = StDev(data = price[-displace], length = BBLength2);
def MidLine = MovingAverage(bb_averageType, data = price[-displace], length = BBLength2);
def LowerBand = MidLine + Num_Dev_Dn * sDev;
def UpperBand = MidLine + Num_Dev_up * sDev;
### end of script
AddCloud(if UpperBand <= Upper_Band and LowerBand >= Lower_Band then BB_Upper else BB_Lower, BB_Lower, Color.yellow);
### END