Mobius Squeeze and Custom Labels For ThinkOrSwim

Mobius Squeeze and Custom Labels For ThinkOrSwim
A little gift for the group. This is my first indicator. I took Mobius' Squeeze and added a few things. There's about 30 line of code and 150 line of comments explaining what everything does. My background is data science as it pertains to advertising and a copywriter. I did some coding 2 centuries ago.

With the indicator, I can predict when a Squeeze will happen, as well as how likely a Squeeze is to happen. Bunch of other stuff, too. Provides about 6 items of direct metrics and another 6 of indirect metrics/indications. It is a pretty good tool for anticipating trends (if I say so myself) :)
Code:
#--------------------- [ Care And Feeding Of Your New Indicator ] ---------------------
#
# In the 'SQUEEZE INDEX' display box, you'll find a bunch of predictive analytics.
# The numbers, left to right...
#
# 1. 'Squeeze Index' number
# 2. The previous SI number
# 3. The Percentage Difference between the current and previous SI
# 4. The current number of Squeeze Bars. If no Squeeze, then default is zero
# 5. The number of bars since a Squeeze
# 6. The lowest Percent Difference seen during the Squeeze. An indication of how
#    deep the Squeeze was.

# Changed the sensitivity of the entrance and exit by just a couple of milliseconds. This was
# done to help smooth the Squeeze. The change is so small, the human eye cannot see a difference,
# but the smoothing prevents false entrances and premature exits. Once a Squeeze has been entered,
# it is less likely to quickly jump back out.

# Previously, the Squeeze indicator would light and then potentially immediately exit, only to jump back
# in again, or maybe not... kinda like how my mother used to drive... start, stop, mash the gas and slam the brakes.
# To her, a stop sign was merely a suggestion. That's LA driving :) How did I survive? Now, a Squeeze is
# 'more committed' (smooth and steady) before it is announced and 'minor' false signals are ignored.
#
# Also made the indicator change color... blue for when the stock is moving away (above SI 20) from a Squeeze,
# color.LIGHT_ORANGE when the Squeeze Index (SI) reaches <= 20. Blue was chosen while moving away
# from a Squeeze because for Squeeze Traders, moving away from a Squeeze is 'sad' :) The LIGHT_ORANGE
# alerts to a upcoming change as the SI reaches 20 and below.
#
# If a Squeeze reaches -20 or below (what I consider a Deep Squeeze) then the plot dot turns violet. Green for no # Squeeze, red for a Squeeze between zero (beginning) and -19, then violet for anything under -20. John Carter
# uses a black dot to signify something (for what, I do not know) so I chose violet for Deep Squeeze.

#-------------------------------------[ Disclaimer ] -------------------------------

# Because TOS is a scripting language (vs a complied language) AND because I am not a REAL programmer,
# there are a few things to consider. TOS has a ton of cool features (recursive) but it doesn't like
# to count numbers. Sometimes the counters (how many bars since a Squeeze) are a few bars off.
# Sometimes they are correct. Depends on the timing of the displaying of the bars. Sounds very British :)
#
# There area few known bugs in TOS that cause the simplest stuff to be difficult, but then again,
# TOS makes some really difficult stuff, easy. And, again... I am NOT a real programmer.
# Add those two ingredients and bad stuff can happen, quickly.
#
# As far as I can tell, the cool recursive features makes doing some of the easy stuff, difficult.
# If you know how to make the counters more accurate. then please... fix at will.
# The math (maths for all my British friends) is correct (double-check,  please).
# The counters give a general idea, the math is more accurate.

# Some of the code was written by me, and some written by others. I credited code when the
# author was known. Everyone deserves a pat on the back :) Or, ala Moody Blues, Every Good Boy Deserves A Favour.
# Gals, too :)
#
#
#-----------------------------------------------------------------------------------
#
#-------------------------------- [ What It All Means ] -----------------------------

# This is my first indicator mod... a Thank You gift to the group :) Forgive my crappy programming...
# fix if you wish.
#
# With this indicator, you can actually click the charts and predict when the next
# Squeeze will happen. Cool. How?

