TTM_Squeeze_PriceLine Indicator for ThinkorSwim

rad14733

Options Expert
VIP
Lifetime
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:
@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:
@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.
 
@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...
Thank you updating code that helps to see if it fires up or down. One question, My squeeze is so high in chart that I have to squeeze chart to see the TTM_Squeeze. How I can bring that squeeze in middle of the chart? Thank you
 
Thank you updating code that helps to see if it fires up or down. One question, My squeeze is so high in chart that I have to squeeze chart to see the TTM_Squeeze. How I can bring that squeeze in middle of the chart? Thank you
The squeeze can print anywhere that has a quantitative definition. There is no definition for "middle of the chart".
Currently, the squeeze plot is defined as plotting at the HighestAll.

Before you ask, there is no definition for Middle-est All :)
 
Last edited:
I have updated the code in the first post to reflect the newest version I am running...
Can you please update your indicator to match the Squeeze Pro criteria. This current indicator works great, and I have changed the inputs to match the Squeeze Pro, but I haven’t been able to the colors to match. Thank you very much for your consideration.
 
Can you please update your indicator to match the Squeeze Pro criteria. This current indicator works great, and I have changed the inputs to match the Squeeze Pro, but I haven’t been able to the colors to match. Thank you very much for your consideration.

What you want is my TTM_Squeeze_Pro_PriceLine Study, listed below... You can edit the code to match your TTM_Squeeze_Pro colors as I haven't modified it to use Global Colors that can be set within the Edit Studies and Stragegies Panel...

Ruby:
# TTM_Squeeze_Pro_PriceLine
# Paints a horizontal price line with TTM_Squeeze_Pro indication
# Based on: A version of the Squeeze Pro
# Coded by rad14733
#
# v1.0 : 2021-01-22 : Initial Code Release
# v1.1 : 2021-02-11 : Added options for coloring chart candles based on either squeeze or momentum. Added Hint text.
#
#START of hints
#hint: \n</b>This study paints a dynamic horizontal line across the chart at the current price level. The lines color changes to indicate each candles TTM_Squeeze_Pro state.
#hint length: This option sets the number of candles both the Bollinger Bands and Keltner Channels use in calculating squeezes.
#hint price: This option allows for any available price type to be used in calculating squeezes with the default being close.
#hint showVerticalLines: Optionally paint vertical lines to indicate the start and end of squeezes.
#hint colorCandlesSqueeze: Optionally paint each candle based on its squeeze condition.
#hint colorCandlesMomentum: Optionally paint each candle based on its momentum condition.\n\n<b>Note:</b> This setting overrides squeeze-based candle colors.
#END of hints

declare upper;

input length = 20;
input price = close;
input showVerticalLines = no;
input colorCandlesSqueeze = no;
input colorCandlesMomentum = yes;
input agg = AggregationPeriod.THIRTY_MIN;

#Keltner Channels
def hb = 1.0;
def mb = 1.5;
def lb = 2.0;
def avg = Average(close(period = agg), length);
def k1 = avg + (hb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));
def k2 = avg + (mb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));
def k3 = avg + (lb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));

#Bollinger Bands
def BB_offset = 2.0;
def sDev = stdev(close(period = agg), length);
def mid = Average(close(period = agg), length);
def bb = mid + BB_offset * sDev;

#Squeeze States
def s0 = bb > k3;
def s1 = bb < k1;
def s2 = bb < k2;
def s3 = bb < k3;

# Paint PriceLine
#plot priceLine = HighestAll(if !IsNaN(price) and IsNaN(price[-1]) then price else Double.NaN);
input barsBack = 1000;
def tmpClose = if !IsNaN(close) and IsNaN(close[-1]) then close else tmpClose[1];
plot priceLine = if isNaN(close[-barsBack]) then tmpClose[-barsBack] else Double.NaN;
priceLine.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.BLACK);
priceLine.AssignValueColor(if s1 then color.orange
   else if s2 then color.red
   else if s3 then color.PLUM
   else color.green
);

