# TTM_Squeeze_Pro_rad14733
# Based on: A version of the Squeeze Pro
# https://usethinkscript.com/threads/john-carters-squeeze-pro-indicator-for-thinkorswim-free.4021/post-60807
# Modified by rad14733
# v1.0 : 2021-01-24 : Added Momo Average and Vertical Squeeze Fire On/Off lines
# v1.1 : 2021-03-13 : Added avgMomo Acrylic line color and optional avgMomo cloud
declare lower;
input length = 20;
input avglength = 20;
input showVerticalLines = yes;
input showAvgMomoCloud = yes;
input momoAverageType = AverageType.SIMPLE;
#Keltner Channels
def hb = 1.0;
def mb = 1.5;
def lb = 2.0;
def avg = Average(close, length);
def k1 = avg + (hb * Average(TrueRange(high, close, low), length));
def k2 = avg + (mb * Average(TrueRange(high, close, low), length));
def k3 = avg + (lb * Average(TrueRange(high, close, low), length));
#Bollinger Bands
def BB_offset = 2.0;
def sDev = stdev(close, length);
def mid = Average(close, length);
def bb = mid + BB_offset * sDev;
#Squeeze
def s0 = bb > k3;
def s1 = bb < k1;
def s2 = bb < k2;
def s3 = bb < k3;
# Code taken from Momentum Squeeze by Mobius
# code is slightly modified to remove the squeeze portion
def c = close;
def h = high;
def l = low;
def K = (Highest(h, length) + Lowest(l, length)) / 2 + ExpAverage(c, length);
plot Momo = if isNaN(close) then double.nan else Inertia(c - K / 2, length);
Momo.setPaintingStrategy(PaintingStrategy.HISTOGRAM);
Momo.setLineWeight(3);
Momo.assignValueColor(
if Momo > Momo[1] and Momo > 0
then Color.Cyan
else if Momo > 0 and Momo < Momo[1]
then Color.DARK_ORANGE
else if Momo < 0 and Momo < Momo[1]
then Color.Red
else Color.Yellow
);
# End Code - Mobius' Momentum Squeeze
# Momo average line - rad14733 - v1.0
#plot avgMomo = Average(Momo, avglength);
plot avgMomo = MovingAverage(momoAverageType, Momo, avglength);
avgMomo.SetLineWeight(2);
# avgMomo Acrylic Line Color - rad14733 - v1.1
avgMomo.AssignValueColor(
if Momo > avgMomo and Momo > 0
then Color.WHITE
else if Momo > 0 and Momo < avgMomo
then Color.RED
else if Momo < 0 and Momo < avgMomo
then Color.WHITE
else Color.CYAN
);
# Squeeze dots on zeroline
plot squeeze = if s0 or s1 or s2 or s3 then 0 else double.nan;#
squeeze.SetLineWeight(3);
squeeze.SetStyle(curve.POINTS);
squeeze.AssignValueColor(
if s1 then color.orange
else if s2 then color.red
else if s3 then color.plum
else color.green
);
# Add vertical lines to further demarcate start and end of Squeeze - rad14733 - v1.0
def aggOk = if GetAggregationPeriod() < AggregationPeriod.DAY then 1 else Double.NaN;
AddVerticalLine(aggOk and showVerticalLines and bb crosses below k3, "Squeeze On", Color.RED);
AddVerticalLine(aggOk and showVerticalLines and bb crosses above k3, "Squeeze Off", Color.GREEN);
# AddCloud - rad14733 - v1.1
AddCloud(if showAvgMomoCloud then 0 else Double.Nan, if showAvgMomoCloud then avgMomo else Double.Nan, Color.WHITE, Color.WHITE);
# end code