this isn't exactly as requested,
i thought an exact match would be very rare, so i made this with 2 tolerance lavels, above and below the last price.
this finds the price of the last bar.
pick a % tolerance, to create upper and lower levels, around the last price. (default 1.5%)
check all the bars, to find when/if price crosses down , through the tolerance levels (cross down). find the most recent occurance.
looks for a bar price that is within the tolerance levels (double top). find most recent occurance.
draws 3 gray lines for double top lines, 2 tolerance and 1 price level.
draws 2 cyan lines when a cross down happens
can show dots above bars when the cross down happens and the double top bar.
i didn't check if no valid data. so long lines might be visible sometimes.
Code:
#same_high_line_00
# test - EMR month chart
#https://usethinkscript.com/threads/help-making-double-top-with-last-bar-only-indicator.16253/
#Help making double top with last bar only indicator
#1. The line should only appear when the double top (DT) occurs with the last bar on the chart. As soon as the next bar prints and it is no longer a DT with a prior bar, the line should disappear.
#2. I'm only looking for EXACT DTs (high == high).
#3. I only want DTs that occur "in the clear". What I mean is, if you trace the high of the current bar back to the left, as soon as you encounter prices that engulf that level it should no longer search for DTs to draw a line.
def na = double.nan;
def bn = barnumber();
# find last bar
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
# get price from last bar, to match to a prev bar
input price = high;
# set the current price to a var, across all bars
def last_price = HighestAll(if lastbn == bn then price else 0);
input price_tol_percent = 1.5;
def tolhi = last_price * (1 + (price_tol_percent/100));
def tollo = last_price * (1 - (price_tol_percent/100));
addlabel(1, price_tol_percent + " % for current bar price", color.yellow);
addlabel(1, tolhi + " upper tolerance level", color.cyan);
addlabel(1, tollo + " lower tolerance level", color.cyan);
# look for a bar thet crosses down over the tolerance levels
def crossdownbn = if bn == 1 then 0
# else if (high > tolhi and low < tollo) or (high[1] > tolhi and low < tollo) then bn
else if (high[1] > tolhi and low < tollo) then bn
else crossdownbn[1];
# find bn of most recent cross down
def crossdownrecent = highestall(crossdownbn);
# define a group of bars to draw lines for cross down bar
def crosslines = if bn >= (crossdownrecent - 1) and bn <= (crossdownrecent + 0) then 1 else 0;
# look for a match to last bar price
# find price within tol levels
def matchbn = if crossdownrecent > 0 and bn != lastbn and bn > crossdownrecent and (price < tolhi and price > tollo) then bn else 0;
def matchbnhi = highestall(matchbn);
# define a group of bars for double top to draw lines
def dbllines = if bn >= matchbnhi and bn <= lastbn then 1 else 0;
# draw line from current bar price
input show_tol_lines = yes;
plot zt0 = if show_tol_lines and dbllines then last_price else na;
zt0.SetDefaultColor(Color.light_gray);
zt0.hidebubble();
# tolerance lines
plot zt1 = if show_tol_lines and dbllines then tolhi else na;
zt1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zt1.SetDefaultColor(Color.gray);
zt1.hidebubble();
plot zt2 = if show_tol_lines and dbllines then tollo else na;
zt2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zt2.SetDefaultColor(Color.gray);
zt2.hidebubble();
# cross down lines , when bars cross down, over the tolerance levels
input show_cross_down_lines = yes;
plot xt1 = if show_cross_down_lines and crosslines then tolhi else na;
xt1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xt1.SetDefaultColor(Color.cyan);
xt1.setlineweight(2);
xt1.hidebubble();
plot xt2 = if show_cross_down_lines and crosslines then tollo else na;
xt2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xt2.SetDefaultColor(Color.cyan);
xt2.setlineweight(2);
xt2.hidebubble();
input show_cross_down_dot = yes;
plot zw1 = if show_cross_down_dot and bn == crossdownrecent then high * 1.004 else na;
zw1.SetPaintingStrategy(PaintingStrategy.POINTS);
zw1.SetDefaultColor(Color.cyan);
zw1.setlineweight(3);
zw1.hidebubble();
input show_match_dot = yes;
plot zw2 = if show_match_dot and bn == matchbnhi then high * 1.004 else na;
zw2.SetPaintingStrategy(PaintingStrategy.POINTS);
zw2.SetDefaultColor(Color.yellow);
zw2.setlineweight(3);
zw2.hidebubble();
#------------------
# test stuff
input test1 = no;
addchartbubble(test1, low*0.999,
bn + " bn\n" +
lastbn + " Lbn\n" +
last_price + " last"
, color.yellow, no);
input test2_crossdown = no;
addchartbubble(test2_crossdown and (bn == crossdownbn), low,
crossdownbn + "\n" +
crossdownrecent
, color.cyan, no);
input test3_match = no;
addchartbubble(test3_match and (bn == matchbn), low,
matchbn + "\n" +
matchbnhi
, color.yellow, no);
#
EMR - month
shows test bubbles, on cross down bars and double top bars
bubbles off