allow x quantity of false conditions to exist in a series of true's For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
allow x quantity of false conditions to exist in a series of true's

find a quantity of previous bars where,
the current close price,
crosses over a consecutive sequence of previous bars,
(current close < a prev high and current close > a prev low)
and is allowed to miss x bars.


Ruby:
# prev_price_crossings_x_skips_01

# allow x quantity of false conditions to exist in a series of true's

# find a quantity of previous bars where,
#  the current close price,
#  crosses over a consecutive sequence of previous bars,
#    (current close < a prev high and current close > a prev low)
#  and is allowed to miss x bars.


#-------------------------------
def bn = BarNumber();
def na = Double.NaN;

input allowed_misses = 3;
AddLabel(1, "allowed non crossings " + 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
#       if a condition is false then +1 , a miss
        if (close <= getvalue(high, j) and close >= getvalue(low, j))
        then 0 else 1 );


#------------------------------------
#------------------------------------
# test stuff

#------------------------------------
# draw a horz line at some close level, to visually look for prev bar crossings
# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
input show_line_at_last_close = yes;
input last_bar_offset = 5;
def barsBack = 1000;
def x = !IsNaN(close[-last_bar_offset]) and IsNaN(close[-(1+last_bar_offset)]);
#def c = if !IsNaN(close[-last_offset]) and IsNaN(close[-(1+last_offset)])
def c = if x
        then close
        else c[1];
plot line = if show_line_at_last_close and isNaN(close[-barsBack])
            then c[-barsBack]
            else Double.NaN;
line.SetLineWeight(1);
line.SetDefaultColor(Color.LIME);
line.SetStyle(Curve.MEDIUM_DASH);

plot y = if show_line_at_last_close and x then high*1.0004 else na;
y.SetPaintingStrategy(PaintingStrategy.POINTS);
y.SetDefaultColor(Color.cyan);
y.setlineweight(3);
y.hidebubble();


input test3_vertical_lines = yes;
addverticalline(test3_vertical_lines and x, "-", color.magenta);


#------------------------------------
# bubble with quantity of prev bars, that have true conditions, but only up to x false bars
def min_qty = 2;
input test2 = yes;
addchartbubble(test2 and (prev_cnt >= min_qty), low*0.999,
#bn + "\n" +
prev_cnt + "\n"
, Color.YELLOW, no);
#------------------------------------
#
#


every bar has a quantity in a bubble, of how many previous bars.

for verifying, can choose an offset, from the last bar
draws a blue dot above that bar, and a purple vertical line is before it.
the yellow bubble under the bar, shows 8, 8 bars before this one that meet the conditions
follow the lime line, and count the bars that cross it , and count 3 more bars that don't cross it.
NFyI68s.jpg



i started with this, a conversion of the pine function , valuewhen()
https://usethinkscript.com/threads/...tos-script-not-pine-studies.11424/#post-99310
 
Last edited:

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

Thread starter Similar threads Forum Replies Date
F False Breakout (Expo) For ThinkOrSwim Custom 3

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
254 Online
Create Post

Similar threads

Similar threads

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