rryan21
rryan21

Reputation: 45

Marking bars with closes equal to specific bar's close

I have tried this many different ways, and this method with line.new and line.get is the closest I have gotten. If I can tweak this method that's great, or if there is a totally different way that's fine too. Thanks.

The goal is to compare the last 200 bar's close prices to the most recent close (not the real-time bar, but the most recent historical bar). I want to highlight any bars that have closes equal to the most recent close. In the attached picture you can see that the real-time bar is marked b/c its close currently meets criteria (but I don't care about that one), but there are several other bars that should be marked (see arrows I drew manually), because they have equal closes to the most recent close.

study("t1t2_020920", overlay=true)

CloseLineFunc() =>
    if (barstate.islast)
        closeline = line.new(bar_index[200],close[1],bar_index[1],close[1])
        closeline

CloseLineFunc()
a=CloseLineFunc()

x1 = line.get_x1(a)  // Returns UNIX time or bar index (depending on the last xloc value set) of the first point of the line
x2 = line.get_x2(a) // Returns UNIX time or bar index (depending on the last xloc value set) of the second point of the line
y1 = line.get_y1(a) // Returns price of the first point of the line
y2 = line.get_y2(a) // Returns price of the second point of the line

// plot(x1)
// plot(x2)
// plot(y1)
// plot(y2)


check = close == y2
plotchar(check, char='*', color=color.green, size=size.normal)

`` enter image description here

I also thought this would work, but I get na results. The tricky part is figuring how to use the value of the most recent close price, not including the realtime bar, in calculations to the left of the chart, then recalculating the study in that way each time a new bar is printed.

study("My Script", overlay=true)

lineprice = valuewhen(barstate.islast,close[1],0)

plot(lineprice)

check = sma(close,1) == lineprice
plotchar(check, char='*', color=color.green, size=size.normal)``

Upvotes: 1

Views: 296

Answers (1)

Michel_T.
Michel_T.

Reputation: 2821

I think, it should be someghing like that:

//@version=4
study("My Script", overlay=true)

MAX_LABELS = 40
if barstate.islast
    labersNum = 0
    for i = 1 to 4999
        if close[i] == close
            label.new(time[i], high[i], xloc=xloc.bar_time, yloc=yloc.abovebar, style=label.style_arrowdown)
            labersNum := labersNum + 1
            if labersNum >= MAX_LABELS
                break

The script should be tuned to support last non-realtime bars and changing the labels if there are new bars, but I thing this script is enough to start with.

Upvotes: 2

Related Questions