Mother Bar

Rd2der

New member
VIP
Is it possible to modify the inside bars congestion box indicator (below), I believe written by halcyonguy, to allow following candles of the mother bars boundaries (wick to wick) to remain within the boundary until the body of candle closes below or above the mother boundary. Here is a chart example showing rectangles of desired area of mother bar. Bar on left is considered the mother bar, as your indicator depicts. The rectangle continues until a candle body closes above or below the wick of the mother bar. http://tos.mx/5W3tKa1 Thank you


# insidebars_congestion_box_ratio_00

# start with copy of
# insidebars_congestion_box_00f

# ============================================
# ============================================

def na = double.nan;
def bn = barnumber();
#-------------------------------------
# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = high;
lowx = low;
case "body":
highx = max(open, close);
lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;

input show_label__candle_levels = no;
addlabel(show_label__candle_levels, "Candle levels - " + candle_levels, color.yellow);

input show_label__min_inside_bars = no;
addlabel(show_label__min_inside_bars, "Min inside bars - " + min_inside_bars, color.yellow);
# --------------------------------


# --------------------------------
# test if a giant candle appears, then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------


# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules

#----------------------------------
# inside bar , count smaller bars after the current bar

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
# highx


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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
with q = 1
# while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
while (ht_en and

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);
(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
do q + 1;



# need to put 3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
# Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars.


#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to met
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.


addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n"

, color.cyan, no);


# ============================================
# ============================================
# ============================================


#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
# with q = 1
## while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
# while (ht_en and getvalue(v3_en, -k))
# do q + 1;

# -------------------------------
def rev_cnt =
if bn == 1 then 0
else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
else if inside_bar_count >= min_inside_bars then inside_bar_count
else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert = 0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.gray, yes);

# ----------------------------------
# plot lines

def inside_hi =
if rev_cnt == 0 then na
else if inside_start then highx
else inside_hi[1];

def inside_lo =
if rev_cnt == 0 then na
else if inside_start then lowx
else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.gray);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.gray);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.gray);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.gray);

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

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();
#
#
 
Last edited:
Solution
Is it possible to modify the inside bars congestion box indicator (below), I believe written by halcyonguy, to allow following candles of the mother bars boundaries (wick to wick) to remain within the boundary until the body of candle closes below or above the mother boundary. Here is a chart example showing rectangles of desired area of mother bar. Bar on left is considered the mother bar, as your indicator depicts. The rectangle continues until a candle body closes above or below the wick of the mother bar. http://tos.mx/5W3tKa1 Thank you

# insidebars_congestion_box_ratio_00

added,
..can choose mother bar , wick or body levels.
..can choose inside bar levels separately, wick or body.


Ruby:
#...
Is it possible to modify the inside bars congestion box indicator (below), I believe written by halcyonguy, to allow following candles of the mother bars boundaries (wick to wick) to remain within the boundary until the body of candle closes below or above the mother boundary. Here is a chart example showing rectangles of desired area of mother bar. Bar on left is considered the mother bar, as your indicator depicts. The rectangle continues until a candle body closes above or below the wick of the mother bar. http://tos.mx/5W3tKa1 Thank you

# insidebars_congestion_box_ratio_00

added,
..can choose mother bar , wick or body levels.
..can choose inside bar levels separately, wick or body.


Ruby:
# insidebars_congestion_box_ratio_01a

# add,
#   choose wick/body for mother bar
#   choose wick/body for inside bar

# insidebars_congestion_box_ratio_00


#inside bars

#this looks for a big candle (bar #1) and several smaller candles (inside bars) after it.
#if a bar is big, but less than the first bar of an existing pattern, it is ignored.
#when price moves beyond the price levels of bar #1, the pattern stops.

#. choose price levels , wicks or body.
#. choose a minimum quantity of smaller bars before a pattern will start. default is 3.
#. choose to ignore big candles. a candle much bigger won't be used to start a pattern.
#. based on comparing to an average.
#. pick the height factor(5) and average length(20)

#. show color shading, cloud.
#. show labels, wick or body, min quantity of bars.
#. show a bubble with the count of bars in the pattern.
#. show an arrow under the 1st bar of pattern.

#on each bar, it compares future bars to the current bar, to see if 3 or more are smaller.


