How would you determine-express-code the number of bars back any price level was within a bar's range or price match? The 2nd most recent such match? 3rd most recent etc?
this will calculate a number, that is an offset , to the desired bar.
on every bar, look back at previous bars,
and find the xth bar that is within some price range,
based on the current bar.
prev_cnt , is equal to the count, the offset
pick which multiple to look for.
default is 3, find the offset, to the 3rd bar, that is within a range.
can pick the range type,
. within_high_low
. percent_of_close, above and below
. within_dollars_of_close, above and below
qty_bubbles , can turn on bubbles that show the count for each bar.
test_rng_lines , testing,
last_bar_offset , can choose an offset number, to define a bar before the last bar.
can show 2 horizontal lines, drawn from a chosen bar, to visually verify the bars are in the range.
Ruby:
# recent_price_match_00
# https://usethinkscript.com/threads/determining-the-most-recent-price-match.12784/#post-108919
# find the number of bars back,
# any price level was within a bar's range or price match? The 2nd most recent such match? 3rd most recent etc?
#-------------------------------
# ref this study
# https://usethinkscript.com/threads/find-engulfing-bars-while-allowing-x-number-of-breakouts-in-the-series-for-thinkorswim.12792/
# engulfing , with x breakouts
#-------------------------------
def bn = BarNumber();
def na = Double.NaN;
#def lastbn = highestall(if(!isnan(close), bn, 0));
input match_quantity = 3;
input match_type = { default within_high_low , percent_of_close, within_dollars_of_close };
input percent_of_close = 0.1;
def cls_per = close * (percent_of_close/100);
input within_dollars_of_close = 0.02;
input lines_from_lastbar = yes;
# ---------------------
def rng_hi;
def rng_lo;
def mtype;
switch (match_type) {
case within_high_low:
rng_hi = high;
rng_lo = low;
mtype = 1;
case percent_of_close:
rng_hi = close + (cls_per);
rng_lo = close - (cls_per);
mtype = 2;
case within_dollars_of_close:
rng_hi = close + (within_dollars_of_close);
rng_lo = close - (within_dollars_of_close);
mtype = 3;
}
def rng = rng_hi - rng_lo;
# ---------------------------
addlabel(1, "match_quantity: " + match_quantity , color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "match type: " + match_type , color.yellow);
addlabel(1, " ", color.black);
addlabel(mtype == 2, "cls_per: " + cls_per , color.yellow);
addlabel(1, " ", color.black);
addlabel(mtype == 3, "within_dollars_of_close " + within_dollars_of_close , color.yellow);
def maxnum = 200;
#addlabel(1, "max bars back " + maxnum, color.yellow);
def n = match_quantity;
def prev_cnt2 = fold j = 1 to maxnum
with p
while p <= n
do p + ( if p == n then j - (n+0) else
if (getvalue(rng_hi, j) >= low) and (getvalue(rng_lo, j) <= high) then 1 else 0 );
# this is the offset, back to the bar, that is the 'match_quantity'th price match
def prev_cnt = prev_cnt2-1;
#------------------------------------
#------------------------------------
# test stuff
# bubble with quantity of prev bars
input qty_bubbles = yes;
addchartbubble(qty_bubbles, low*0.999,
#bn + "\n" +
prev_cnt + "\n"
, Color.YELLOW, no);
#------------------------------------
# ref...
# 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 test_rng_lines = yes;
input last_bar_offset = 1;
def barsBack = 200;
def x = !IsNaN(close[-last_bar_offset]) and IsNaN(close[-(1+last_bar_offset)]);
def noline = IsNaN(close[-(0+last_bar_offset)]);
# hi
def c = if x
then rng_hi
else c[1];
def d = if test_rng_lines and isNaN(close[-barsBack])
then c[-barsBack]
else Double.NaN;
plot line = if noline then na else d;
line.SetLineWeight(1);
line.SetDefaultColor(Color.LIME);
line.SetStyle(Curve.MEDIUM_DASH);
# lo
def c2 = if x
then rng_lo
else c2[1];
def d2 = if test_rng_lines and isNaN(close[-barsBack])
then c2[-barsBack]
else Double.NaN;
plot line2 = if noline then na else d2;
line2.SetLineWeight(1);
line2.SetDefaultColor(Color.LIME);
line2.SetStyle(Curve.MEDIUM_DASH);
plot y = if test_rng_lines and x then high*1.0004 else na;
y.SetPaintingStrategy(PaintingStrategy.POINTS);
y.SetDefaultColor(Color.cyan);
y.setlineweight(3);
y.hidebubble();
plot off = if test_rng_lines and x then last_bar_offset else na;
off.SetPaintingStrategy(PaintingStrategy.values_above);
off.SetDefaultColor(Color.white);
off.hidebubble();
addverticalline(test_rng_lines and x, "-", color.magenta);
#
input last_bar_offset = , was set to some number, to choose a bar, before the last bar. (where the horizontal lines end)
this is looking for the 3rd bar, within a price range , from the current bar
the circled bubble has a number 9. 9 bars back is the 3rd bar that is within the price range