Find outside bars and inside bars, with various quantities of smaller bars

halcyonguy

Moderator - Expert
VIP
Lifetime
finding outside bars and inside bars, with various quantities of smaller bars

here are 2 studies for finding, outside bars and inside bars

draws horizontal lines during the range of bars
can pick a minimum quantity of smaller bars
will find smaller x bars, within larger x bar ranges
can pick wicks or body

uses a loop to find the smaller bars, (up to 100)


outside bar
. a larger bar , with smaller bars before it
https://www.newtraderu.com/2021/10/29/outside-bar-candlestick-pattern/

inside bar
. a larger bar , followed by smaller bars
https://www.newtraderu.com/2021/12/01/inside-candle-pattern/

-------------------------

i was reading another post and part of the rules included finding inside bars and outside bars...
and i got off on a tangent and made these

-------------------------

outside bars

Code:
# outside_bars_00_wickbody_engulf

#----------------
# halcyonguy
# 23-04-03
# find outside bars, can pick a minimum quantity
# find smaller outside bars, within larger outside bar ranges
# can pick wicks or body
#----------------

# outside bar , engulfing
#  previous bars are smaller than the current bar

def na = double.nan;
def bn = barnumber();
def o = open;
def h = high;
def l = low;
def c = close;
#def barup = (c > o);

DefineGlobalColor("cout", color.cyan);
#GlobalColor("cout")

input minimum_engulfed_bars = 1;
input show_engulfed_count_bubbles = yes;
input show_engulfed_count_values_below = yes;
input draw_a_box_around_outside_bar = yes;
input show_horizontal_lines = yes;
input show_outside_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);
}

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

# enable engulfing bars with enough smaller bars
def outcnt2 = if outcnt1 >= minimum_engulfed_bars then outcnt1 else 0;
def outbar = outcnt2 > 0 ;

# skip over smaller engulfed ranges
# find first bar of engulfed range
#  look ahead for biggest future outside bar, 
#   when the future engulfed bar qty is the same as the offset to it, then keep offset , j
def o_off = fold j = 1 to loop1
  with q
  do if getvalue(outcnt2, -j) == j then j else q;

# first bar of a range.  when the future engulfed bar qty is the same as the offset to that bar
def rng1 = if o_off > 0 and (o_off == getvalue(outcnt2, -o_off)) then 1 else 0;

# count down the bars in the outside range , cnt+1 to 1, from left to right
def outcountdown = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then o_off + 1
 else if outcountdown[1] > 0 then (outcountdown[1] - 1)
 else 0;

# line levels
def ohi = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then getvalue(highx, -o_off)
 else if outcountdown > 0 then ohi[1]
 else 0;

def olo = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then getvalue(lowx, -o_off)
 else if outcountdown > 0 then olo[1]
 else 0;

plot zhi = if show_horizontal_lines and ohi > 0 then ohi else na;
plot zlo = if show_horizontal_lines and olo > 0 then olo else na;
zhi.SetDefaultColor(GlobalColor("cout"));
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.hidebubble();
zlo.SetDefaultColor(GlobalColor("cout"));
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zlo.hidebubble();

# mark outside bars, that are within bigger outside bar ranges
def w = (show_outside_bars_within_larger_range and outbar and outcountdown > 1);
plot zw3 = w;
zw3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zw3.SetDefaultColor(GlobalColor("cout"));
zw3.setlineweight(2);
zw3.hidebubble();

plot zw4 = w;
zw4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zw4.SetDefaultColor(GlobalColor("cout"));
zw4.setlineweight(2);
zw4.hidebubble();


# show engulfed count in a bubble
def yfactor = 0.001;
def yo = l * (1 - yfactor);
addchartbubble(show_engulfed_count_bubbles and outbar and outcountdown == 1, yo, outcnt2, GlobalColor("cout"), no);

# show engulfed count values below
plot z2 = if show_engulfed_count_values_below and outcountdown == 1 then outcnt2 else na;
z2.SetPaintingStrategy(PaintingStrategy.VALUES_below);
z2.SetDefaultColor(GlobalColor("cout"));


# draw box around inside bar
# hollow -  open = high , close = low 
def o1 = if draw_a_box_around_outside_bar and outcountdown == 1 then h * (1 + (yfactor/1)) else na;
def c1 = if draw_a_box_around_outside_bar and outcountdown == 1 then l * (1 -(yfactor/1)) else na;
AddChart(growcolor = GlobalColor("cout"), high = o1, low = c1, open = c1, close = o1, type = ChartType.CANDLE);


