Bollinger Band Squeeze Scan

DisciplinedTrader

New member
Plus
Can someone please write a script for a condition where n variable offset's high crosses above Bollinger Band's upperband and all bars' highs and lows afterwards have stayed inside the range of n offset's high and low. To reiterate, I want a stock whose high has crossed above BB upperband in the past and the price never dropped below that particular bar's low and never crossed above high of the same bar that crossed above BB upperband.
 

Attachments

  • Screenshot 2024-09-23 093143.jpg
    Screenshot 2024-09-23 093143.jpg
    20.8 KB · Views: 175
  • Screenshot 2024-09-23 203843.jpg
    Screenshot 2024-09-23 203843.jpg
    27.5 KB · Views: 187
Last edited:
Solution
Can someone please write a script for a condition where n variable offset's high crosses above Bollinger Band's upperband and all bars' highs and lows afterwards have stayed inside the range of n offset's high and low. To reiterate, I want a stock whose high has crossed above BB upperband in the past and the price never dropped below that particular bar's low and never crossed above high of the same bar that crossed above BB upperband.

here is a chart study that can be changed to be a scan code

look for this line and read the comments
# ///////////////////////////////////////////


Code:
#inside_and_bbline_cross

#https://usethinkscript.com/threads/bollinger-band-squeeze-scan.19715/
#Bollinger Band Squeeze Scan

def bn =...
Can someone please write a script for a condition where n variable offset's high crosses above Bollinger Band's upperband and all bars' highs and lows afterwards have stayed inside the range of n offset's high and low. To reiterate, I want a stock whose high has crossed above BB upperband in the past and the price never dropped below that particular bar's low and never crossed above high of the same bar that crossed above BB upperband.

so , find an inside bar that crossed a bb line...
what is a desired minimum number for n? 5? 10? 15?

inside bar
https://usethinkscript.com/threads/congestion-zone.10503/#post-93191
 
Can someone please write a script for a condition where n variable offset's high crosses above Bollinger Band's upperband and all bars' highs and lows afterwards have stayed inside the range of n offset's high and low. To reiterate, I want a stock whose high has crossed above BB upperband in the past and the price never dropped below that particular bar's low and never crossed above high of the same bar that crossed above BB upperband.

here is a chart study that can be changed to be a scan code

look for this line and read the comments
# ///////////////////////////////////////////


Code:
#inside_and_bbline_cross

#https://usethinkscript.com/threads/bollinger-band-squeeze-scan.19715/
#Bollinger Band Squeeze Scan

def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;

def up = close > open;
def dwn = close < open;

DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")

#----------------------------
# BollingerBands
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;

def sDev = StDev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

def xup_bbupper = (high > UpperBand and low[1] < UpperBand[1]);

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

#inside_bars

input minimum_inside_bars = 2;
input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = yes;
input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = yes;

input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
    highx = h;
    lowx = l;
case "body":
    highx = Max(o, c);
    lowx = Min(o, c);
}


def loop1 = 100;
#  count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to loop1
  with p
  while (highx >= GetValue(highx, -k) and lowx <= GetValue(lowx, -k))
  do p + 1;

def incnt2 = if incnt1 >= minimum_inside_bars then incnt1 else 0;

# count down the bars in the inside range , cnt+1 to 1
def incountdown = if bn == 1 then 0
 else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
 else if incountdown[1] > 0 then (incountdown[1] - 1)
 else 0;


# did an inside bar cross above upper bb line 
def inside_xup_bbupper = (xup_bbupper and incnt2 > 0 and up);



# disable this with #  if using as a chart study
#plot scan = inside_xup_bbupper;

# ///////////////////////////////////////////

# delete stuff after this line when using this study as scan code


def yoff = 0.001;
def y6 = h * (1 + yoff);
AddChartBubble(show_inside_count_bubbles and inside_xup_bbupper and incountdown[1] <= 1 and incountdown > 0 , y6, incnt2 , GlobalColor("c1"), yes);


# plot horz lines during inside range
def in_hi = if incountdown[1] <= 1 and incountdown > 0 then highx
 else if incountdown > 0 then in_hi[1]
 else 0;
def in_lo = if incountdown[1] <= 1 and incountdown > 0 then lowx
 else if incountdown > 0 then in_lo[1]
 else 0;

plot zinhi = if inside_xup_bbupper and show_horizontal_lines and in_hi > 0 then in_hi else na;
plot zinlo = if inside_xup_bbupper and show_horizontal_lines and in_lo > 0 then in_lo else na;
zinhi.SetDefaultColor(GlobalColor("c1"));
zinhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinhi.HideBubble();
zinlo.SetDefaultColor(GlobalColor("c1"));
zinlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinlo.HideBubble();
#
 

Attachments

  • img1.JPG
    img1.JPG
    49.5 KB · Views: 221
Solution
Thank you for your awesome code.
How would you modify it when a bar crosses down BB lowerband instead and inside bars thereafter?

here is a chart study that can be changed to be a scan code

look for this line and read the comments
# ///////////////////////////////////////////


Code:
#inside_and_bbline_cross

#https://usethinkscript.com/threads/bollinger-band-squeeze-scan.19715/
#Bollinger Band Squeeze Scan

def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;

def up = close > open;
def dwn = close < open;

DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")

#----------------------------
# BollingerBands
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;

def sDev = StDev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

def xup_bbupper = (high > UpperBand and low[1] < UpperBand[1]);

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

#inside_bars

input minimum_inside_bars = 2;
input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = yes;
input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = yes;

