Search code examples
pine-scripttradingtradingview-apipine-script-v5

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?


Solution

  • 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)