Jaffer Sheriff
Jaffer Sheriff

Reputation: 1564

How to get current day's high, low, open price for a script in pine script?

Hi I'm pretty new to pine script. So sorry if I'm missing something very obvious. I'm trying to plot current day's open, high , low and close on a given script . For that I'm using below code.

strategy("Intraday Test Strategy", overlay=true)


dh = security(syminfo.tickerid,"D",high)
do = security(syminfo.tickerid,"D",open)
dl = security(syminfo.tickerid,"D",low)

plot(dh, title="High",color=color.red,linewidth=2,trackprice=true)
plot(do, title="Open",color=color.yellow,linewidth=2,trackprice=true)
plot(dl, title="Low",color=color.green,linewidth=2,trackprice=true)

When I execute this all I see is previous day's high, Open, Low prices and not current day's high, Open, Low prices. Its clear I'm missing something very basic. I would appreciate very much if u clarify what I'm missing here.

SPY

As we can from above pic the previous day's open, low and high prices are plotted where as i need current day's values.

Upvotes: 2

Views: 14866

Answers (2)

Tim
Tim

Reputation: 1

With this one you can see how the HOD and LOD move with the plot.

indicator("OHL OD Chart", "OHL", overlay = true)


FirstCandel = if session.isfirstbar
    0
else
    ta.barssince(session.isfirstbar)
OPEN = if session.isfirstbar
    //line.delete(NextOpenLine[3])
    open
HOD = if FirstCandel < 1
    high
else
    OpenHOD_CANDEL = ta.barssince(session.isfirstbar)
    ta.highest(high, FirstCandel)
LOD = if FirstCandel < 1
    low
else
    OpenHOD_CANDEL = ta.barssince(session.isfirstbar)
    ta.lowest(low, FirstCandel)
COD = if session.islastbar
    close
plot(HOD, "HOD", color.red, linewidth = 1, trackprice = true)
plot(LOD, "LOD", color.green, linewidth = 1, trackprice = true)
plot(OPEN, "OPEN", color.yellow, linewidth = 1, trackprice = true)
plot(COD,"CLOSE", color.blue, linewidth = 1, trackprice = true)

Upvotes: 0

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

Just like in the real world, Pine cannot look into the future.
So, when you're at the start of a day, there's no way to know what the high and low will be.

However, Pine does allow to look ahead on historical bars, because on those bars, the high and low points for that day are known.

//@version=4
study("Test", "Test", true)

[do,dh,dl] = security(syminfo.ticker, "D", [open,high,low], lookahead=barmerge.lookahead_on)

plot(dh, title="High", color=color.red,    linewidth=2, trackprice=true)
plot(do, title="Open", color=color.yellow, linewidth=2, trackprice=true)
plot(dl, title="Low",  color=color.green,  linewidth=2, trackprice=true)

You must be careful though, because the lookahead values will only be correct for days in the past.

Upvotes: 11

Related Questions