# The higher the SI (first number) the less likely (lower numbers means the event is near)
# a Squeeze event will happen. The Percent Difference (3rd number) tells you if the
# SI trend is up or down. An SI of 115 with a PD of -20% means the SI has been
# recently driving downwards at a rate of 20% (roughly) per bar. A consistent drop of 20% will
# mean the SI could reach zero in 5 bars, depending on Volume. Not likely, but you
# get the idea. If the SI is at 100 and the PD shows -.025% (a slow downward drop) then we
# know the Squeeze is a long way off. Time to look for another underlying. A positive number means the
# underlying is moving away from a Squeeze. A Percent Difference (or SI) rise or fall of 20+ points indicates
# a fast-moving stock, regardless of IV.
#
# More Examples:
#
# Nomenclature (In order, left to right):
# -------------------------------
# SI = Squeeze Index (Group 1 or G1)
# Past SI = PSI (G2)
# Percent Difference = PD (G3)
# Number Of Bars While In Squeeze = BIS (G4)
# Number Of Bars Since Squeeze = BSS (G5)
# Squeeze Depth (Lowest Percent Down While In Squeeze... variable 'historicalDifference') = SD (G6)
#
#
# 1.  SI (G1) = 500; G5 = 2 == huge swing UP coming out of Squeeze
# IE: Only 2 bars out of Squeeze (G5) and already 500 Index Units UP
#
# 2.  SI (G1) = 1000; G3= -5.00%; == huge swing DOWN towards Squeeze
# IE: A negative G3 indicates negative movement (towards a Squeeze). However, since G1 is a positive
#     1000 Units aways from a Squeeze(zero) then there is no immediate Squeeze. In fact, the trend
#     could reverse... and probably will. If G1 was <20, then a Squeeze is nearing.

# 3.  SI (G1) = 2000; G2 = 200; G5 = 100 == huge swing Up... moving away from Squeeze
# IE: Since G! is a big number and it is >G2, there is no Squeeze approaching. Time for lunch :)
#
# 4.  SI (G1) = -1; G4 = 1 == Just started Squeeze
# IE: When in a Squeeze and G1 is increasing and/or G3 is increasing, then the Squeeze is reversing. Get ready :)
#
# 5.  SI G(1) = 200; G2 = 100 == Moving fast towards (down) a Squeeze ( a 50% jump down)
#
# SI (G1) tells how far away (positive number) from a Squeeze
# G2 indicates direction. If G1 > G2 then UP. If G1 < G2 then DOWN
# G3 See G2 explanation
# G4 The Percent Difference between G1 and G2. A positive number indicates UP and negative numbers indicates DOWN
# G5 The number (always positive) of bars the Squeeze has occurred. Will be ZERO if not in a Squeeze
# G6 The lowest Percent Difference while in a Squeeze
#
# For example... an SI of 120 and a previous SI of 110 with a Percent Difference of 1.5(%...
# this indicate the underlying is moving very slowly away from a squeeze. An SI of 5 with a
# previous of 4 and a PD of -1% means the underlying is moving to a possible Squeeze...
# about 1-4 minutes to a Squeeze if the current conditions prevail...the bathroom can wait.
# An SI of 1,000 (group 1) and a previous of 1,500 (group 2) and a PD of -50% (group 3) means the stock
# is headed South towards a Squeeze, quickly.
#
# The relationship between the current and previous bar (Percent Difference) gives insight on direction,
# as well as overall market trend, at the moment. I successfully calculate all my clients' digital advertising
# based on 7 calculations. One of those predictive calculations is in this indicator.
#
# During testing, I have accurately predicted a Squeeze, with time to fetch coffee. I can
# even predict the intensity of action after the Squeeze, while still in the Squeeze. The direction
# of the Squeeze exit is shown visually via Bollinger Bands (not included).
#
# The number of bars during a Squeeze is the 4th group. In my testing, the more Squeeze bars
# does not indicate a better Squeeze. What more bars does measure is a lack of interest in the underlying.
# Low volume will drive a Squeeze to 10-20 bars in duration. Dreadfully painful. In my testing, a deeper Squeeze is
# more beneficial than a longer Squeeze, as it indicates a potentially more explosive exit.
# The SI will show you the depth of the Squeeze. I believe it's a valuable piece of previously missing information.