# Color Chart Candles based on squeeze.
#NOTE: MAGENTA used instead of BLACK for Dark Theme visibility.
AssignPriceColor(
   if colorCandlesSqueeze then
      if s1 then Color.ORANGE
      else if s2 then Color.DARK_RED
      else if s3 then Color.PLUM
      else Color.GREEN
   else Color.CURRENT
);
#AssignPriceColor(if colorCandlesSqueeze then if s1 then Color.ORANGE
#   else if colorCandlesSqueeze and s2 then Color.RED
#   else if colorCandlesSqueeze and s3 then Color.MAGENTA
#   else if colorCandlesSqueeze then Color.GREEN
#   else Color.CURRENT

# Code taken from Momentum Squeeze by Mobius
# code is slightly modified to remove the squeeze portion
# Modified code to color chart candles based on momentum
def c = close;
def h = high;
def l = low;
def K = (Highest(h, length) + Lowest(l, length)) / 2 + ExpAverage(c, length);
def Momo = if isNaN(close) then double.nan else Inertia(c - K / 2, length);

AssignPriceColor(if colorCandlesMomentum and Momo > Momo[1] and Momo > 0 then Color.CYAN
   else if colorCandlesMomentum and Momo > 0 and Momo < Momo[1] then Color.DARK_ORANGE
   else if colorCandlesMomentum and Momo < 0 and Momo < Momo[1] then Color.DARK_RED
   else if colorCandlesMomentum and Momo < 0 and Momo > Momo[1] then Color.YELLOW
   else Color.CURRENT
);
# End Code - Mobius' Momentum Squeeze

# Add vertical lines to further demarcate start and end of Squeeze
AddVerticalLine(showVerticalLines and bb crosses below k3, "Squeeze On", Color.RED);
AddVerticalLine(showVerticalLines and bb crosses above k3, "Squeeze Off", Color.GREEN);

# END - TTM_Squeeze_Pro_PriceLine
 
What you want is my TTM_Squeeze_Pro_PriceLine Study, listed below... You can edit the code to match your TTM_Squeeze_Pro colors as I haven't modified it to use Global Colors that can be set within the Edit Studies and Stragegies Panel...

Ruby:
# TTM_Squeeze_Pro_PriceLine
# Paints a horizontal price line with TTM_Squeeze_Pro indication
# Based on: A version of the Squeeze Pro
# Coded by rad14733
#
# v1.0 : 2021-01-22 : Initial Code Release
# v1.1 : 2021-02-11 : Added options for coloring chart candles based on either squeeze or momentum. Added Hint text.
#
#START of hints
#hint: \n</b>This study paints a dynamic horizontal line across the chart at the current price level. The lines color changes to indicate each candles TTM_Squeeze_Pro state.
#hint length: This option sets the number of candles both the Bollinger Bands and Keltner Channels use in calculating squeezes.
#hint price: This option allows for any available price type to be used in calculating squeezes with the default being close.
#hint showVerticalLines: Optionally paint vertical lines to indicate the start and end of squeezes.
#hint colorCandlesSqueeze: Optionally paint each candle based on its squeeze condition.
#hint colorCandlesMomentum: Optionally paint each candle based on its momentum condition.\n\n<b>Note:</b> This setting overrides squeeze-based candle colors.
#END of hints

declare upper;

input length = 20;
input price = close;
input showVerticalLines = no;
input colorCandlesSqueeze = no;
input colorCandlesMomentum = yes;
input agg = AggregationPeriod.THIRTY_MIN;

#Keltner Channels
def hb = 1.0;
def mb = 1.5;
def lb = 2.0;
def avg = Average(close(period = agg), length);
def k1 = avg + (hb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));
def k2 = avg + (mb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));
def k3 = avg + (lb * Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), length));

#Bollinger Bands
def BB_offset = 2.0;
def sDev = stdev(close(period = agg), length);
def mid = Average(close(period = agg), length);
def bb = mid + BB_offset * sDev;

