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();
#
#
# 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: