Search code examples
customizationpine-scriptohlc

How to Draw Previous High and Low on Custom Instrument precisely?


Good day, i have done a indicator which marks the high and the low of the previous day.

It works well but on custom instrument such as : USDEUR * USDGBP * USDJPY * USDCHF * USDAUD * USDNZD * USDCAD

And, sometimes, it doesn't fit perfectly.

enter image description here

How can I do it properly please ?

//@version=4
study("Prev H&L", overlay=true)
security_1 = security(syminfo.tickerid, '1440', high[1],  lookahead=barmerge.lookahead_on)
security_2 = security(syminfo.tickerid, '1440', low[1],   lookahead=barmerge.lookahead_on)
plot(timeframe.isintraday ? security_1 : na, title="Yhigh",  trackprice=true, offset=-99999, color=#a5d6a7, linewidth=2)
plot(timeframe.isintraday ? security_2 : na, title="Ylow",   trackprice=true, offset=-99999, color=#a5d6a7, linewidth=2)

Regards.


Solution

  • A spread is built from lower resolution, so just requesting another resolution via security is not suitable here.

    You should use simple searching of the high through whole previous day:

    //@version=4
    study("Prev H&L", overlay=true)
    
    dailyTime = security(syminfo.tickerid, "D", time, lookahead=true)
    prevDayEnd = barssince(dailyTime != dailyTime[1]) + 1
    
    prevHigh = high[prevDayEnd]
    prevLow = low[prevDayEnd]
    for i = prevDayEnd + 1 to 10000
        if nz(dailyTime[i]) != nz(dailyTime[prevDayEnd])
            break
    
        if high[i] > prevHigh
            prevHigh := high[i]
    
        if low[i] < prevLow
            prevLow := low[i]
    
    plot(timeframe.isintraday ? prevHigh : na, title="Yhigh", offset=-24, color=#a5d6a7, linewidth=2)
    plot(timeframe.isintraday ? prevLow : na, title="Ylow",   offset=-24, color=#a5d6a7, linewidth=2)