#----------------------------------
# test stuff


addchartbubble(0, yo,
 outcnt1 + "\n" +
 outcnt2 + "\n" +
o_off + "\n" +
getvalue(outcnt2, -o_off) + "\n" +
outcountdown + "\n" +
rng1
, (if rng1 then color.yellow else color.gray), no);

#

outside bar
TSLA 2min
avyJxXu.jpg



--------------------------



inside bars

Code:
# inside_bars_00_wickbody

#----------------
# halcyonguy
# 23-04-03
# find inside bars, can pick a minimum quantity
# find smaller inside bars, within larger inside bar ranges
# can pick wicks or body
#----------------

# inside bars
#  count smaller future bars, after a big bar

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

DefineGlobalColor("c1", color.yellow);
# GlobalColor("c1")

input minimum_inside_bars = 1;
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;

# ignore future mother bars, within prev inside bar range
#  if countdown > 0 then ignore 
def skip_in = if incnt2 > 0 and incountdown > 0 and incountdown[1] > 1 then 1 else 0;
def skipin = (show_inside_bars_within_larger_range and skip_in);

plot zskp1 = skipin;
zskp1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zskp1.SetDefaultColor(GlobalColor("c1"));
zskp1.setlineweight(2);
zskp1.hidebubble();

plot zskp2 = skipin;
zskp2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zskp2.SetDefaultColor(GlobalColor("c1"));
zskp2.setlineweight(2);
zskp2.hidebubble();


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

plot z = if show_inside_count_values_above and incnt2 > 0 and !skipin then incnt2 else na;
z.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
z.SetDefaultColor(GlobalColor("c1"));


# draw box around inside bar
# hollow -  open = high , close = low 
def o1 = if draw_a_box_around_inside_bar and (incountdown[1] <= 1 and incountdown > 0) then h * (1 + (yoff/1)) else na;
def c1 = if draw_a_box_around_inside_bar and (incountdown[1] <= 1 and incountdown > 0) then l * (1 -(yoff/1)) else na;
AddChart(growcolor = GlobalColor("c1"), high = o1, low = c1, open = c1, close = o1, type = ChartType.CANDLE);


# 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 show_horizontal_lines and in_hi > 0 then in_hi else na;
plot zinlo = if 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();

#----------------------------
# test stuff


addchartbubble(0, l,
 incnt1 + "\n" +
 incnt2 + "\n" +
 incountdown,
 (if incountdown > 0 then color.yellow else color.gray), no);
#

inside bars
AAPL 2min
wMymWK2.jpg
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

engulf variation,
..can show all engulf or only the last one
..lines extending from last engulf
https://usethinkscript.com/threads/reverse-engulfing-indicator.17662/#post-136627


Code:
#https://usethinkscript.com/threads/find-outside-bars-and-inside-bars-with-various-quantities-of-smaller-bars.15055/

# outside_bars_line_00_engulf

#----------------
# halcyonguy
# 23-04-03
# find outside bars, can pick a minimum quantity
# find smaller outside bars, within larger outside bar ranges
# can pick wicks or body
#----------------

# outside bar , engulfing
#  previous bars are smaller than the current bar

def na = double.nan;
def bn = barnumber();
def o = open;
def h = high;
def l = low;
def c = close;
#def barup = (c > o);

DefineGlobalColor("cout", color.cyan);
#GlobalColor("cout")

input minimum_engulfed_bars = 1;
input show_engulfed_count_bubbles = yes;
input show_engulfed_count_values_below = no;
input draw_a_box_around_outside_bar = no;
input show_horizontal_lines = yes;
input show_outside_bars_within_larger_range = yes;

input show_only_last_engulf = 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);
}

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

# enable engulfing bars with enough smaller bars
def outcnt2 = if outcnt1 >= minimum_engulfed_bars then outcnt1 else 0;
def outbar = outcnt2 > 0 ;

# skip over smaller engulfed ranges
# find first bar of engulfed range
#  look ahead for biggest future outside bar, 
#   when the future engulfed bar qty is the same as the offset to it, then keep offset , j
def o_off = fold j = 1 to loop1
  with q
  do if getvalue(outcnt2, -j) == j then j else q;


