find engulfing bars, while allowing x number of breakouts in the series.
[/code]
Ruby:
# engulf_skip_x_breakouts_01
# engulfing , with x breakouts
# allow x quantity of false conditions to exist in a series of true's
#-------------------------------
def bn = BarNumber();
def na = Double.NaN;
input allowed_misses = 1;
AddLabel(1, "allowed breakouts " + allowed_misses, Color.YELLOW);
def maxnum = 200;
def n = allowed_misses;
def prev_cnt = fold j = 0 to maxnum
with p
while p <= n
do p + ( if p == n then j - (n+1) else
# engulf - check if prev bar is brkout
if (getvalue(high, j) > high) or (getvalue(low, j) < low) then 1 else 0 );
#---------------------------------
def bigbarcnt1 = prev_cnt;
def minimum_engulfed_bars = allowed_misses + 2;
def lookback = 48;
def offset = lookback;
def barup = (close > open);
addlabel(1, "minimum bars " + minimum_engulfed_bars, color.yellow);
# disable engulfing bars with too few smaller bars
def bigbarcnt2 = if bigbarcnt1 >= minimum_engulfed_bars then bigbarcnt1 else 0;
# is the next future engulf bar, engulfing the current engulf bar?
# if yes , then disable current engulf bar
#def bigbar_off2 = fold m = 1 to lookback
def bigbar_off2 = fold m = 1 to offset
with q = 1
while getvalue(bigbarcnt2, -m) == 0
do q + 1;
def bigbarcnt3;
if isnan( getvalue(high, -bigbar_off2) ) then {
bigbarcnt3 = bigbarcnt2;
} else if (getvalue(high, -bigbar_off2) > high and getvalue(low, -bigbar_off2) < low) then {
bigbarcnt3 = 0;
} else {
bigbarcnt3 = bigbarcnt2;
}
def outbar = if (bigbarcnt3 >= minimum_engulfed_bars) then 1 else 0;
input engulfed_count_bubbles = yes;
addchartbubble(engulfed_count_bubbles and outbar,(low * 0.999),
bigbarcnt3 + "\n"
,(if bigbarcnt3 == 0 then color.dark_gray else if barup then color.green else color.red), no);
# ------------------------------
# add horz lines over the engulfing range
def engulf_off = fold t = 0 to offset
with r
while getvalue(bigbarcnt3, -t) == 0
do r + 1;
input show_horizontal_engulf_lines = yes;
def gulf_hi = if (show_horizontal_engulf_lines and getvalue(bigbarcnt3, -engulf_off) >= engulf_off) then getvalue(high, -engulf_off) else na;
def gulf_lo = if (show_horizontal_engulf_lines and getvalue(bigbarcnt3, -engulf_off) >= engulf_off) then getvalue(low, -engulf_off) else na;
plot zhi = gulf_hi;
plot zlo = gulf_lo;
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.SetDefaultColor(Color.cyan);
zhi.hidebubble();
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zlo.SetDefaultColor(Color.cyan);
zlo.hidebubble();
#--------
addchartbubble(0, (low * 0.999),
bigbarcnt1 + " 1\n" +
bigbarcnt2 + " 2\n" +
bigbar_off2 + " off2\n" +
bigbarcnt3 + " 3\n"
# engulf_off
, color.cyan, no);
#
Last edited: