TradingView BB Squeeze For ThinkOrSwim

mbarcala

Active member
I have this BB Squeeze that I would like to try in TOS. can some one help to convert this to thinkscript please?

if anyone know any link to any BB Squeeze that it can share with me I will appreciate?

Code:
study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)
 
keltPrd=20
keltFactor=1.5
cciPeriod=50
BandsDeviations=2.0
BandsPeriod=20

d=cci(close,cciPeriod)

diff = atr(keltPrd)*keltFactor
std = stdev(close,BandsPeriod)

bbs = BandsDeviations * std / diff
res = bbs < 1 and d>0
res2 = bbs < 1 and d<=0

plot( (res or res2) ? 1 : na, style = histogram, color = black, linewidth =2)
 
Does this work?

Code:
#study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)

declare Lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = lindev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then 1 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);

Or this?
Code:
declare lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = LinDev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot ZeroLine = 0;
plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then (close - Average(close, cciPeriod)) / d / 0.015 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);
 
Last edited:
Does this work?

Code:
#study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)

declare Lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = lindev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then 1 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);

Or this?
Code:
declare lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = LinDev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot ZeroLine = 0;
plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then (close - Average(close, cciPeriod)) / d / 0.015 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);
Thank you, both work perfect!
 
Does this work?

Code:
#study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)

declare Lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = lindev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then 1 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);

Or this?
Code:
declare lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = LinDev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot ZeroLine = 0;
plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then (close - Average(close, cciPeriod)) / d / 0.015 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);

Does this work?

Code:
#study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)

declare Lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = lindev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then 1 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);

Or this?
Code:
declare lower;

def keltPrd = 20;
def keltFactor = 1.5;
def cciPeriod = 50;
def BandsDeviations = 2.0;
def BandsPeriod = 20;

def d = LinDev(close, cciPeriod);
def diff = ATR(keltPrd) * keltFactor;
def std = StDev(close, BandsPeriod);

def bbs = BandsDeviations * std / diff;
def res = if bbs < 1 and d > 0 then yes else Double.NaN;
def res2 = if bbs < 1 and d <= 0 then yes else Double.NaN;

plot ZeroLine = 0;
plot BBSPlot = if !IsNaN(res) or !IsNaN(res2) then (close - Average(close, cciPeriod)) / d / 0.015 else Double.NaN;
BBSPlot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BBSPlot.SetDefaultColor(Color.BLACK);
BBSPlot.SetLineWeight(2);
can you help with a count label that let me know how many days in squeeze and when is not squeeze?
 
This should work for either version.
You can delete the AddChartBubble lines, I was just using them to check logic.

Code:
def BBSSqueeze = if IsNaN(BBSPlot) then 0 else if !IsNaN(BBSPlot) then BBSSqueeze[1]+1 else BBSSqueeze[1];
def BBSnoSqueeze = if !IsNaN(BBSPlot) then 0 else if IsNaN(BBSPlot) then BBSnoSqueeze[1]+1 else BBSnoSqueeze[1];

#Can remove 2 lines below
AddChartBubble(!IsNaN(BBSPlot), BBSPlot, BBSSqueeze,color.white,yes);
AddChartBubble(IsNaN(BBSPlot), 0, BBSnoSqueeze,color.white,no);
#

AddLabel(yes,"Number of Bars in Squeeze "+BBSSqueeze,color.white);
AddLabel(yes,"Number of Bars without Squeeze "+BBSnoSqueeze,color.white);

BBSP.jpg
 
Last edited:
This should work for either version.
You can delete the AddChartBubble lines, I was just using them to check logic.

Code:
def BBSSqueeze = if IsNaN(BBSPlot) then 0 else if !IsNaN(BBSPlot) then BBSSqueeze[1]+1 else BBSSqueeze[1];
def BBSnoSqueeze = if !IsNaN(BBSPlot) then 0 else if IsNaN(BBSPlot) then BBSnoSqueeze[1]+1 else BBSnoSqueeze[1];

#Can remove 2 lines below
AddChartBubble(!IsNaN(BBSPlot), BBSPlot, BBSSqueeze,color.white,yes);
AddChartBubble(IsNaN(BBSPlot), 0, BBSnoSqueeze,color.white,no);
#

AddLabel(yes,"Number of Bars in Squeeze "+BBSSqueeze,color.white);
AddLabel(yes,"Number of Bars without Squeeze "+BBSnoSqueeze,color.white);

BBSP.jpg
I create second squeeze compression but for some reason on the label do not display me well the number on about the second compression, can you show me what is wrong counting the second compression?