#
# Hope you find the SI informative and educational. I spent many years as a data scientist
# and copywriter in the advertising industry. I use data to drive all decisions for my
# FaceBook advertising clients, with impressive results. You can never have enough data,
# but feel free to mod this sucker any way you wish. One size does not fit all.
# Of course, don't use this thing, lose a bunch of money, and then blame me. Likewise, if make a ton of cash,
# I won't ask for a commission :) All indicators are for informational purposes only. Just because it works
# for my brain, doesn't mean it will work for you. I have a moderate brain injury from the military...
# I know I see things differently, but in some ways it has become an asset. Using different tools and
# seeing things differently than others is usually good. Some Data Scientists think my use of Percent Difference
# vs Percent Change or even just regular Percent is strange, but I see something in Percent Difference that they don't...
# a buffer. In my view, Percent Difference smooths calculations. A change from +10% to +20% would be seen as a 100% increase,
# but that's not correct. It is actually only a 66.66% Percent Difference. That's the buffer. In my world,
# Different or Unusual or Avantgarde > Normal. Do (and use) what works for your brain.
#
#
# Thank You
# David M. (Some Random Alien From Outer Space)
#
# PS: Generally speaking, scripting languages do not ignore comments. I don't know if TOS
# is negatively affected by comments... I am sure if you delete all the long comments, the code
# will run faster. Not sure... just a guess.

#----------------------------------------------------------------------------------------

# Momentum Squeeze
# Mobius
# Added Squeeze Label with directional color
# Label is green when momentum is ascending, red when descending

#----------------------------------------------------------------------------------

#----------------------------------------------------------------------------------

# David changed the default Input to 21. After years of data scientist-ery, I have
# become fond of odd numbers. They do all sorts of cool stuff. Odd numbers smooth
# calculations and such. It seems the Universe can spilt the difference with even numbers
# (the dreaded 50/50 split), but must make a choice when presented with odd numbers. I chose '21'
# as a default because a lot of traders follow the 21 EMA or 21 SMA. A nod to those who travelled before me :)
# Now.... on with the show
#
#----------------------------------------------------------------------------------


# --------------------------- [ Mobius code below ] -------------------------------

declare lower; # Remark this out to allow placement on upper chart... or 'declare upper'
input length = 21; #hint length: Length for average calculation.
input price = close;
input SDmult = 2.0;
input ATRmult = 1.5;
def K = (Highest(high, length) + Lowest(low, length)) /
2 + ExpAverage(close, length);
plot Momo = Inertia(price - 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.BLUE
else if Momo < 0 and Momo < Momo[1]
then Color.RED
else Color.YELLOW);

#--------------- [ Metrics From Mobius ] ---------------

def SD = StDev(close, length);
def Avg = Average(close, length);
def ATR = Average(TrueRange(high, close, low), length);
def SDup = Avg + (SDmult * SD);
def ATRup = Avg + (ATRmult * ATR);
plot Squeeze = if SDup < ATRUp then 0 else Double.NaN;


#------------------------------------------------------------------------------------------
#  David M.(Some Random Alien) 10-6-2021
#  Don't blame Mobius... I made this mess :)
#  Code displays Squeeze Watch Prediction Index.
#  Depending on volume, a score of less then 25 could signal an impeding Squeeze.

def squeezePrediction = ((SDup - ATRup) / (SDup + ATRup) / 2) * 100;
def squeezeIndex = Round(squeezePrediction * 10000, 0);
def squeezeIndexDifference = (squeezeIndex - squeezeIndex[1]) / (squeezeIndex + squeezeIndex[1]) / 2 * 100;
def squeezeProgress = Round((squeezePrediction - squeezePrediction[1]) * 10000, 2);
def squeezeIndexOn = squeezeIndex <= 0;
rec counter = if squeezeIndexOn then counter[1] + 1 else 0;
def deepSqueeze = squeezeIndex <= -20;

rec accumCounter = if squeezeIndex >= 0 then accumCounter[1] + 1 else 0;
rec historicalDifference = if squeezeIndexOn then Lowest(squeezeIndexDifference) else historicalDifference[1];


#---------------------------- [ Plots Some Dots ] -----------------------------

Squeeze.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze.SetLineWeight(3);
Squeeze.AssignValueColor( if deepSqueeze then CreateColor(185, 30, 249)else color.RED);
Squeeze.SetDefaultColor(Color.RED);

plot zero = if IsNaN(close) or !IsNaN(Squeeze) then Double.NaN else 0;

zero.SetPaintingStrategy(PaintingStrategy.POINTS);
zero.SetLineWeight(3);
zero.SetDefaultColor(Color.GREEN);


#------------------------------------------------------------------------------
# Make the SI bar look pretty :)

