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 ****er 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.
Thx @SRA, appreciate it. Question: if Sqz Index and Last Sqz Index are negative, does the same hold true as in your notes and is the %diff still valid or do we need to change it from negative to positive (or vice versa). Thanks!
 
Thx @SRA, appreciate it. Question: if Sqz Index and Last Sqz Index are negative, does the same hold true as in your notes and is the %diff still valid or do we need to change it from negative to positive (or vice versa). Thanks!
and PS: it seems the sqz index changes dramatically after hours, wondering if an extra piece of code needs to be there. Thanks!
 
and PS: it seems the sqz index changes dramatically after hours, wondering if an extra piece of code needs to be there. Thanks!
The %diff still valid.
The default is the use of the indicator is with extended hours off.
As @Some Random Alien has not been back. So changes are not possible.
To determine if the indicator is useable after hours, put it on your chart. Nothing beats personal experience ;) The only way you will know what works best for you is to play with the settings and see how the indicator line up w/ your strategy and with your other indicators. To determine if this indicator brings value, analyze it over different timeframes, across history and with multiple instruments.
 
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 ****er 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.
Are you able to share both the scan and chart study via file ?
 
Hello again. I am in no way a coder, so pardon any lack of convention or conciseness. I seem to be descent at modifying other's codes to my liking.

I tried to make a custom quote / watchlist colomn based on this, that would tell me sqz vs deepsqz, sqz count, early sqz, sqz ft or sqz sell (two lower momentum bars). In a watchlist of about 40 stocks, it seems to work for a WEEKLY aggregation on all of them except AAPL and EBAY. Columns for daily, monthly and quarterly all seem to work. I have not been able to figure out why and have tried numerous itererations. Note I made some custom changes, for example colors I prefer. Code and screenshots below. Anyone have any ideas? Thanks so much! Evil.


#David M.(Some Random Alien) 10-6-2021
#Based on Mobius Momentum Squeeze
#EvilSurgeon Mod for Watchlist Custom Quote 12-7-2021 V.Beta.1

# --------------------------- [ 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);
def Momo = Inertia(price - K / 2, length);


#--------------- [ 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);
def 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;
def 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 ] -----------------------------

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

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

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

#-------------------------------Squeeze Label----------------------------------

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

#---------------------------- [ Paint Some Arrows ] -----------------------------

def SqzAlertEarly = (Squeeze == 0 and Momo > 0 and Momo > Momo[1] and Momo[2] > Momo[1]);

def SqzAlertFT = (zero == 0 and Squeeze[1] == 0 and Momo > 0 and Momo > Momo[1]);

def SqzAlertSell = (SqzAlertFT is true within 14 bars and Momo > 0 and Momo[2] > Momo[3] and Momo[1] < Momo[2] and Momo < Momo[1]);

addLabel(yes, if deepsqueeze and squeeze == 0 then "DeepSqz: " + counter + if SqzAlertEarly then " ErlySqz" else " " + if SqzAlertFT then " SqzFT" else " " + if SqzAlertSell then " SqzSell" else " " else if squeeze == 0 and !deepsqueeze then "Sqz: " + counter else " " + if SqzAlertEarly then " ErlySqz" else " " + if SqzAlertFT then " SqzFT" else " " + if SqzAlertSell then " SqzSell" else " ", color.black);

assignBackgroundColor(if momo >= 0 then if momo > momo[1] then color.dark_green else color.yellow else if momo < 0 and momo < momo[1] then color.RED else color.dark_orange);

# End Code - Momentum Squeeze
 
Last edited:
Is there an ideal or optimum time frame, is this good on most time frames. Thanks.
 
Last edited by a moderator:
Is there an ideal or optimum time frame, is this good on most time frames. Thanks.
I have not done extensive testing, but over a few days looked at 20 or so stocks. Seems to always work on daily, and what is weird to me, works on most for other time frames, but seems like there are always one or two in the higher time frames that are a bar off. If they were all off, that would make more sense to me, but why many are not and very few are off I can't figure out. Hoping someone can chime in with thoughts on the cause. Thx for asking.
 
Hello again. I am in no way a coder, so pardon any lack of convention or conciseness. I seem to be descent at modifying other's codes to my liking.

I tried to make a custom quote / watchlist colomn based on this, that would tell me sqz vs deepsqz, sqz count, early sqz, sqz ft or sqz sell (two lower momentum bars). In a watchlist of about 40 stocks, it seems to work for a WEEKLY aggregation on all of them except AAPL and EBAY. Columns for daily, monthly and quarterly all seem to work. I have not been able to figure out why and have tried numerous itererations. Note I made some custom changes, for example colors I prefer. Code and screenshots below. Anyone have any ideas? Thanks so much! Evil.