def na = double.nan;
def bn = barnumber();
#-------------------------------------

#  mother bar

# choose wick or body price levels
input mother_bar_levels = { default "wick" , "body" };
def high_m;
def low_m;
switch (mother_bar_levels) {
case "wick":
  high_m = high;
  low_m = low;
case "body":
  high_m = max(open, close);
  low_m = min(open, close);
}

def ht_m = high_m - low_m;



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


# insidebars

# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max(open, close);
  lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;

input show_label__candle_levels = no;
addlabel(show_label__candle_levels, "Candle levels - " + candle_levels, color.yellow);

input show_label__min_inside_bars = no;
addlabel(show_label__min_inside_bars, "Min inside bars - " + min_inside_bars, color.yellow);
# --------------------------------


# --------------------------------
# test if a giant candle appears,  then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------




# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules


#----------------------------------
# inside bar , count smaller bars after the current bar


# ....................

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
#  highx



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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
  with q = 1
#  while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
  while (ht_en and 

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);

#(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
(absvalue( min(high_m, getvalue(highx, -k)) - max(low_m, getvalue(lowx, -k))) / (getvalue(highx, -k) - getvalue(lowx, -k))) >= ratio_min)
  do q + 1;



# need to put  3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
#  Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars. 



#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to met
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.



addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n" 

, color.cyan, no);


# ============================================
# ============================================
# ============================================



#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
#  with q = 1
##  while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
#  while (ht_en and getvalue(v3_en, -k))
#  do q + 1;

# -------------------------------
def rev_cnt =
 if bn == 1 then 0
 else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
 else if inside_bar_count >= min_inside_bars then inside_bar_count
 else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert = 0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.gray, yes);

# ----------------------------------
# plot lines

def inside_hi =
 if rev_cnt == 0 then na
# else if inside_start then highx
 else if inside_start then high_m
 else inside_hi[1];

def inside_lo =
 if rev_cnt == 0 then na
# else if inside_start then lowx
 else if inside_start then low_m
 else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.gray);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.gray);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.gray);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.gray);

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

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();
#
#
 
Solution
added,
..can choose mother bar , wick or body levels.
..can choose inside bar levels separately, wick or body.


Ruby:
# insidebars_congestion_box_ratio_01a

# add,
#   choose wick/body for mother bar
#   choose wick/body for inside bar

# insidebars_congestion_box_ratio_00


#inside bars

#this looks for a big candle (bar #1) and several smaller candles (inside bars) after it.
#if a bar is big, but less than the first bar of an existing pattern, it is ignored.
#when price moves beyond the price levels of bar #1, the pattern stops.

#. choose price levels , wicks or body.
#. choose a minimum quantity of smaller bars before a pattern will start. default is 3.
#. choose to ignore big candles. a candle much bigger won't be used to start a pattern.
#. based on comparing to an average.
#. pick the height factor(5) and average length(20)

#. show color shading, cloud.
#. show labels, wick or body, min quantity of bars.
#. show a bubble with the count of bars in the pattern.
#. show an arrow under the 1st bar of pattern.

#on each bar, it compares future bars to the current bar, to see if 3 or more are smaller.


def na = double.nan;
def bn = barnumber();
#-------------------------------------

#  mother bar

# choose wick or body price levels
input mother_bar_levels = { default "wick" , "body" };
def high_m;
def low_m;
switch (mother_bar_levels) {
case "wick":
  high_m = high;
  low_m = low;
case "body":
  high_m = max(open, close);
  low_m = min(open, close);
}

def ht_m = high_m - low_m;



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


# insidebars

# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max(open, close);
  lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;

input show_label__candle_levels = no;
addlabel(show_label__candle_levels, "Candle levels - " + candle_levels, color.yellow);

input show_label__min_inside_bars = no;
addlabel(show_label__min_inside_bars, "Min inside bars - " + min_inside_bars, color.yellow);
# --------------------------------


# --------------------------------
# test if a giant candle appears,  then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------




# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules


#----------------------------------
# inside bar , count smaller bars after the current bar


# ....................

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
#  highx



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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
  with q = 1
#  while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
  while (ht_en and

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);