# first bar of a range.  when the future engulfed bar qty is the same as the offset to that bar
def rng1 = if o_off > 0 and (o_off == getvalue(outcnt2, -o_off)) then 1 else 0;


# count down the bars in the outside range , cnt+1 to 1, from left to right
def outcountdown = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then o_off + 1
 else if outcountdown[1] > 0 then (outcountdown[1] - 1)
 else 0;

# line levels
def ohi = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then getvalue(highx, -o_off)
 else if outcountdown > 0 then ohi[1]
 else 0;

def olo = if bn == 1 then 0
 else if outcountdown[1] <= 1 and rng1 then getvalue(lowx, -o_off)
 else if outcountdown > 0 then olo[1]
 else 0;


# find the first bar in an engulf group
def linefirst = if ohi > 0 and ohi[1] != ohi then 1 else 0;
def firstbn = if linefirst then bn else firstbn[1];
def last_engulfbn = highestall(firstbn);

# master enable 
def en = if show_only_last_engulf and bn >= last_engulfbn then 1
 else if show_only_last_engulf and bn < last_engulfbn then 0
 else 1;


plot zhi = if show_horizontal_lines and ohi > 0 and en then ohi else na;
plot zlo = if show_horizontal_lines and olo > 0 and en then olo else na;
zhi.SetDefaultColor(GlobalColor("cout"));
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.hidebubble();
zlo.SetDefaultColor(GlobalColor("cout"));
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zlo.hidebubble();

# mark outside bars, that are within bigger outside bar ranges
def w = (show_outside_bars_within_larger_range and outbar and outcountdown > 1 and en);
plot zw3 = w;
zw3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zw3.SetDefaultColor(GlobalColor("cout"));
zw3.setlineweight(2);
zw3.hidebubble();

plot zw4 = w;
zw4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
zw4.SetDefaultColor(GlobalColor("cout"));
zw4.setlineweight(2);
zw4.hidebubble();


# show engulfed count in a bubble
def yfactor = 0.001;
def yo = l * (1 - yfactor);
addchartbubble(show_engulfed_count_bubbles and outbar and outcountdown == 1 and en, yo, outcnt2, GlobalColor("cout"), no);

# show engulfed count values below
plot z2 = if show_engulfed_count_values_below and outcountdown == 1 and en then outcnt2 else na;
z2.SetPaintingStrategy(PaintingStrategy.VALUES_below);
z2.SetDefaultColor(GlobalColor("cout"));


# draw box around inside bar
# hollow -  open = high , close = low 
def o1 = if draw_a_box_around_outside_bar and outcountdown == 1 and en then h * (1 + (yfactor/1)) else na;
def c1 = if draw_a_box_around_outside_bar and outcountdown == 1 then l * (1 -(yfactor/1)) else na;
AddChart(growcolor = GlobalColor("cout"), high = o1, low = c1, open = c1, close = o1, type = ChartType.CANDLE);

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

# entend lines from last engulf

input show_entend_lines = yes;
def ext_hi = if !show_entend_lines then 0
 else if bn == 1 or isnan(close) then 0
 else if bn >= last_engulfbn and ohi[1] > 0 and ohi == 0 then ohi[1]
 else ext_hi[1];

def ext_lo = if !show_entend_lines then 0
 else if bn == 1 or isnan(close) then 0
 else if bn >= last_engulfbn and olo[1] > 0 and olo == 0 then olo[1]
 else ext_lo[1];

plot zexthi = if ext_hi > 0 then ext_hi else na;
zexthi.SetDefaultColor(color.yellow);
zexthi.setlineweight(2);
zexthi.hidebubble();

plot zextlo = if ext_lo > 0 then ext_lo else na;
zextlo.SetDefaultColor(color.yellow);
zextlo.setlineweight(2);
zextlo.hidebubble();


#----------------------------------
# test stuff

addchartbubble(0, yo,
 outcnt1 + "\n" +
 outcnt2 + "\n" +
o_off + "\n" +
getvalue(outcnt2, -o_off) + "\n" +
outcountdown + "\n" +
rng1
, (if rng1 then color.yellow else color.gray), no);


#addchartbubble(linefirst, low,
addchartbubble(0 and en, low,
"1"
, color.yellow, no);
#

CAT hour
yellow lines extneding from last engulfing
SODqCte.jpg
 
Last edited:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
415 Online
Create Post

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