#David M.(Some Random Alien) 10-6-2021
#Based on Mobius Momentum Squeeze
#EvilSurgeon Mod for Watchlist Custom Quote 12-7-2021 V.Beta.1

# --------------------------- [ 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);
def Momo = Inertia(price - K / 2, length);


#--------------- [ 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);
def 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;
def 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 ] -----------------------------

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

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

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

#-------------------------------Squeeze Label----------------------------------

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

#---------------------------- [ Paint Some Arrows ] -----------------------------

def SqzAlertEarly = (Squeeze == 0 and Momo > 0 and Momo > Momo[1] and Momo[2] > Momo[1]);

def SqzAlertFT = (zero == 0 and Squeeze[1] == 0 and Momo > 0 and Momo > Momo[1]);

def SqzAlertSell = (SqzAlertFT is true within 14 bars and Momo > 0 and Momo[2] > Momo[3] and Momo[1] < Momo[2] and Momo < Momo[1]);

addLabel(yes, if deepsqueeze and squeeze == 0 then "DeepSqz: " + counter + if SqzAlertEarly then " ErlySqz" else " " + if SqzAlertFT then " SqzFT" else " " + if SqzAlertSell then " SqzSell" else " " else if squeeze == 0 and !deepsqueeze then "Sqz: " + counter else " " + if SqzAlertEarly then " ErlySqz" else " " + if SqzAlertFT then " SqzFT" else " " + if SqzAlertSell then " SqzSell" else " ", color.black);

assignBackgroundColor(if momo >= 0 then if momo > momo[1] then color.dark_green else color.yellow else if momo < 0 and momo < momo[1] then color.RED else color.dark_orange);

# End Code - Momentum Squeeze
Wow this watchlist column looks amazing.
Are you able to share the file associated with each column ? I know it’s a big ask ☺️ But i am unable to copy and paste the code how everyone else does on here and only able to enter the shared file.
 
Wow this watchlist column looks amazing.
Are you able to share the file associated with each column ? I know it’s a big ask ☺️ But i am unable to copy and paste the code how everyone else does on here and only able to enter the shared file.
Sure, here you go......http://tos.mx/xDcnfFq
 
Let me know which other columns, if any, I just noted you asked for more than one. For the sqz mod, its same code, just different aggregations (set at top), just add it 3 time and change that button. Not bugs noted above, I fixed one.
Truth be told I love your set up and if you were able to put the columns in a file instead of one by one that would be much appreciated, and your price action study, you really have a great set up it looks like and would love to test it and use it.
Where my labtop is insanely slow I can’t copy code and upload it, lol I can’t even get on the internet and get on here without it taking 10 years. It’s what I have now, but hopefully I can upgrade soon.
 
Last edited:
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);
when I used this my chart was shrink do you know what is the reason.
 
Truth be told I love your set up and if you were able to put the columns in a file instead of one by one that would be much appreciated, and your price action study, you really have a great set up it looks like and would love to test it and use it.
Where my labtop is insanely slow I can’t copy code and upload it, lol I can’t even get on the internet and get on here without it taking 10 years. It’s what I have now, but hopefully I can upgrade soon.
Well thanks. Very flattering. I have not shared anything before. Though largely this is the work of others with some personal customizations in visualization, not in calculations. Happy to share.
Grid with charts: http://tos.mx/Oo3SX36
Watchlilst: http://tos.mx/WQyXtvo
 
Last edited:
Well thanks. Very flattering. I have not shared anything before. Though largely this is the work of others with some personal customizations in visualization, not in calculations. Happy to share.
Grid with charts: http://tos.mx/Oo3SX36
Watchlilst: http://tos.mx/WQyXtvo
In this example, the Monthly Sqz watchlist does not match the chart, but everything else does, still can not figure out a way around this bug, which seems to show up sporadically.
 
Last edited:
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
Hello, I copied the code but my TOS doesn't show the top labels "SQUEEZE INDEX Current: -44 vs previous bar: -38" and none of the other information. How can I add this?
 
Hello, I copied the code but my TOS doesn't show the top labels "SQUEEZE INDEX Current: -44 vs previous bar: -38" and none of the other information. How can I add this?
That was just provided to illustrate the meaning of the actual label. That is too wordy to use in real life. I don't think I even kept that.
 
@evilsurgeon , I’ve had your chart set up trying to watch and take mental notes. Question is, what time frame do you use to trade on? Me, I will look at daily to see if trend or squeeze is eminent and go to the lower time frames to see if there is an opening of what kind of trade I’m wanting to do for that particular day. I use the 1 min for when I would actually buy and sell, but only decide when to sell on the 5 min.
I’m afraid I’m making it to complicated
 

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
472 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