#(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
(absvalue( min(high_m, getvalue(highx, -k)) - max(low_m, getvalue(lowx, -k))) / (getvalue(highx, -k) - getvalue(lowx, -k))) >= ratio_min)
  do q + 1;



# need to put  3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
#  Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars.



#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to met
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.



addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n"

, color.cyan, no);


# ============================================
# ============================================
# ============================================



#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
#  with q = 1
##  while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
#  while (ht_en and getvalue(v3_en, -k))
#  do q + 1;

# -------------------------------
def rev_cnt =
 if bn == 1 then 0
 else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
 else if inside_bar_count >= min_inside_bars then inside_bar_count
 else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert = 0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.gray, yes);

# ----------------------------------
# plot lines

def inside_hi =
 if rev_cnt == 0 then na
# else if inside_start then highx
 else if inside_start then high_m
 else inside_hi[1];

def inside_lo =
 if rev_cnt == 0 then na
# else if inside_start then lowx
 else if inside_start then low_m
 else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.gray);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.gray);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.gray);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.gray);
mas
# ---------------------------------------

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();
#
#
Thank you, for your time and expertise! An absolute masterful piece art! Highest regards.
 
I have played around with a couple of your scripts halcyonguy. I have not really done any coding since the late 80's. So I have been just splicing things together and making it work little by little. Here is what I would like to do with your indicator. I would like it to place a outside count bubble for when the price exits the box. I would then like to know how many candles close either higher or lower than the exit price until a new congestion box. I then would like to utilize the labels that you created in another version to calculate the highest percentage of winning candles over the past opportunities. I tried to not butcher your code too much. Thanks for any help.

# insidebars_congestion_box_ratio_01a

# add,
# choose wick/body for mother bar
# choose wick/body for inside bar

# insidebars_congestion_box_ratio_00


#inside bars

#this looks for a big candle (bar #1) and several smaller candles (inside bars) after it.
#if a bar is big, but less than the first bar of an existing pattern, it is ignored.
#when price moves beyond the price levels of bar #1, the pattern stops.

#. choose price levels , wicks or body.
#. choose a minimum quantity of smaller bars before a pattern will start. default is 3.
#. choose to ignore big candles. a candle much bigger won't be used to start a pattern.
#. based on comparing to an average.
#. pick the height factor(5) and average length(20)

#. show color shading, cloud.
#. show labels, wick or body, min quantity of bars.
#. show a bubble with the count of bars in the pattern.
#. show an arrow under the 1st bar of pattern.

#on each bar, it compares future bars to the current bar, to see if 3 or more are smaller.


def na = double.nan;
def bn = barnumber();
#-------------------------------------

# mother bar

# choose wick or body price levels
input mother_bar_levels = { default "wick" , "body" };
def high_m;
def low_m;
switch (mother_bar_levels) {
case "wick":
high_m = high;
low_m = low;
case "body":
high_m = max(open, close);
low_m = min(open, close);
}

def ht_m = high_m - low_m;



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


# insidebars

# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = high;
lowx = low;
case "body":
highx = max(open, close);
lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;
input min_outside_bars=3;


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


# --------------------------------
# test if a giant candle appears, then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------




# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules


#----------------------------------
# inside bar , count smaller bars after the current bar


# ....................

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
# highx



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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
with q = 1
# while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
while (ht_en and

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);

#(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
(absvalue( min(high_m, getvalue(highx, -k)) - max(low_m, getvalue(lowx, -k))) / (getvalue(highx, -k) - getvalue(lowx, -k))) >= ratio_min)
do q + 1;



# need to put 3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
# Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars.



#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to meet
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.



addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n"

, color.cyan, no);


# ============================================
# ============================================
# ============================================



#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
# with q = 1
## while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
# while (ht_en and getvalue(v3_en, -k))
# do q + 1;

# -------------------------------
def rev_cnt =
if bn == 1 then 0
else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
else if inside_bar_count >= min_inside_bars then inside_bar_count
else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert =0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.yellow, yes);

# ----------------------------------
# plot lines

def inside_hi =
if rev_cnt == 0 then na
# else if inside_start then highx
else if inside_start then high_m
else inside_hi[1];

def inside_lo =
if rev_cnt == 0 then na
# else if inside_start then lowx
else if inside_start then low_m
else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.yellow);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.yellow);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.white);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.yellow);

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

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

