# TTM_Squeeze_PriceLine
# Paints a horizontal price line with TTM_Squeeze indication
# Coded by rad14733 for usethinkscript.com
# v1.0 : 2021-01-10 : Original Code
# v1.1 : 2021-01-14 : Made user configurable by adding inputs
# v1.2 : 2021-01-19 : Modified code so when BB & KC bands are equal it is considered a squeeze
# Use reference calls to TOS BB's and plot midline and bands
input bbprice = close;
input displace = 0;
input length = 20;
input numdevdn = -2.0;
input numdevup = 2.0;
input averagetype = AverageType.SIMPLE;
input showVerticalLines = yes;
def bb_midline = BollingerBands(bbprice, displace, length, numdevdn, numdevup, averagetype).MidLine;
def bb_upperband = BollingerBands(bbprice, displace, length, numdevdn, numdevup, averagetype).UpperBand;
def bb_lowerband = BollingerBands(bbprice, displace, length, numdevdn, numdevup, averagetype).LowerBand;
# Use reference calls to TOS KC's and plot midline and bands
input kcdisplace = 0;
input kcfactor = 1.5;
input kclength = 20;
input kcprice = close;
input kcaverageType = AverageType.SIMPLE;
input kctrueRangeAverageType = AverageType.SIMPLE;
def kc_midline = KeltnerChannels(kcdisplace, kcfactor, kclength, kcprice, kcaveragetype, kctruerangeaveragetype).Avg;
def kc_upperband = KeltnerChannels(kcdisplace, kcfactor, kclength, kcprice, kcaveragetype, kctruerangeaveragetype).Upper_Band;
def kc_lowerband = KeltnerChannels(kcdisplace, kcfactor, kclength, kcprice, kcaveragetype, kctruerangeaveragetype).Lower_Band;
# When the BB upper and lower bands cross inside the KC's we are in a Squeeze
def squeezed = if bb_upperband <= kc_upperband and bb_lowerband >= kc_lowerband then 1 else if bb_upperband >= kc_upperband and bb_lowerband <= kc_lowerband then -1 else 0;
#def squeezed = if bb_upperband crosses below kc_upperband and bb_lowerband crosses above kc_lowerband then 1 else if bb_upperband crosses above kc_upperband and bb_lowerband crosses below kc_lowerband then -1 else 0;
AddVerticalLine(showVerticalLines and bb_upperband crosses below kc_upperband and bb_lowerband crosses above kc_lowerband, "Squeeze On", Color.RED);
AddVerticalLine(showVerticalLines and bb_upperband crosses above kc_upperband and bb_lowerband crosses below kc_lowerband, "Squeeze Off", Color.GREEN);
#Plot the current price across an entire chart
#Also helps determine when price is approaching support and resistance
#Color coded to indicate TTM_Squeeze zones
input price = close;
input barsBack = 300;
def c = if !IsNaN(close) and IsNaN(close[-1]) then close else c[1];
plot priceline = if isNaN(close[-barsBack]) then c[-barsBack] else Double.NaN;
#plot priceLine = HighestAll(if !IsNaN(price) and IsNaN(price[-1]) then price else Double.NaN);
priceLine.DefineColor("squeezed", Color.RED);
priceLine.DefineColor("unsqueezed", Color.GREEN);
#priceLine.AssignValueColor(if squeezed == 1 then priceLine.Color("squeezed") else if squeezed == -1 then priceLine.Color("unsqueezed") else if squeezed == 0 then priceLine.Color("squeezed") else Color.CURRENT);
priceLine.AssignValueColor(if squeezed == 1 then priceLine.Color("squeezed") else priceLine.Color("unsqueezed"));
priceLine.SetPaintingStrategy(PaintingStrategy.DASHES);
priceline.SetStyle(Curve.FIRM);
priceLine.SetLineWeight(2);
#plot sq = squeezed;
# END - TTM_Squeeze_PriceLine