nicktrader
nicktrader

Reputation: 1

How to plot a horizontal line based off a specific closing candle and timeframe

I am looking to plot a horizontal line in tradingview (pine script) based on a specific time and on a specific time frame. So for example I want to plot a hline on the close of the 5m candle at the 1600 (4pm EST) timeframe.

Ive tried a lot but cant seem to figure out how to a historic value within pine script. Meaning you are looking to plot a line from 4pm and have it show on your charts for the rest of that day and the beginning of the next day. It simple to do this with the function show_last = 1 within PS. but I cant figure out how to calculate the 5m closing candle at 4pm?

Some of my code that doesnt work fully

//@version=3
study("4pm_Line")

highTimeFrame = input("5", type = resolution)
sessSpec = input("1600-0930", type = session)

is_newbar(res, sess) =>
    t = time(res, sess)
    na(t[1]) and not na(t) or t[1] < t

newbar = is_newbar("5", sessSpec)
s2 = na
s2 := newbar ? close : nz(s2[1])

plot(s2, style=line, linewidth=1, color=lime, trackprice = true, 
     show_last = 1)

The line that plots is off and i have no idea how its getting its values.

Upvotes: 0

Views: 9027

Answers (1)

Lucem
Lucem

Reputation: 3072

its pretty easy actually. I created a script for you

//@version=4
//@author=lucemanb
study("Closing Time", overlay=true)

period  = input("5", "Period", input.resolution)
session = input("1500-1600", "Session", input.session)

float data = na
data := data[1]
getData() =>
    float d = na
    inSession = time(period, session)
    if not inSession and inSession[1]
        d := close[1]
    d
d = security(syminfo.tickerid, period, getData())
if not na(d)
    data := d

plot(data, "Line", color.yellow, 2, plot.style_line, true, show_last=1)

We basically store the value we found in a variable that we access on each candle I hope this helps. Enjoy

Upvotes: 2

Related Questions