#//@version=3
#study("MACD SQUEEZE [Bathula]")
# converted by Sam4Cok@Samer800 - Request from useThinkScript.com member
Declare lower;
input movAvgType = AverageType.EXPONENTIAL;
input BollingerSource = hlc3; # "Source"
input BollingerLength = 12; # "Bollinger length")
input BollingerMulti = 2.0; # "Bollinger deviation")
input keltnerSource = close; # "Source"
input keltnerLength = 25; # "keltner Length")
input KeltnerMulti = 1.25; # "Keltner deviation")
input macdSource = close; # "Source"
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 12;
def na = Double.NaN;
def last = isNaN(close);
#//Keltner
def nATR = ATR(Length = keltnerLength);
def kUpper = MovingAverage(movAvgType, keltnerSource, keltnerLength) + KeltnerMulti * nATR;
def kLower = MovingAverage(movAvgType, keltnerSource, keltnerLength) - KeltnerMulti * nATR;
#//--- Bollinger
def stdv = stdev(BollingerSource, BollingerLength);
def bbUpper = MovingAverage(movAvgType, BollingerSource, BollingerLength) + BollingerMulti * stdv;
def bbLower = MovingAverage(movAvgType, BollingerSource, BollingerLength) - BollingerMulti * stdv;
#//--- BB Squeeze
def squeeze = bbUpper <= kUpper and bbLower >=kLower;
#//macd
def fastMA = MovingAverage(movAvgType, macdSource, macdFastLength);
def slowMA = MovingAverage(movAvgType, macdSource, macdSlowLength);
def macd = fastMA - slowMA;
def signal = Average(macd, macdSignalLength);
def hist = macd - signal;
#//plots
plot macdLine = macd;
plot sigLine = signal;
plot zero = if last then na else 0; # "Squeeze indicator"
plot Histogram = hist; # "Histogram"
zero.SetPaintingStrategy(PaintingStrategy.SQUARES);
zero.AssignValueColor(if squeeze then Color.YELLOW else Color.DARK_GRAY);
Histogram.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Histogram.AssignValueColor(if hist>=0 then (if hist[1] < hist then Color.GREEN else Color.DARK_GREEN) else
(if hist[1] < hist then Color.DARK_RED else Color.RED));
macdLine.SetDefaultColor(Color.CYAN);
sigLine.SetDefaultColor(Color.MAGENTA);
#-- End of CODE