AddLabel(yes, " SQUEEZE INDEX: " + squeezeIndex + " [" + squeezeIndex[1] + "] " + "[" + AsText(squeezeIndexDifference, NumberFormat.TWO_DECIMAL_PLACES) + "%] " + "[" + (counter[1]) + "] | " + "[" + accumCounter + "] " + "[" + AsText(historicalDifference[1], NumberFormat.TWO_DECIMAL_PLACES) + "%] ", if squeezeIndex > squeezeIndex[1] and squeezeIndex > 50 and squeezeIndexDifference > 0 then CreateColor(16, 198, 226) else color.LIGHT_ORANGE);

def fixBug = if squeezeIndex >= 0 then counter[1] == counter[1] == 0  else Double.NaN;

#--------------------------------------------------------------------------------------------------


#---------------------------------------------------------------------------------------------------
#
# I changed this Mobius code to show orange during initial squeeze
# and then turns to red if >5 squeeze bars. John Carter believes that a squeeze of 5 is better
# than a squeeze of 2 or 3. He is correct. However, during my initial testing, a squeeze of more
# than 5 is not better. During my testing, a squeeze substantially greater than 20 is worse than a 5.
# Your mileage may vary. Consult your owner's manual for more information. Hands and feet inside the ride
# at  all times :) And because I am a self-taught coder, I messed up Mobius' pretty formatting to reflect
# my ignorance of the craft :)
#
#----------------------------------------------------------------------------------------------------

AddLabel(!IsNaN(Squeeze), "  >>> Squeeze <<<  ", if IsAscending(Momo)
then Color.GREEN else if counter[1] >= 5 then Color.RED else CreateColor(16, 198, 226));

def cleanup = if IsNaN(Squeeze) then counter[1] == 0 else 0;

# End Code - Momentum Squeeze


Thanks to the group for answering all my questions and providing inspiration.
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
Alien's Analytics Simplified:
The purpose of these analytics are to display if you were moving toward or away from a squeeze and to predict if a squeeze was eminent.

The Math:
  1. Mobius finds the squeeze by looking at when a calculation using the Average True Range drops below a calculation using the standard deviation of the 21 moving average creating the squeeze.
  2. Alien's Squeeze Index makes a ratio of the ATR above the Std.Dev vs the ATR below the Std.Dev -This tells us whether we are moving toward or away from squeezing.

How Do We Read This? Blue is no chance of a squeeze :( ; Orange is in eminent chance of squeeze or in-squeeze :)
I6gWSws.png
 
Last edited:

MerryDay

Administrative
Staff member
Staff
VIP
Here is an example of a stock squeezing (with some upper labels to give you hints as to how to read the lower study labels).
NvRfYTx.png
 
Last edited:

OZ_Net

New member
VIP
Customized Mobius Squeeze For ThinkOrSwim
A little gift for the group. This is my first indicator. I took Mobius' Squeeze and added a few things. There's about 30 line of code and 150 line of comments explaining what everything does. My background is data science as it pertains to advertising and a copywriter. I did some coding 2 centuries ago.

With the indicator, I can predict when a Squeeze will happen, as well as how likely a Squeeze is to happen. Bunch of other stuff, too. Provides about 6 items of direct metrics and another 6 of indirect metrics/indications. It is a pretty good tool for anticipating trends (if I say so myself) :)
Code:
#--------------------- [ Care And Feeding Of Your New Indicator ] ---------------------
#
# In the 'SQUEEZE INDEX' display box, you'll find a bunch of predictive analytics.
# The numbers, left to right...
#
# 1. 'Squeeze Index' number
# 2. The previous SI number
# 3. The Percentage Difference between the current and previous SI
# 4. The current number of Squeeze Bars. If no Squeeze, then default is zero
# 5. The number of bars since a Squeeze
# 6. The lowest Percent Difference seen during the Squeeze. An indication of how
#    deep the Squeeze was.

# Changed the sensitivity of the entrance and exit by just a couple of milliseconds. This was
# done to help smooth the Squeeze. The change is so small, the human eye cannot see a difference,
# but the smoothing prevents false entrances and premature exits. Once a Squeeze has been entered,
# it is less likely to quickly jump back out.