input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
    highx = h;
    lowx = l;
case "body":
    highx = Max(o, c);
    lowx = Min(o, c);
}


def loop1 = 100;
#  count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to loop1
  with p
  while (highx >= GetValue(highx, -k) and lowx <= GetValue(lowx, -k))
  do p + 1;

def incnt2 = if incnt1 >= minimum_inside_bars then incnt1 else 0;

# count down the bars in the inside range , cnt+1 to 1
def incountdown = if bn == 1 then 0
 else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
 else if incountdown[1] > 0 then (incountdown[1] - 1)
 else 0;


# did an inside bar cross above upper bb line
def inside_xup_bbupper = (xup_bbupper and incnt2 > 0 and up);



# disable this with #  if using as a chart study
#plot scan = inside_xup_bbupper;

# ///////////////////////////////////////////

# delete stuff after this line when using this study as scan code


def yoff = 0.001;
def y6 = h * (1 + yoff);
AddChartBubble(show_inside_count_bubbles and inside_xup_bbupper and incountdown[1] <= 1 and incountdown > 0 , y6, incnt2 , GlobalColor("c1"), yes);


# plot horz lines during inside range
def in_hi = if incountdown[1] <= 1 and incountdown > 0 then highx
 else if incountdown > 0 then in_hi[1]
 else 0;
def in_lo = if incountdown[1] <= 1 and incountdown > 0 then lowx
 else if incountdown > 0 then in_lo[1]
 else 0;

plot zinhi = if inside_xup_bbupper and show_horizontal_lines and in_hi > 0 then in_hi else na;
plot zinlo = if inside_xup_bbupper and show_horizontal_lines and in_lo > 0 then in_lo else na;
zinhi.SetDefaultColor(GlobalColor("c1"));
zinhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinhi.HideBubble();
zinlo.SetDefaultColor(GlobalColor("c1"));
zinlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zinlo.HideBubble();
#
I appreciate all your work. You're the best.

Can you help me out with scan codes for two conditions:
1) n off bar's low hit year low and all bars afterwards are inside bars,
2) n off bar's high hit 20-bar high and all bars afterwards did not move above n off bar's high and did not move below 20-bar low before n off bar.


@halcyonguy
This modified code is not giving me anything, can you see what I missed in the script to satisfy the second condition I mentioned above?

Code:
def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;

def up = close > open;
def dwn = close < open;

DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")

#----------------------------
input length=20;
def periodhigh = high==highest(high,length);
def periodlow=lowest(low,length);
#----------------------------

#inside_bars

input minimum_inside_bars = 2;
input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = yes;
input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = yes;

input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = h;
lowx = l;
case "body":
highx = Max(o, c);
lowx = Min(o, c);
}


def loop1 = 100;
# count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to loop1
with p
while (highx >= GetValue(highx, -k) and periodlow <= GetValue(lowx, -k))
do p + 1;

def incnt2 = if incnt1 >= minimum_inside_bars then incnt1 else 0;

# count down the bars in the inside range , cnt+1 to 1
def incountdown = if bn == 1 then 0
else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
else if incountdown[1] > 0 then (incountdown[1] - 1)
else 0;


def inside_periodhighandperiodlow = (periodhigh and incnt2 > 0 and up);


plot scan = inside_periodhighandperiodlow;
 

Attachments

  • Screenshot 2024-09-25 143024.png
    Screenshot 2024-09-25 143024.png
    33.1 KB · Views: 163
Last edited by a moderator:
Thank you for your awesome code.
How would you modify it when a bar crosses down BB lowerband instead and inside bars thereafter?


I appreciate all your work. You're the best.

Can you help me out with scan codes for two conditions:
1) n off bar's low hit year low and all bars afterwards are inside bars,
2) n off bar's high hit 20-bar high and all bars afterwards did not move above n off bar's high and did not move below 20-bar low before n off bar.


@halcyonguy
This modified code is not giving me anything, can you see what I missed in the script to satisfy the second condition I mentioned above?

Code:
def bn = BarNumber();
def na = Double.NaN;
def o = open;
def h = high;
def l = low;
def c = close;

def up = close > open;
def dwn = close < open;

DefineGlobalColor("c1", Color.YELLOW);
# GlobalColor("c1")

#----------------------------
input length=20;
def periodhigh = high==highest(high,length);
def periodlow=lowest(low,length);
#----------------------------

#inside_bars

input minimum_inside_bars = 2;
input show_inside_count_bubbles = yes;
input show_inside_count_values_above = no;
input draw_a_box_around_inside_bar = yes;
input show_horizontal_lines = yes;
input show_inside_bars_within_larger_range = yes;

input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = h;
lowx = l;
case "body":
highx = Max(o, c);
lowx = Min(o, c);
}


def loop1 = 100;
# count how many future bars are smaller than the current bar
def incnt1 = fold k = 1 to loop1
with p
while (highx >= GetValue(highx, -k) and periodlow <= GetValue(lowx, -k))
do p + 1;

def incnt2 = if incnt1 >= minimum_inside_bars then incnt1 else 0;

# count down the bars in the inside range , cnt+1 to 1
def incountdown = if bn == 1 then 0
else if incountdown[1] <= 1 and incnt2 > 0 then incnt2 + 1
else if incountdown[1] > 0 then (incountdown[1] - 1)
else 0;


def inside_periodhighandperiodlow = (periodhigh and incnt2 > 0 and up);


plot scan = inside_periodhighandperiodlow;
What are you trying to get out of this script?
 

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