Pamba
Pamba

Reputation: 822

Previous Day High and Low using Pine Script v5

I am trying to convert a pinescript from v1 to v5.

v1

study(title="Previous Day High and Low", shorttitle="Previous Day High and Low", overlay=true)
D_High = security(tickerid, 'D', high[1]) 
D_Low = security(tickerid, 'D', low[1]) 
D_Close =  security(tickerid, 'D', close[1]) 
D_Open =  security(tickerid, 'D', open[1]) 

plot(isintraday ? D_High : na, title="Daily High",style=line, color=blue,linewidth=1) 
plot(isintraday ? D_Low : na, title="Daily Low",style=line, color=blue,linewidth=1) 

Output: enter image description here

v1 is working fine.

I am trying to convert v5

//@version=5
indicator(title="Previous Day High and Low New", shorttitle="Previous Day High and Low New", overlay=true)

D_High = request.security(syminfo.tickerid, 'D', high[1]) 
D_Low = request.security(syminfo.tickerid, 'D', low[1]) 

plot(timeframe.isintraday ? D_High : na, title="Daily High", color=color.green,linewidth=2)
plot(timeframe.isintraday ? D_Low : na, title="Daily Low", color=color.red,linewidth=2) 

Output: enter image description here

But its showing wrong. Any Idea?

Upvotes: 1

Views: 7022

Answers (3)

Arun_K_Bhaskar
Arun_K_Bhaskar

Reputation: 51

Code:

//@version=5
indicator(title='PDHL', shorttitle='PDHL', overlay=true)

// Get OHLC Data
i_timeframe ='D'
i_past = 1
[high_price, low_price]  = request.security(symbol=syminfo.tickerid, timeframe=i_timeframe, expression=[high[i_past], low[i_past]], lookahead=barmerge.lookahead_on)

// Hide Historical Plots
i_showlast = false
htf_islast = i_showlast ? request.security(symbol=syminfo.tickerid, timeframe=i_timeframe, expression=barstate.islast, lookahead=barmerge.lookahead_on) : true

// Plot
high_break = high_price == high_price[1]
low_break = low_price == low_price[1]

plot(series=htf_islast and high_break ? high_price : na, title='High', color=#FF00ff, linewidth=2, style=plot.style_steplinebr, offset=-1)
plot(series=htf_islast and low_break ? low_price : na, title='Low', color=#00FFFF, linewidth=2, style=plot.style_steplinebr, offset=-1)

Result: enter image description here

Upvotes: 2

waldo
waldo

Reputation: 1

The logic was wrong because it was plotting the 2 days back on a new day. Check out this version:

//@version=5
indicator(title="Previous Day High and Low New", shorttitle="Previous Day High and Low New", overlay=true)

D_High = request.security(syminfo.tickerid, 'D', high) 
D_Low = request.security(syminfo.tickerid, 'D', low) 

plot(timeframe.isintraday ? D_High : na, title="Daily High", color=color.green,linewidth=2)
plot(timeframe.isintraday ? D_Low : na, title="Daily Low", color=color.red,linewidth=2)

Upvotes: 0

e2e4
e2e4

Reputation: 3828

Pine v1-v2's security() function is using the lookahead parameter by default, which could be modified in v3-v5 with the lookahead= argument. To match the result declare the barmerge.lookahead_on:

//@version=5
indicator(title="Previous Day High and Low New", shorttitle="Previous Day High and Low New", overlay=true)

D_High = request.security(syminfo.tickerid, 'D', high[1], lookahead = barmerge.lookahead_on) 
D_Low = request.security(syminfo.tickerid, 'D', low[1], lookahead = barmerge.lookahead_on) 

plot(timeframe.isintraday ? D_High : na, title="Daily High", color=color.green,linewidth=2)
plot(timeframe.isintraday ? D_Low : na, title="Daily Low", color=color.red,linewidth=2) 

Upvotes: 4

Related Questions