# Previously, the Squeeze indicator would light and then potentially immediately exit, only to jump back
# in again, or maybe not... kinda like how my mother used to drive... start, stop, mash the gas and slam the brakes.
# To her, a stop sign was merely a suggestion. That's LA driving :) How did I survive? Now, a Squeeze is
# 'more committed' (smooth and steady) before it is announced and 'minor' false signals are ignored.
#
# Also made the indicator change color... blue for when the stock is moving away (above SI 20) from a Squeeze,
# color.LIGHT_ORANGE when the Squeeze Index (SI) reaches <= 20. Blue was chosen while moving away
# from a Squeeze because for Squeeze Traders, moving away from a Squeeze is 'sad' :) The LIGHT_ORANGE
# alerts to a upcoming change as the SI reaches 20 and below.
#
# If a Squeeze reaches -20 or below (what I consider a Deep Squeeze) then the plot dot turns violet. Green for no # Squeeze, red for a Squeeze between zero (beginning) and -19, then violet for anything under -20. John Carter
# uses a black dot to signify something (for what, I do not know) so I chose violet for Deep Squeeze.

#-------------------------------------[ Disclaimer ] -------------------------------

# Because TOS is a scripting language (vs a complied language) AND because I am not a REAL programmer,
# there are a few things to consider. TOS has a ton of cool features (recursive) but it doesn't like
# to count numbers. Sometimes the counters (how many bars since a Squeeze) are a few bars off.
# Sometimes they are correct. Depends on the timing of the displaying of the bars. Sounds very British :)
#
# There area few known bugs in TOS that cause the simplest stuff to be difficult, but then again,
# TOS makes some really difficult stuff, easy. And, again... I am NOT a real programmer.
# Add those two ingredients and bad stuff can happen, quickly.
#
# As far as I can tell, the cool recursive features makes doing some of the easy stuff, difficult.
# If you know how to make the counters more accurate. then please... fix at will.
# The math (maths for all my British friends) is correct (double-check,  please).
# The counters give a general idea, the math is more accurate.

# Some of the code was written by me, and some written by others. I credited code when the
# author was known. Everyone deserves a pat on the back :) Or, ala Moody Blues, Every Good Boy Deserves A Favour.
# Gals, too :)
#
#
#-----------------------------------------------------------------------------------
#
#-------------------------------- [ What It All Means ] -----------------------------

# This is my first indicator mod... a Thank You gift to the group :) Forgive my crappy programming...
# fix if you wish.
#
# With this indicator, you can actually click the charts and predict when the next
# Squeeze will happen. Cool. How?

# The higher the SI (first number) the less likely (lower numbers means the event is near)
# a Squeeze event will happen. The Percent Difference (3rd number) tells you if the
# SI trend is up or down. An SI of 115 with a PD of -20% means the SI has been
# recently driving downwards at a rate of 20% (roughly) per bar. A consistent drop of 20% will
# mean the SI could reach zero in 5 bars, depending on Volume. Not likely, but you
# get the idea. If the SI is at 100 and the PD shows -.025% (a slow downward drop) then we
# know the Squeeze is a long way off. Time to look for another underlying. A positive number means the
# underlying is moving away from a Squeeze. A Percent Difference (or SI) rise or fall of 20+ points indicates
# a fast-moving stock, regardless of IV.
#
# More Examples:
#
# Nomenclature (In order, left to right):
# -------------------------------
# SI = Squeeze Index (Group 1 or G1)
# Past SI = PSI (G2)
# Percent Difference = PD (G3)
# Number Of Bars While In Squeeze = BIS (G4)
# Number Of Bars Since Squeeze = BSS (G5)
# Squeeze Depth (Lowest Percent Down While In Squeeze... variable 'historicalDifference') = SD (G6)
#
#
# 1.  SI (G1) = 500; G5 = 2 == huge swing UP coming out of Squeeze
# IE: Only 2 bars out of Squeeze (G5) and already 500 Index Units UP
#
# 2.  SI (G1) = 1000; G3= -5.00%; == huge swing DOWN towards Squeeze
# IE: A negative G3 indicates negative movement (towards a Squeeze). However, since G1 is a positive
#     1000 Units aways from a Squeeze(zero) then there is no immediate Squeeze. In fact, the trend
#     could reverse... and probably will. If G1 was <20, then a Squeeze is nearing.

