Search code examples
pine-scriptpine-script-v5tradingpine-script-v4

Repainting issue with barmerge.lookahead_on in security


I tried to extract closing data from higher timeframe. But It is repainting when I add the lookahead = barmerge.lookahead_on. It is not repainting when the lookahead is not entered. Not sure what's wrong with it.

I tried both barstate.isrealtime and barstate.isconfirmed. But both repaints.

f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0], lookahead = barmerge.lookahead_on) 
Htfclose = f_secureSecurity(syminfo.tickerid, '60', close)

and

f_secure(_symbol, _res, _src) => request.security(_symbol, _res, _src[barstate.isconfirmed ? 0 : 1], lookahead = barmerge.lookahead_on)
Htfclose_1 = f_secure(syminfo.tickerid, '60', close)

and also this

f_security(_symbol, _res, _src, _repaint) => request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0], lookahead = barmerge.lookahead_on)
Htfclose_2 = f_security(syminfo.tickerid, '60', close, false)

And I know it will not repaint if I use _src[1].

What would be the possible way to make it non-repaint with lookahead = barmerge.lookahead_on and _src[barstate.isconfirmed ? 0 : 1] or _src[barstate.isrealtime ? 1 : 0]?


Solution

  • You need to look at the last closed HTF bar when requesting data from higher timeframes if you don't want any repainting.

    You can use barstate.isconfirmed would refer to your chart's timeframe so you cannot use that to avoid repainting.

    You can use barstate.isrealtime but you need an additional check at the end of your function call.

    See below:

    f_security(_symbol, _res, _src, _repaint) => request.security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
    

    Please note that there are two barstate.isrealtime checks in the above code.

    More readings: