def na = double.nan;
def bn = barnumber();
input minimum_engulfed_bars = 3;
input show_engulfed_count_bubbles = yes;
input show_horizontal_engulf_lines = yes;
def lookback = 48;
def barup = (close > open);
def bigbarcnt1 = fold k = 1 to lookback
with p
while (high >= getvalue(high, k) and low <= getvalue(low, k))
do p + 1;
def bigbarcnt2 = if bigbarcnt1 >= minimum_engulfed_bars then bigbarcnt1 else 0;
def bigbar_off2 = fold m = 1 to lookback
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;
addchartbubble(show_engulfed_count_bubbles and outbar,(low * 0.999), bigbarcnt3, if bigbarcnt3 == 0 then color.dark_gray else if barup then color.green else color.red, no);
def engulf_off = fold n = 0 to lookback
with r
while getvalue(bigbarcnt3, -n) == 0
do r + 1;
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.SetDefaultColor(Color.cyan);
zhi.hidebubble();
zlo.SetDefaultColor(Color.cyan);
zlo.hidebubble();
def engulf_first = bn - bigbarcnt3;
input test_show_first_last_bns = yes;
addchartbubble(test_show_first_last_bns and outbar,(low * 0.999),
bn + " this bn\n" +
engulf_first + " first bn\n"
, color.gray, no);
def line_start = if (isnan(gulf_hi[1]) and !isnan(gulf_hi)) then 1 else 0;
def start_bn = if line_start then bn else start_bn[1];
input test_show_first_bn = yes;
addchartbubble(test_show_first_bn and line_start, gulf_hi,
start_bn + " start bn\n" +
engulf_off + " engulfed bars"
, color.yellow, yes);
input test1 = no;
addchartbubble(test1 , high,
engulf_off + "\n" +
line_start + "\n" +
start_bn
, (if line_start then color.yellow else color.gray), yes);
input test2 = no;
addchartbubble(test2, (low * 0.999),
bigbarcnt1 + " 1\n" +
bigbarcnt2 + " 2\n" +
bigbar_off2 + " off2\n" +
bigbarcnt3 + " 3\n"
, color.cyan, no);
def test_bubbles1 = 0;
addchartbubble((test_bubbles1 and bigbarcnt1 > 0),(low * 0.999), bigbarcnt1 + " 1", if bigbarcnt1 == 0 then color.dark_gray else if barup then color.green else color.red, no);
addchartbubble((test_bubbles1 and bigbarcnt2 >= minimum_engulfed_bars),(low * 0.999), bigbarcnt2 + " 2", if bigbarcnt2 == 0 then color.dark_gray else if barup then color.green else color.red, no);
addchartbubble((test_bubbles1 and bigbarcnt3 >= minimum_engulfed_bars),(low * 0.999), bigbarcnt3 + " 3", if bigbarcnt3 == 0 then color.dark_gray else if barup then color.green else color.red, no);
def engulf_bars = if outbar then bigbarcnt3 else engulf_bars[1];
addlabel(1, "recent engulf bars, quantity " + engulf_bars, color.yellow);
def engulfed_hi;
def engulfed_lo;
def engulfed_hibn;
def engulfed_lobn;
def xbn;
if line_start then {
xbn = bn;
engulfed_hi = fold g1 = 0 to (engulf_off-0)
with g2
while !isnan(gulf_hi)
do (if getvalue(high, -g1) > g2 then getvalue(high, -g1) else g2);
engulfed_lo = fold h1 = 0 to (engulf_off-0)
with h2 = 99999
while !isnan(gulf_hi)
do (if getvalue(low, -h1) < h2 then getvalue(low, -h1) else h2);
engulfed_hibn = fold g3 = 0 to (engulf_off - 0)
with g4
while !isnan(gulf_hi)
do (if engulfed_hi == getvalue(high, -g3) then (bn + g3) else g4);
engulfed_lobn = fold h3 = 0 to (engulf_off-0)
with h4
while !isnan(gulf_hi)
do (if engulfed_lo == getvalue(low, -h3) then (bn + h3) else h4);
} else {
engulfed_hi = engulfed_hi[1];
engulfed_lo = engulfed_lo[1];
engulfed_hibn = engulfed_hibn[1];
engulfed_lobn = engulfed_lobn[1];
xbn = xbn[1];
}
input show_points_onhighest_lowest_in_engulfed_group = yes;
plot zenhi = if show_points_onhighest_lowest_in_engulfed_group and bn == engulfed_hibn then high else na;
zenhi.SetPaintingStrategy(PaintingStrategy.POINTS);
zenhi.SetDefaultColor(Color.white);
zenhi.setlineweight(2);
zenhi.hidebubble();
plot zenlo = if show_points_onhighest_lowest_in_engulfed_group and bn == engulfed_lobn then low else na;
zenlo.SetPaintingStrategy(PaintingStrategy.POINTS);
zenlo.SetDefaultColor(Color.white);
zenlo.setlineweight(2);
zenlo.hidebubble();
input test_hi_lo_bns = yes;
addchartbubble(test_hi_lo_bns and line_start, gulf_lo*0.999,
"hi $" + engulfed_hi + " at bn: " + engulfed_hibn + "\n" +
"lo $" + engulfed_lo + " at bn: " + engulfed_lobn
, color.yellow, no);
def engulf_minbn = min( engulfed_hibn, engulfed_lobn);
def t = if line_start then ((bn + engulf_off) - engulf_minbn) else t[1];
def t_start_off = engulf_minbn - bn;
def isfirst_hi = if getvalue(high, -t_start_off) == engulfed_hi then 1 else 0;
addchartbubble(line_start, gulf_lo*0.999,
getvalue(high, -t_start_off) + " t hi\n" +
(if isfirst_hi then "High" else "Low") + " is first"
, (if isfirst_hi then color.green else color.red), no);
def t_min = 3;
def thrust_factor = if t < t_min then 0 else t;
def thrust_ratio = round(thrust_factor/ engulf_off,2);
addchartbubble(line_start, gulf_lo*0.999,
thrust_factor + " thrust_factor \n" +
"out of\n" +
engulf_off + " engulfed bars\n" +
"is a ratio of\n" +
thrust_ratio + " : 1"
, (if isfirst_hi then color.green else color.red), no);