# 3.  SI (G1) = 2000; G2 = 200; G5 = 100 == huge swing Up... moving away from Squeeze
# IE: Since G! is a big number and it is >G2, there is no Squeeze approaching. Time for lunch :)
#
# 4.  SI (G1) = -1; G4 = 1 == Just started Squeeze
# IE: When in a Squeeze and G1 is increasing and/or G3 is increasing, then the Squeeze is reversing. Get ready :)
#
# 5.  SI G(1) = 200; G2 = 100 == Moving fast towards (down) a Squeeze ( a 50% jump down)
#
# SI (G1) tells how far away (positive number) from a Squeeze
# G2 indicates direction. If G1 > G2 then UP. If G1 < G2 then DOWN
# G3 See G2 explanation
# G4 The Percent Difference between G1 and G2. A positive number indicates UP and negative numbers indicates DOWN
# G5 The number (always positive) of bars the Squeeze has occurred. Will be ZERO if not in a Squeeze
# G6 The lowest Percent Difference while in a Squeeze
#
# For example... an SI of 120 and a previous SI of 110 with a Percent Difference of 1.5(%...
# this indicate the underlying is moving very slowly away from a squeeze. An SI of 5 with a
# previous of 4 and a PD of -1% means the underlying is moving to a possible Squeeze...
# about 1-4 minutes to a Squeeze if the current conditions prevail...the bathroom can wait.
# An SI of 1,000 (group 1) and a previous of 1,500 (group 2) and a PD of -50% (group 3) means the stock
# is headed South towards a Squeeze, quickly.
#
# The relationship between the current and previous bar (Percent Difference) gives insight on direction,
# as well as overall market trend, at the moment. I successfully calculate all my clients' digital advertising
# based on 7 calculations. One of those predictive calculations is in this indicator.
#
# During testing, I have accurately predicted a Squeeze, with time to fetch coffee. I can
# even predict the intensity of action after the Squeeze, while still in the Squeeze. The direction
# of the Squeeze exit is shown visually via Bollinger Bands (not included).
#
# The number of bars during a Squeeze is the 4th group. In my testing, the more Squeeze bars
# does not indicate a better Squeeze. What more bars does measure is a lack of interest in the underlying.
# Low volume will drive a Squeeze to 10-20 bars in duration. Dreadfully painful. In my testing, a deeper Squeeze is
# more beneficial than a longer Squeeze, as it indicates a potentially more explosive exit.
# The SI will show you the depth of the Squeeze. I believe it's a valuable piece of previously missing information.

#
# Hope you find the SI informative and educational. I spent many years as a data scientist
# and copywriter in the advertising industry. I use data to drive all decisions for my
# FaceBook advertising clients, with impressive results. You can never have enough data,
# but feel free to mod this sucker any way you wish. One size does not fit all.
# Of course, don't use this thing, lose a bunch of money, and then blame me. Likewise, if make a ton of cash,
# I won't ask for a commission :) All indicators are for informational purposes only. Just because it works
# for my brain, doesn't mean it will work for you. I have a moderate brain injury from the military...
# I know I see things differently, but in some ways it has become an asset. Using different tools and
# seeing things differently than others is usually good. Some Data Scientists think my use of Percent Difference
# vs Percent Change or even just regular Percent is strange, but I see something in Percent Difference that they don't...
# a buffer. In my view, Percent Difference smooths calculations. A change from +10% to +20% would be seen as a 100% increase,
# but that's not correct. It is actually only a 66.66% Percent Difference. That's the buffer. In my world,
# Different or Unusual or Avantgarde > Normal. Do (and use) what works for your brain.
#
#
# Thank You
# David M. (Some Random Alien From Outer Space)
#
# PS: Generally speaking, scripting languages do not ignore comments. I don't know if TOS
# is negatively affected by comments... I am sure if you delete all the long comments, the code
# will run faster. Not sure... just a guess.

#----------------------------------------------------------------------------------------

# Momentum Squeeze
# Mobius
# Added Squeeze Label with directional color
# Label is green when momentum is ascending, red when descending

#----------------------------------------------------------------------------------

#----------------------------------------------------------------------------------

# David changed the default Input to 21. After years of data scientist-ery, I have
# become fond of odd numbers. They do all sorts of cool stuff. Odd numbers smooth
# calculations and such. It seems the Universe can spilt the difference with even numbers
# (the dreaded 50/50 split), but must make a choice when presented with odd numbers. I chose '21'
# as a default because a lot of traders follow the 21 EMA or 21 SMA. A nod to those who travelled before me :)
# Now.... on with the show
#
#----------------------------------------------------------------------------------


# --------------------------- [ Mobius code below ] -------------------------------

declare lower; # Remark this out to allow placement on upper chart... or 'declare upper'
input length = 21; #hint length: Length for average calculation.
input price = close;
input SDmult = 2.0;
input ATRmult = 1.5;
def K = (Highest(high, length) + Lowest(low, length)) /
2 + ExpAverage(close, length);
plot Momo = Inertia(price - 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.BLUE
else if Momo < 0 and Momo < Momo[1]
then Color.RED
else Color.YELLOW);

#--------------- [ Metrics From Mobius ] ---------------

def SD = StDev(close, length);
def Avg = Average(close, length);
def ATR = Average(TrueRange(high, close, low), length);
def SDup = Avg + (SDmult * SD);
def ATRup = Avg + (ATRmult * ATR);
plot Squeeze = if SDup < ATRUp then 0 else Double.NaN;


#------------------------------------------------------------------------------------------
#  David M.(Some Random Alien) 10-6-2021
#  Don't blame Mobius... I made this mess :)
#  Code displays Squeeze Watch Prediction Index.
#  Depending on volume, a score of less then 25 could signal an impeding Squeeze.

def squeezePrediction = ((SDup - ATRup) / (SDup + ATRup) / 2) * 100;
def squeezeIndex = Round(squeezePrediction * 10000, 0);
def squeezeIndexDifference = (squeezeIndex - squeezeIndex[1]) / (squeezeIndex + squeezeIndex[1]) / 2 * 100;
def squeezeProgress = Round((squeezePrediction - squeezePrediction[1]) * 10000, 2);
def squeezeIndexOn = squeezeIndex <= 0;
rec counter = if squeezeIndexOn then counter[1] + 1 else 0;
def deepSqueeze = squeezeIndex <= -20;

rec accumCounter = if squeezeIndex >= 0 then accumCounter[1] + 1 else 0;
rec historicalDifference = if squeezeIndexOn then Lowest(squeezeIndexDifference) else historicalDifference[1];


#---------------------------- [ Plots Some Dots ] -----------------------------

Squeeze.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze.SetLineWeight(3);
Squeeze.AssignValueColor( if deepSqueeze then CreateColor(185, 30, 249)else color.RED);
Squeeze.SetDefaultColor(Color.RED);

plot zero = if IsNaN(close) or !IsNaN(Squeeze) then Double.NaN else 0;

zero.SetPaintingStrategy(PaintingStrategy.POINTS);
zero.SetLineWeight(3);
zero.SetDefaultColor(Color.GREEN);


#------------------------------------------------------------------------------
# Make the SI bar look pretty :)

AddLabel(yes, " SQUEEZE INDEX: " + squeezeIndex + " [" + squeezeIndex[1] + "] " + "[" + AsText(squeezeIndexDifference, NumberFormat.TWO_DECIMAL_PLACES) + "%] " + "[" + (counter[1]) + "] | " + "[" + accumCounter + "] " + "[" + AsText(historicalDifference[1], NumberFormat.TWO_DECIMAL_PLACES) + "%] ", if squeezeIndex > squeezeIndex[1] and squeezeIndex > 50 and squeezeIndexDifference > 0 then CreateColor(16, 198, 226) else color.LIGHT_ORANGE);

def fixBug = if squeezeIndex >= 0 then counter[1] == counter[1] == 0  else Double.NaN;

#--------------------------------------------------------------------------------------------------


#---------------------------------------------------------------------------------------------------
#
# I changed this Mobius code to show orange during initial squeeze
# and then turns to red if >5 squeeze bars. John Carter believes that a squeeze of 5 is better
# than a squeeze of 2 or 3. He is correct. However, during my initial testing, a squeeze of more
# than 5 is not better. During my testing, a squeeze substantially greater than 20 is worse than a 5.
# Your mileage may vary. Consult your owner's manual for more information. Hands and feet inside the ride
# at  all times :) And because I am a self-taught coder, I messed up Mobius' pretty formatting to reflect
# my ignorance of the craft :)
#
#----------------------------------------------------------------------------------------------------

AddLabel(!IsNaN(Squeeze), "  >>> Squeeze <<<  ", if IsAscending(Momo)
then Color.GREEN else if counter[1] >= 5 then Color.RED else CreateColor(16, 198, 226));

def cleanup = if IsNaN(Squeeze) then counter[1] == 0 else 0;

# End Code - Momentum Squeeze


Thanks to the group for answering all my questions and providing inspiration.
Amazing work sir. Would u know how to paint the candles the same color as the squeeze bars?
 

OZ_Net

New member
VIP
Amazing work sir. Would u know how to paint the candles the same color as the squeeze bars?
Figured it out incase anyone else wants it.

declare once_per_bar;
input length = 21; #hint length: Length for average calculation.
input price = close;
input SDmult = 2.0;
input ATRmult = 1.5;
def K = (Highest(high, length) + Lowest(low, length)) /
2 + ExpAverage(close, length);
plot Momo = Inertia(price - 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.BLUE
else if Momo < 0 and Momo < Momo[1]
then Color.RED
else Color.YELLOW);
AssignPriceColor(if Momo > Momo[1] and Momo > 0
then Color.CYAN
else if Momo > 0 and Momo < Momo[1]
then Color.BLUE
else if Momo < 0 and Momo < Momo[1]
then Color.RED
else Color.YELLOW);
 

MerryDay

Administrative
Staff member
Staff
VIP
From this analysis could you make a simple label that shows - avg number of bars per squeeze event (start to end)?
That information is not calculated in this study. Your request would require new code. Please post your request in a new thread.
Make sure to include how far back and how many squeeze events do you want to count? How do you want to average? Intraday? Or higher aggregations?

You should state which squeeze code you would like to use:
Mobius's: https://usethinkscript.com/threads/mobius’-momentum-squeeze-for-thinkorswim.1123/
John Carter's: https://usethinkscript.com/threads/john-carters-squeeze-pro-indicator-for-thinkorswim-free.4021
or
ToS: https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/T-U/TTM-Squeeze

AND MAKE SURE to include charts highlighting the squeeze events and how they should be documented in your label.
Unsure of how to upload screenshots to the forum, Here are directions.

Lastly, provide an explanation of how this metric will help our trading.
 
Last edited:
This is COST. I've been looking at charts all day and my brain is melting so I'm trying to understand all the #'s and what they mean

Could you go thru each one here and tell us what they mean for this sqz?

Thank you sir
rLcunJu.png
 

Titan

New member
VIP
This is COST. I've been looking at charts all day and my brain is melting so I'm trying to understand all the #'s and what they mean

Could you go thru each one here and tell us what they mean for this sqz?

Thank you sir
rLcunJu.png
I too, would appreciate a better understanding. Your first explanation is not easy to grasp. Very much appreciated.
Thanks
 

MerryDay

Administrative
Staff member
Staff
VIP
A screenshot of the code at work would be great :)
This is COST. I've been looking at charts all day and my brain is melting so I'm trying to understand all the #'s and what they mean
Could you go thru each one here and tell us what they mean for this sqz?
I too, would appreciate a better understanding. Your first explanation is not easy to grasp. Very much appreciated.
Thanks
@omwup @RedToGreen @Titan
See post#2
 

MerryDay

Administrative
Staff member
Staff
VIP
With the indicator, I can predict when a Squeeze will happen, as well as how likely a Squeeze is to happen.

Also made the indicator change color...
Blue was chosen while moving away from a Squeeze because for Squeeze Traders, moving away from a Squeeze is 'sad' :) T

With this indicator, you can actually click the charts and predict when the next Squeeze will happen. Cool.

Op made reference to being able to determine direction the squeeze will fire.
@RedToGreen,
I disagree. The OP stated that the purpose of these analytics are to display if you are moving toward or away from a squeeze and to predict if a squeeze is eminent. Nothing was added to this Mobius Squeeze study that could be used to "determine direction the squeeze will fire."
 
Last edited:

MerryDay

Administrative
Staff member
Staff
VIP
Red "<<< Squeeze >>>" is a bearish squeeze and the blue "<<< squeeze >>>" is bullish right?
@Drak,
A squeeze represents when a stock is consolidating. The consensus is: after the contraction the stock will expand but there is no direction indicated by squeeze studies as explained in the post above yours. There is no bearish or bullish signals.

The OP has created this study to alert whether a stock is headed for a contraction (squeeze) or if it is heading away.
The label colors are blue and orange:
Screenshot (234).png


The OP didn't change when the squeeze dots occur in Mobius's study but he did add a color:
green = no squeeze
red = squeezed
purple = deep squeeze

Here is an example of a stock squeezing (with some upper labels to give you hints as to how to read the lower study labels).
NvRfYTx.png
 

Similar threads

Top