input show_arrow_on_last_bar = no;
plot z2 = if show_arrow_on_last_bar and inside_start then (low * (1 - vert)) else na;
z2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();


# count how many patterns on chart
# if a stock has many, more opportuneties to catch a move
def cnt3 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 3) then (cnt3[1] + 1) else cnt3[1];
def cnt4 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 4) then (cnt4[1] + 1) else cnt4[1];
def cnt5 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 5) then (cnt5[1] + 1) else cnt5[1];
def cnt6 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 6) then (cnt6[1] + 1) else cnt6[1];
def cnt7 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 7) then (cnt7[1] + 1) else cnt7[1];
def cnt8 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 8) then (cnt8[1] + 1) else cnt8[1];
def cnt9 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 9) then (cnt9[1] + 1) else cnt9[1];

def cnt10 = if bn == 1 then 0 else if (inside_start and inside_bar_count > 9) then (cnt10[1] + 1) else cnt10[1];

input cnt_labels = yes;
addlabel(cnt_labels, " " , color.black);
addlabel(cnt_labels, "3 bars 75% 3 bars " + cnt3, color.yellow);
addlabel(cnt_labels, "4 bars " + cnt4, color.yellow);
addlabel(cnt_labels, "5 bars " + cnt5, color.yellow);
addlabel(cnt_labels, "6 bars " + cnt6, color.yellow);
addlabel(cnt_labels, "7 bars " + cnt7, color.yellow);
addlabel(cnt_labels, "8 bars " + cnt8, color.yellow);
addlabel(cnt_labels, "9 bars " + cnt9, color.yellow);
addlabel(cnt_labels, "10+ bars " + cnt10, color.yellow);

#
 
I have played around with a couple of your scripts halcyonguy. I have not really done any coding since the late 80's. So I have been just splicing things together and making it work little by little. Here is what I would like to do with your indicator. I would like it to place a outside count bubble for when the price exits the box. I would then like to know how many candles close either higher or lower than the exit price until a new congestion box. I then would like to utilize the labels that you created in another version to calculate the highest percentage of winning candles over the past opportunities. I tried to not butcher your code too much. Thanks for any help.

# insidebars_congestion_box_ratio_01a

# add,
# choose wick/body for mother bar
# choose wick/body for inside bar

# insidebars_congestion_box_ratio_00


#inside bars

#this looks for a big candle (bar #1) and several smaller candles (inside bars) after it.
#if a bar is big, but less than the first bar of an existing pattern, it is ignored.
#when price moves beyond the price levels of bar #1, the pattern stops.

#. choose price levels , wicks or body.
#. choose a minimum quantity of smaller bars before a pattern will start. default is 3.
#. choose to ignore big candles. a candle much bigger won't be used to start a pattern.
#. based on comparing to an average.
#. pick the height factor(5) and average length(20)

#. show color shading, cloud.
#. show labels, wick or body, min quantity of bars.
#. show a bubble with the count of bars in the pattern.
#. show an arrow under the 1st bar of pattern.

#on each bar, it compares future bars to the current bar, to see if 3 or more are smaller.


def na = double.nan;
def bn = barnumber();
#-------------------------------------

# mother bar

# choose wick or body price levels
input mother_bar_levels = { default "wick" , "body" };
def high_m;
def low_m;
switch (mother_bar_levels) {
case "wick":
high_m = high;
low_m = low;
case "body":
high_m = max(open, close);
low_m = min(open, close);
}

def ht_m = high_m - low_m;



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


# insidebars

# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = high;
lowx = low;
case "body":
highx = max(open, close);
lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;
input min_outside_bars=3;


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


# --------------------------------
# test if a giant candle appears, then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------




# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules


#----------------------------------
# inside bar , count smaller bars after the current bar


# ....................

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
# highx



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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
with q = 1
# while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
while (ht_en and

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);

#(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
(absvalue( min(high_m, getvalue(highx, -k)) - max(low_m, getvalue(lowx, -k))) / (getvalue(highx, -k) - getvalue(lowx, -k))) >= ratio_min)
do q + 1;



# need to put 3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
# Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars.



#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to meet
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.



addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n"

, color.cyan, no);


# ============================================
# ============================================
# ============================================



#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
# with q = 1
## while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
# while (ht_en and getvalue(v3_en, -k))
# do q + 1;

# -------------------------------
def rev_cnt =
if bn == 1 then 0
else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
else if inside_bar_count >= min_inside_bars then inside_bar_count
else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert =0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.yellow, yes);

# ----------------------------------
# plot lines

def inside_hi =
if rev_cnt == 0 then na
# else if inside_start then highx
else if inside_start then high_m
else inside_hi[1];

def inside_lo =
if rev_cnt == 0 then na
# else if inside_start then lowx
else if inside_start then low_m
else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.yellow);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.yellow);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.white);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.yellow);

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

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

input show_arrow_on_last_bar = no;
plot z2 = if show_arrow_on_last_bar and inside_start then (low * (1 - vert)) else na;
z2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();


# count how many patterns on chart
# if a stock has many, more opportuneties to catch a move
def cnt3 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 3) then (cnt3[1] + 1) else cnt3[1];
def cnt4 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 4) then (cnt4[1] + 1) else cnt4[1];
def cnt5 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 5) then (cnt5[1] + 1) else cnt5[1];
def cnt6 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 6) then (cnt6[1] + 1) else cnt6[1];
def cnt7 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 7) then (cnt7[1] + 1) else cnt7[1];
def cnt8 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 8) then (cnt8[1] + 1) else cnt8[1];
def cnt9 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 9) then (cnt9[1] + 1) else cnt9[1];

def cnt10 = if bn == 1 then 0 else if (inside_start and inside_bar_count > 9) then (cnt10[1] + 1) else cnt10[1];

input cnt_labels = yes;
addlabel(cnt_labels, " " , color.black);
addlabel(cnt_labels, "3 bars 75% 3 bars " + cnt3, color.yellow);
addlabel(cnt_labels, "4 bars " + cnt4, color.yellow);
addlabel(cnt_labels, "5 bars " + cnt5, color.yellow);
addlabel(cnt_labels, "6 bars " + cnt6, color.yellow);
addlabel(cnt_labels, "7 bars " + cnt7, color.yellow);
addlabel(cnt_labels, "8 bars " + cnt8, color.yellow);
addlabel(cnt_labels, "9 bars " + cnt9, color.yellow);
addlabel(cnt_labels, "10+ bars " + cnt10, color.yellow);

#
Thank you, Powerhousescott. I loaded your revised script. In the chart included is how I determine a new mother bar. The only setting, I changed was to not display the bubbles. Please let me know if I am interpreting the concept of a MB incorrectly. Thank you again!! http://tos.mx/IakCQzV
 
Thank you, Powerhousescott. I loaded your revised script. In the chart included is how I determine a new mother bar. The only setting, I changed was to not display the bubbles. Please let me know if I am interpreting the concept of a MB incorrectly. Thank you again!! http://tos.mx/IakCQzV
I notice that the indicator doesn't update during a live trade. the indicator only plots the mother bar after the chart has been refreshed.
 
I have played around with a couple of your scripts halcyonguy. I have not really done any coding since the late 80's. So I have been just splicing things together and making it work little by little. Here is what I would like to do with your indicator. I would like it to place a outside count bubble for when the price exits the box. I would then like to know how many candles close either higher or lower than the exit price until a new congestion box. I then would like to utilize the labels that you created in another version to calculate the highest percentage of winning candles over the past opportunities. I tried to not butcher your code too much. Thanks for any help.

# insidebars_congestion_box_ratio_01a

# add,
# choose wick/body for mother bar
# choose wick/body for inside bar

# insidebars_congestion_box_ratio_00


#inside bars

#this looks for a big candle (bar #1) and several smaller candles (inside bars) after it.
#if a bar is big, but less than the first bar of an existing pattern, it is ignored.
#when price moves beyond the price levels of bar #1, the pattern stops.

#. choose price levels , wicks or body.
#. choose a minimum quantity of smaller bars before a pattern will start. default is 3.
#. choose to ignore big candles. a candle much bigger won't be used to start a pattern.
#. based on comparing to an average.
#. pick the height factor(5) and average length(20)