Code:
########## BB Squeeze ###########
declare lower;

def lDev = 1.0;
def mDev = 1.5;
def nDev = 2.0;

def ldn = LinDev(close, 50);
def dflv = ATR(20) * mDev;
def dflvl = ATR(20) * lDev;
def std = StDev(close, 20);

def mSq = nDev * std / dflv;
def lSq = mDev * std / dflv;

def res = if mSq < 1 and ldn > 0 or mSq < 1 and ldn <= 0 then 1 else Double.NaN;
def resl = if lSq < 1 and ldn > 0 or lSq < 1 and ldn <= 0 then 1 else Double.NaN;

plot ZeroLine = 0;
zeroLine.SetDefaultColor(Color.GRAY);
zeroLine.SetLineWeight(2);

plot bbSq = if !IsNaN(res) then ZeroLine else Double.NaN;
bbSq.SetPaintingStrategy(PaintingStrategy.POINTS);
bbSq.SetDefaultColor(Color.DARK_ORANGE);
bbSq.SetLineWeight(4);

plot bbSql = if !IsNaN(resl) then ZeroLine else Double.NaN;
bbSql.SetPaintingStrategy(PaintingStrategy.POINTS);
bbSql.SetDefaultColor(Color.BLACK);
bbSql.SetLineWeight(4);

def sqCount = if IsNaN(bbSq) and IsNAN(bbSql) then 0 else if !IsNaN(bbSq) and !IsNaN(bbSql) then sqCount[1] + 1 else sqCount[1];

AddLabel(yes, " Current Squeeze: " + sqCount + " ", Color.YELLOW);
 
Your code works, but only if you are currently in the squeeze you defined.
bbs.jpg


If you wish to see the last squeeze add these two lines of code after the definition of sqCount.
Code:
def sqCount = if IsNaN(bbSq) and IsNAN(bbSql) then 0 else if !IsNaN(bbSq) and !IsNaN(bbSql) then sqCount[1] + 1 else sqCount[1];
#Added Code
def lastsqCount = if sqCount!=0 then sqCount else lastsqCount[1];
AddLabel(yes, " Last Squeeze: " + lastsqCount + " ", Color.YELLOW);
#
AddLabel(yes, " Current Squeeze: " + sqCount + " ", Color.YELLOW);

bbs2.jpg
 
Your code works, but only if you are currently in the squeeze you defined.
bbs.jpg


If you wish to see the last squeeze add these two lines of code after the definition of sqCount.
Code:
def sqCount = if IsNaN(bbSq) and IsNAN(bbSql) then 0 else if !IsNaN(bbSq) and !IsNaN(bbSql) then sqCount[1] + 1 else sqCount[1];
#Added Code
def lastsqCount = if sqCount!=0 then sqCount else lastsqCount[1];
AddLabel(yes, " Last Squeeze: " + lastsqCount + " ", Color.YELLOW);
#
AddLabel(yes, " Current Squeeze: " + sqCount + " ", Color.YELLOW);

bbs2.jpg
what I trying to display in my label is that if orange squeeze happen count only orange no black and then if count black just count black and if not on squeeze then 0
 
If you want to count them separately then you have to define them separately.
Code:
def sqCount1 = if IsNaN(bbSq) then 0 else if !IsNaN(bbSq) then sqCount1[1] + 1 else sqCount1[1];
def sqCount2 = if IsNAN(bbSql) then 0 else if !IsNaN(bbSql) then sqCount2[1] + 1 else sqCount2[1];
AddLabel(yes, " Current Orange Squeeze: "+sqCount1+"| Current Black Squeeze: "+sqCount2, Color.YELLOW);
 
I have this BB Squeeze that I would like to try in TOS. can some one help to convert this to thinkscript please?

if anyone know any link to any BB Squeeze that it can share with me I will appreciate?

Code:
study(title="Bollinger Bands Squeeze", shorttitle="BB Squeeze", overlay=false)
 
keltPrd=20
keltFactor=1.5
cciPeriod=50
BandsDeviations=2.0
BandsPeriod=20

d=cci(close,cciPeriod)

diff = atr(keltPrd)*keltFactor
std = stdev(close,BandsPeriod)

bbs = BandsDeviations * std / diff
res = bbs < 1 and d>0
res2 = bbs < 1 and d<=0

plot( (res or res2) ? 1 : na, style = histogram, color = black, linewidth =2)
Just use the TTM_Squeeze. It plots the squeeze as red dots and also plots momentum as the histogram.
 

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