# 2021.03.01 V1.0 @Newrulez Initial Port Release
# Request @rbark3r
#BBTrend Indicator
#
#
#LINK
# https://www.tradingview.com/script/7nDSiWoZ-BBTrend/
#
#USAGE
#
declare lower;
input price = close;
input showSqueeze=yes;
input displace = 0;
input length = 20;
input slowlength = 50;
input signallength=9;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
def sDev = stdev(data = price[-displace], length = length);
def sDevslow = stdev(data = price[-displace], length = slowlength);
def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn*sDev ;
def UpperBand = MidLine + num_Dev_Up*sDev ;
def MidBB = MovingAverage(averageType, data = price[-displace], length = slowlength);
def LowerBB = MidBB + num_Dev_Dn*sDevslow;
def UpperBB = MidBB + num_Dev_Up *sDevslow;
def Lower = AbsValue(lowerBand - lowerBB);
def Upper = AbsValue(upperBand - upperBB);
plot BBTrend = (lower -upper)/midline ;
BBTrend.SetDefaultColor(GetColor(1));
BBTrend.SetDefaultColor(GetColor(5));
BBTrend.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBTrend.SetLineWeight(3);
BBTrend.DefineColor("Positive and Up", Color.GREEN);
BBTrend.DefineColor("Positive and Down", Color.DARK_GREEN);
BBTrend.DefineColor("Negative and Down", Color.RED);
BBTrend.DefineColor("Negative and Up", Color.DARK_RED);
BBTrend.AssignValueColor(if BBTrend >= 0 then if BBTrend > BBTrend[1] then BBTrend.color("Positive and Up") else BBTrend.color("Positive and Down") else if BBTrend < BBTrend[1] then BBTrend.color("Negative and Down") else BBTrend.color("Negative and Up"));
plot signal=MovingAverage(averageType, data = BBtrend, signallength);
#Add Squeeze
# show squeeze dots on zero line
def squeezeLevel =
if TTM_Squeeze(nK=1.0).SqueezeAlert == 0 then 3
else if TTM_Squeeze(nK=1.5).SqueezeAlert == 0 then 2
else if TTM_Squeeze(nK=2.0).SqueezeAlert == 0 then 1
else 0
;
plot Squeeze = if !showSqueeze then Double.NaN else 0;
Squeeze.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze.SetDefaultColor(Color.GRAY);
Squeeze.DefineColor("Loose Squeeze", Color.BLACK);
Squeeze.DefineColor("Squeeze", Color.RED);
Squeeze.DefineColor("Tight Squeeze", Color.YELLOW);
Squeeze.DefineColor("No Squeeze", Color.GRAY);
Squeeze.AssignValueColor(
if squeezeLevel == 3 then Squeeze.Color("Tight Squeeze")
else if squeezeLevel == 2 then Squeeze.Color("Squeeze")
else if squeezeLevel == 1 then Squeeze.Color("Loose Squeeze")
else Squeeze.Color("No Squeeze")
);
#End Squeeze