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

Pine script code is Repaiting, can someone help me resolve it?


I am trying to calculate daily range and plot levels when market session opens. Code works fine but during day levels tend to move and eventually either higher level or lower level which was plotted in morning moves and come to either highest or lowest of day. I am using request.security function call and tried to avoid repainting but since source in function call is series float data I am not able to fix the script.

[day_adr, day_adr_high, day_adr_low] = request.security(syminfo.tickerid, 'D', adr_levels(daily_adr_length), barmerge.gaps_on, lookahead=barmerge.lookahead_on)

I tried to implement different ways noted in below link but not able to solve it.

https://www.tradingview.com/script/cyPWY96u-How-to-avoid-repainting-when-using-security-PineCoders-FAQ/


Solution

  • from pine reference: lookahead (simple barmerge_lookahead) On historical bars only, returns data from the timeframe before it elapses. Possible values: barmerge.lookahead_on, barmerge.lookahead_off. Has no effect on realtime values. Optional. The default is barmerge.lookahead_off starting from Pine Script™ v3. The default is barmerge.lookahead_on in v1 and v2. WARNING: Using barmerge.lookahead_on at timeframes higher than the chart's without offsetting the expression argument like in close[1] will introduce future leak in scripts, as the function will then return the close price before it is actually known in the current context. As is explained in the User Manual's page on Repainting this will produce misleading results.

    So use lookahead = barmerge.lookahead_off or change your expression to the previous index by adding [1]. Take all data from closed bars only and it will avoid repainting.