#. show color shading, cloud.
#. show labels, wick or body, min quantity of bars.
#. show a bubble with the count of bars in the pattern.
#. show an arrow under the 1st bar of pattern.

#on each bar, it compares future bars to the current bar, to see if 3 or more are smaller.


def na = double.nan;
def bn = barnumber();
#-------------------------------------

# mother bar

# choose wick or body price levels
input mother_bar_levels = { default "wick" , "body" };
def high_m;
def low_m;
switch (mother_bar_levels) {
case "wick":
high_m = high;
low_m = low;
case "body":
high_m = max(open, close);
low_m = min(open, close);
}

def ht_m = high_m - low_m;



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


# insidebars

# choose wick or body price levels
input candle_levels = { "wick" , default "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
highx = high;
lowx = low;
case "body":
highx = max(open, close);
lowx = min(open, close);
}

def ht = highx - lowx;
#-----------------------------------
input min_inside_bars = 3;
input min_outside_bars=3;


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


# --------------------------------
# test if a giant candle appears, then ignore it
input ignore_big_candles = no;
input candle_ht_avg_multiplier_max = 5.0;
input candle_height_avg_len = 20;

def ht_avg = round(Average( ht[1], candle_height_avg_len),2);
def ht_avg_factor = round(ht / ht_avg, 1);

def ht_en = !ignore_big_candles or (ht < (ht_avg * candle_ht_avg_multiplier_max));
#----------------------------------




# new code here

# ============================================
# ============================================
# ============================================

# rewrite the rules


#----------------------------------
# inside bar , count smaller bars after the current bar


# ....................

# finding bar1 will have to be rewritten, because there are new rules to determine what bars are in congestion. the large bar, bar1, is found by looking at future bars, not bars in the past.
# 1. First find the big bar, this bar is just a factor*average range of past n bars. i can see The script already has done this perfectly

# redo these formulas below
# inside_start , true/false
# start_cnt , incr counter, starts at 1 on bar1


# 2. Next define the high of the first big bar, bar1, found as
# highx



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

input ratio_min = 0.9;

# inside bar , count bars after the current bar that meet the rules
def max_bars = 50;
def inside_bar_count = fold k = 1 to max_bars
with q = 1
# while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
while (ht_en and

# v1 = min(highx, getvalue(high, -k)
# v2 = max(lowx, getvalue(low, -k)
# v3 = absvalue(v1-v2)/(high[0]-low[0]);

#(absvalue( min(highx, getvalue(high, -k)) - max(lowx, getvalue(low, -k))) / (getvalue(high, -k) - getvalue(low, -k))) >= ratio_min)
(absvalue( min(high_m, getvalue(highx, -k)) - max(low_m, getvalue(lowx, -k))) / (getvalue(highx, -k) - getvalue(lowx, -k))) >= ratio_min)
do q + 1;



# need to put 3 to 6 within a fold loop that looks at future bars
# this is just comparing to itself
# 3. comapare bar1 high to current high, find min
#def v1 = min(highx, high[0]);


# 4. comapare bar1 low to current low, find max
#def v2 = max(lowx, low[0]);
# Please note the high[0] or Low[0] CAN be outside the highx and lowx range of FIRST big bar.


# 5. calc a ratio and compare to some number
#def v3 = absvalue(v1-v2)/(high[0]-low[0]);


# 6. v3 has to be greater than 0.9, which is a static number.
#input ratio_min = 0.9;
#def v3_en = if v3 >= ratio_min then 1 else 0;

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

# 7. if above condition is met, v3_en is true, then this bar is an inside bar and as long as this condition is met, keep counting inside bars.



#Let's say the first big bar is identified, then the second inside bar is also identified, the current bar has to meet
#v1=lowest(highx, high[0])
#v2=highest(lowx, low[0]) and
#v3=abs(v1-v2)/(high[0]-low[0])>0.9 again. Please note the high or low of the second bar is not used, i only care about the current bar and FIRST big bar relationship

#If the condition is not met. Then this pattern ends.



addchartbubble(0, low*0.997,
bn + "\n" +
inside_bar_count + "\n"

, color.cyan, no);


# ============================================
# ============================================
# ============================================



#----------------------------------
# inside bar , count smaller bars after the current bar
#def max_bars = 50;
#def inside_bar_count = fold k = 1 to max_bars
# with q = 1
## while (ht_en and highx[0] >= getvalue(highx, -k) and lowx[0] <= getvalue(lowx, -k))
# while (ht_en and getvalue(v3_en, -k))
# do q + 1;

# -------------------------------
def rev_cnt =
if bn == 1 then 0
else if rev_cnt[1] > 1 then (rev_cnt[1] - 1)
else if inside_bar_count >= min_inside_bars then inside_bar_count
else 0;

def inside_start = (rev_cnt[1] <= 1 and inside_bar_count >= min_inside_bars);
def start_cnt = if bn == 1 then 0 else if inside_start then (start_cnt[1] + 1) else start_cnt[1];

#def vert = 0.0006;
def vert =0.00;
input show_cloud = yes;
input show_bubble_pattern_bar_count = yes;
addchartbubble(show_bubble_pattern_bar_count and inside_start, (high * (1 + vert)), inside_bar_count, color.yellow, yes);

# ----------------------------------
# plot lines

def inside_hi =
if rev_cnt == 0 then na
# else if inside_start then highx
else if inside_start then high_m
else inside_hi[1];

def inside_lo =
if rev_cnt == 0 then na
# else if inside_start then lowx
else if inside_start then low_m
else inside_lo[1];

plot zhi = inside_hi;
plot zlo = inside_lo;

zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zhi.SetStyle(Curve.MEDIUM_DASH);
#zhi.SetDefaultColor(Color.cyan);
zhi.SetDefaultColor(Color.yellow);
zhi.setlineweight(1);
zhi.hidebubble();

zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zlo.SetStyle(Curve.MEDIUM_DASH);
#zlo.SetDefaultColor(Color.cyan);
zlo.SetDefaultColor(Color.yellow);
zlo.setlineweight(1);
zlo.hidebubble();

# ---------------------------------------
# use 2 clouds, to avoid 2 adjacent patterns being connected
def iscnt_odd = ( start_cnt % 2 == 1 );
def zhiodd = if (show_cloud and iscnt_odd) then zhi else na;
addcloud(zhiodd, zlo, color.white);
def zhieven = if (show_cloud and !iscnt_odd) then zhi else na;
addcloud(zhieven, zlo, color.yellow);

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

input show_arrow_on_bar1 = no;
plot z1 = if show_arrow_on_bar1 and inside_start then (low * (1 - vert)) else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

input show_arrow_on_last_bar = no;
plot z2 = if show_arrow_on_last_bar and inside_start then (low * (1 - vert)) else na;
z2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();


# count how many patterns on chart
# if a stock has many, more opportuneties to catch a move
def cnt3 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 3) then (cnt3[1] + 1) else cnt3[1];
def cnt4 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 4) then (cnt4[1] + 1) else cnt4[1];
def cnt5 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 5) then (cnt5[1] + 1) else cnt5[1];
def cnt6 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 6) then (cnt6[1] + 1) else cnt6[1];
def cnt7 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 7) then (cnt7[1] + 1) else cnt7[1];
def cnt8 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 8) then (cnt8[1] + 1) else cnt8[1];
def cnt9 = if bn == 1 then 0 else if (inside_start and inside_bar_count == 9) then (cnt9[1] + 1) else cnt9[1];

def cnt10 = if bn == 1 then 0 else if (inside_start and inside_bar_count > 9) then (cnt10[1] + 1) else cnt10[1];

input cnt_labels = yes;
addlabel(cnt_labels, " " , color.black);
addlabel(cnt_labels, "3 bars 75% 3 bars " + cnt3, color.yellow);
addlabel(cnt_labels, "4 bars " + cnt4, color.yellow);
addlabel(cnt_labels, "5 bars " + cnt5, color.yellow);
addlabel(cnt_labels, "6 bars " + cnt6, color.yellow);
addlabel(cnt_labels, "7 bars " + cnt7, color.yellow);
addlabel(cnt_labels, "8 bars " + cnt8, color.yellow);
addlabel(cnt_labels, "9 bars " + cnt9, color.yellow);
addlabel(cnt_labels, "10+ bars " + cnt10, color.yellow);

#
Great work! Thank you very much! Best regards
 

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