find engulfing bars, while allowing x number of breakouts in the series for ThinkOrSwim

halcyonguy

Expert
VIP
Lifetime
find engulfing bars, while allowing x number of breakouts in the series.


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

SNIfk1X.jpg
[/code]
 
Last edited:
find engulfing bars, while allowing x number of breakouts in the series.


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

SNIfk1X.jpg
[/code]
Great Job @halcyonguy ! i really like this system, very simple and good. a quick question, i noticed must of the time when is break always come to the middle of the box, do you think is possible to add a line in the middle of the box when break it? to enter in the pullback? thanks in advance
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
455 Online
Create Post

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