Here's halcyonguy's code for the Thrust Factor. It's the strength of a completed pattern that breaks in either direction. You take this value and compare it to any Impedance values in the direction of favorable movement once both ends of the engulfed range have been eclipsed. The first Impedance Point reached, based upon the Pivots or the PDI as mentioned earlier that are >= the Thrust Factor determines your exit point. Take the pre-defined (as an input) percentage of the move from the entry point to that pivot-based Impedance Point as the price to exit MIT. In the case of the PDI stopping a trade, it is AT the first PDI reached that is >= The Thrust Factor.
<p>
The endless (we hope) battle of the irresistible force and the immovable object is always in play with this strategy. It's not omniscient or 100% accurate as far as the comparative values while actual market action theoretically takes into account other mundane factors such as volume and its variations, but I think it's close enough to perfect for me.
<p>
The Thrust Ratio in the following code is really not necessary, though it might be interesting to see how many times you have 3 or more bars engulfed without at least a 3 Thrust Factor. Hats off to halcyonguy for his knowledge and efforts here!...
<p>
# outsidebars_02b_engulf_cln
# mod with his rules
# rework engulf , # with thrust terms
#
https://usethinkscript.com/threads/...thin-a-known-group-of-bars.12721/#post-108393
# post10 TNTrader5159
#------------------------------------
# engulfing pattern,
# find qty of engulf bars, display in a bubble
# draw lines to mother bar
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 offset = lookback;
def barup = (close > open);
# find engulfing bar
# look backwards for an engulfing bar
# count how many previous bars are smaller than the current bar
#def lookback = 48;
def bigbarcnt1 = fold k = 1 to lookback
with p
while (high >= getvalue(high, k) and low <= getvalue(low, k))
do p + 1;
# 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
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;
# bubble with qty of engulfed bars
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);
# look ahead to find qty of bars to next mother bar
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);
# find the first bar in an engulfed line, which is the same as the engulfed group
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);
#-----------------------------
# test stuff
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"
# engulf_off
, color.cyan, no);
#input test_bubbles1 = 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);
# mod with his rules in post#10
# rework engulf , with thrust terms
#
https://usethinkscript.com/threads/...thin-a-known-group-of-bars.12721/#post-108393
# post10 TNTrader5159
#------------------------------------
# make new vars, labels
# 1. Yes, first to see HOW MANY bars the current bar may engulf.
# recent engulf quantity
def engulf_bars = if outbar then bigbarcnt3 else engulf_bars[1];
addlabel(1, "recent engulf bars, quantity " + engulf_bars, color.yellow);
# 2. Then to find the relative positions of the highest and lowest prices within the engulfed range.
# loop , from line_start to outbar
# loop , 0 to engulf_off ( the qty of engulded bars )
def engulfed_hi;
def engulfed_lo;
def engulfed_hibn;
def engulfed_lobn;
def xbn;
if line_start then {
xbn = bn;
# start at the 1st bar of an engulfed period,
# look at future bars for the highest price
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);
# look at future bars for the lowest price
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);
# look for the highest and lowest price , and read the bn
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];
}
# plot points on highest and lowest prices, that are engulfed
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;
#plot zenlo = if bn == xbn 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);
# 3. Then to take the farther one back or lower bar number of those two prices, and subtract it from the current bar number.
def engulf_minbn = min( engulfed_hibn, engulfed_lobn);
def t = if line_start then ((bn + engulf_off) - engulf_minbn) else t[1];
# offset from first engulfed bar , to the first low/hi
def t_start_off = engulf_minbn - bn;
# 4. This returns the final value I call the Thrust Factor.
# The Thrust Factor measures the strength of the pattern once a breakout to either the upside or downside of the engulfed range occurs in real time.
# And of course which occurs first is the key to trade direction. That's another matter though.
# is the offset t pointing to the highest price in the group?
def isfirst_hi = if getvalue(high, -t_start_off) == engulfed_hi then 1 else 0;
# show a green or red bubble, on the first bar of engulfed group, that lists which comes first, a low or a high.
addchartbubble(line_start, gulf_lo*0.999,
getvalue(high, -t_start_off) + " t hi\n" +
#engulfed_hi + " ehi\n" +
(if isfirst_hi then "High" else "Low") + " is first"
, (if isfirst_hi then color.green else color.red), no);
# rule 4 questions
# you will have to define what a breakout is.
# there will be a highest price and a lowest price DURING a group of engulfed bars.
# but thos 2 points are within the engulfed price range. they didn't 'breakout'.
# there is only 1 breakout during a group of engulfed bars, on the bar after the last, big, engulfing bar.
# ( we could call the first bar, a breakin )
# 5. The Thrust Factor must be a minimum of 3 to qualify for a legit pattern per my definition. Otherwise it would be discarded from further qualifications for a trade, ie the Impedance Factor, which is irrelevant to the current discussion.
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" +
#t + "\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);
# 6. So for now, if I can determine the Thrust Factor, that part will be complete.
# 7 Your code shows the sizes of all engulfed ranges, which is good, and now only the highs and lows of those engulfed ranges need to be determined, with the bar number of the farther one back subtracted from the number of the engulfing bar to return the Thrust Factor.
# 8. It looks like the Thrust Factor of the first example on your chart with 21 total bars engulfed would be either 16 or 21, as it's hard to tell which of those two bars was higher. The second example with 7 total bars engulfed would be 6, with the high occurring on Bar 179 and the low on Bar 176 vs Bar 182 as the engulfing bar.
# 9. Both signals indicated taking Short positions.