• LIMITED TIME OFFER: use coupon code PRIMEDAY (case sensitive) at checkout to save $50 off VIP membership (down to $149 / year). Valid through Tuesday 6/22. Biggest discount this year!

TTM_Squeeze_PriceLine Indicator for ThinkorSwim

rad14733

Well-known member
VIP
With chart real estate being at a premium there are times when combining indicators can be helpful... The code below is a combination of the PriceLine Chart Indicator I use combined with the TTM_Squeeze code I use in my TTM_Squeeze_Clouds Chart Study... Combined they show the current price as a moving line across the chart, colored to show when pricing indicates a squeeze caused by Bollinger Bands "squeezing" inside Keltner Channels, without the need to display the Bollinger Bands and Keltner Channels...

5cHS8Vj.jpg


iKWw05k.jpg


Ruby:
# 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 = 21;
input numdevdn = -2.0;
input numdevup = 2.0;
input averagetype = AverageType.SIMPLE;

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;


#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;

plot priceLine = HighestAll(if !IsNaN(price) and IsNaN(price[-1]) then price else Double.NaN);
priceLine.DefineColor("squeezed", Color.DARK_RED);
priceLine.DefineColor("unsqueezed", Color.WHITE);
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.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
priceLine.SetLineWeight(1);


# END - TTM_Squeeze_PriceLine

Please note that the Thinkscript code above has been corrected for proper PriceLine color change... The logic indicating current squeeze state was flawed...
 
Last edited:

MatthewA

Active member
@rad14733 This is awesome brother... One change you could make is that when a TTM squeeze is present the strength of the squeeze is subject to how far the Keltner Channel overlaps the Bollinger Band. There is a Bollinger Band below price and a Bollinger Band above price and the Keltner Channel will be at some angle of tilt...

You may wish to know if there is a stronger squeeze coming from above or below. Check this out...
White dot for compression squeeze, red dot for squeeze firing down, and green dot for squeeze firing down... No dots when squeeze is insignificant.

Code:
def NAN=double.NAN;
plot priceLine = highestAll(if isNaN(close[-1])
                            then close
                            else NaN);
priceLine.SetStyle(Curve.FIRM);
priceLine.AssignValueColor(CreateColor(100, 100, 200));
priceLine.SetLineWeight(1); PriceLine.HideTitle(); #priceline.HideBubble();
def BBhigh = bollingerBands().UpperBand;
def KChigh = keltnerChannels().Upper_Band;
def BBlow = bollingerBands().LowerBand;
def KClow = keltnerChannels().Lower_Band;
def FirstSqueezeBarHigh = if BBhigh crosses below KChigh
                          then KChigh-BBhigh
                          else 0;
def FirstSqueezeBarLow = if BBlow crosses above KClow
                         then BBlow-KClow
                         else 0;

plot squeeze = if BBhigh < KChigh or BBlow > KClow
               then priceLine
               else double.nan; squeeze.HideTitle(); squeeze.HideBubble();
squeeze.SetPaintingStrategy(PaintingStrategy.Points);
squeeze.AssignValueColor(if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                         then color.white
                         else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                              then color.green
                         else if close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                              then color.red
                              else Color.white);
squeeze.SetLineWeight(1);
squeeze.setHiding(squeeze==NaN);
addLabel(squeeze, (if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                  then  "squeeze compressing"
                 else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                       then "squeeze expanding up"
                       else if close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1])) then "squeeze expanding down" else "Squeeze"),
                 (if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                  then color.white
                  else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                       then color.green
                       else if  close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1])) then  color.red else color.light_gray));
plot SqueezeAlert= if squeeze > 0 then priceline else Double.NAN; SqueezeAlert.HideTitle(); SqueezeAlert.HideBubble();
SqueezeAlert.setHiding(squeeze==NAN);
SqueezeAlert.SetStyle(Curve.FIRM);
SqueezeAlert.SetdefaultColor(Color.White);
SqueezeAlert.SetLineWeight(2);

STUDY : http://tos.mx/Es8pJXm

30E6Zri.jpg


Edit: I left the Not-A-Number line off which cause errors...
 
Last edited:

Moose

Member
@rad14733 This is awesome brother... One change you could make is that when a TTM squeeze is present the strength of the squeeze is subject to how far the Keltner Channel overlaps the Bollinger Band. There is a Bollinger Band below price and a Bollinger Band above price and the Keltner Channel will be at some angle of tilt...

SO you would like to know if there is a stronger squeeze coming from above or below. Check this out...

Code:
plot priceLine = highestAll(if isNaN(close[-1])
                            then close
                            else NaN);
priceLine.SetStyle(Curve.FIRM);
priceLine.AssignValueColor(CreateColor(100, 100, 200));
priceLine.SetLineWeight(1); PriceLine.HideTitle(); #priceline.HideBubble();
def BBhigh = bollingerBands().UpperBand;
def KChigh = keltnerChannels().Upper_Band;
def BBlow = bollingerBands().LowerBand;
def KClow = keltnerChannels().Lower_Band;
def FirstSqueezeBarHigh = if BBhigh crosses below KChigh
                          then KChigh-BBhigh
                          else 0;
def FirstSqueezeBarLow = if BBlow crosses above KClow
                         then BBlow-KClow
                         else 0;

plot squeeze = if BBhigh < KChigh or BBlow > KClow
               then priceLine
               else double.nan; squeeze.HideTitle(); squeeze.HideBubble();
squeeze.SetPaintingStrategy(PaintingStrategy.Points);
squeeze.AssignValueColor(if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                         then color.white
                         else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                              then color.green
                         else if close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))  
                              then color.red
                              else Color.white);
squeeze.SetLineWeight(1);
squeeze.setHiding(squeeze==NaN);
addLabel(squeeze, (if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                  then  "squeeze compressing"
                 else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                       then "squeeze expanding up"
                       else if close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1])) then "squeeze expanding down" else "Squeeze"),
                 (if((FirstSqueezeBarLow>FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh>FirstSqueezeBarHigh[1]))
                  then color.white
                  else if close>close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1]))
                       then color.green
                       else if  close<close[1] && ((FirstSqueezeBarLow<FirstSqueezeBarLow[1]) && (FirstSqueezeBarHigh<FirstSqueezeBarHigh[1])) then  color.red else color.light_gray));
plot SqueezeAlert= if squeeze > 0 then priceline else Double.NAN; SqueezeAlert.HideTitle(); SqueezeAlert.HideBubble();
SqueezeAlert.setHiding(squeeze==NAN);
SqueezeAlert.SetStyle(Curve.FIRM);
SqueezeAlert.SetdefaultColor(Color.White);
SqueezeAlert.SetLineWeight(2);
The code is giving errors.
 

Similar threads

Top