#Squeeze States
def s0 = bb > k3;
def s1 = bb < k1;
def s2 = bb < k2;
def s3 = bb < k3;

# Paint PriceLine
#plot priceLine = HighestAll(if !IsNaN(price) and IsNaN(price[-1]) then price else Double.NaN);
input barsBack = 1000;
def tmpClose = if !IsNaN(close) and IsNaN(close[-1]) then close else tmpClose[1];
plot priceLine = if isNaN(close[-barsBack]) then tmpClose[-barsBack] else Double.NaN;
priceLine.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.BLACK);
priceLine.AssignValueColor(if s1 then color.orange
   else if s2 then color.red
   else if s3 then color.PLUM
   else color.green
);

# Color Chart Candles based on squeeze.
#NOTE: MAGENTA used instead of BLACK for Dark Theme visibility.
AssignPriceColor(
   if colorCandlesSqueeze then
      if s1 then Color.ORANGE
      else if s2 then Color.DARK_RED
      else if s3 then Color.PLUM
      else Color.GREEN
   else Color.CURRENT
);
#AssignPriceColor(if colorCandlesSqueeze then if s1 then Color.ORANGE
#   else if colorCandlesSqueeze and s2 then Color.RED
#   else if colorCandlesSqueeze and s3 then Color.MAGENTA
#   else if colorCandlesSqueeze then Color.GREEN
#   else Color.CURRENT

# Code taken from Momentum Squeeze by Mobius
# code is slightly modified to remove the squeeze portion
# Modified code to color chart candles based on momentum
def c = close;
def h = high;
def l = low;
def K = (Highest(h, length) + Lowest(l, length)) / 2 + ExpAverage(c, length);
def Momo = if isNaN(close) then double.nan else Inertia(c - K / 2, length);

AssignPriceColor(if colorCandlesMomentum and Momo > Momo[1] and Momo > 0 then Color.CYAN
   else if colorCandlesMomentum and Momo > 0 and Momo < Momo[1] then Color.DARK_ORANGE
   else if colorCandlesMomentum and Momo < 0 and Momo < Momo[1] then Color.DARK_RED
   else if colorCandlesMomentum and Momo < 0 and Momo > Momo[1] then Color.YELLOW
   else Color.CURRENT
);
# End Code - Mobius' Momentum Squeeze

# Add vertical lines to further demarcate start and end of Squeeze
AddVerticalLine(showVerticalLines and bb crosses below k3, "Squeeze On", Color.RED);
AddVerticalLine(showVerticalLines and bb crosses above k3, "Squeeze Off", Color.GREEN);

# END - TTM_Squeeze_Pro_PriceLine
Awesome!!! This is exactly what I was looking for! Thank you very much!!! I really appreciate you sharing this!
 
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...

View attachment 9022

View attachment 9023

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...
Can this code be updated reflect the squeeze colors that you see on ttm squeeze pro and labels of histogram color. ?
 
Can this code be updated reflect the squeeze colors that you see on ttm squeeze pro and labels of histogram color. ?
Post #9 above contains the TTM_Squeeze_Pro_Priceline code but you will need to manually edit the colors to match whatever TTM_Squeeze_Pro indicator you are using at this time...
 
@Marvosb @rad14733 Which timeframe do you use TTM_Squeeze_Pro_Priceline? Do you keep chart aggregation 30 mins or change accordingly?
I change the code based on the timeframe I’m using. It really helps save space on my charts. I also find the vertical line feature helpful on 15 minute timeframe. It helps when looking at multiple charts
 
@Marvosb @rad14733 Which timeframe do you use TTM_Squeeze_Pro_Priceline? Do you keep chart aggregation 30 mins or change accordingly?

I use the same priceline on all of my charts using several timeframes... Because I primarily scalp my timeframes are mostly below 30m, primarily 2m, 5m, 10m, 15m, and usually one 30m...
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
315 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top