Search code examples
pine-scriptpine-script-v5tradingview-api

TradingView Pine script - getting signals one day late


//@version=5
indicator(title="CDsGam", shorttitle="CDss", overlay=true)
plot=close

// Calculate Average Daily Range (ADR)
OPENN = request.security(syminfo.tickerid, 'D', open)
dayrange = high - low

r1 = request.security(syminfo.tickerid, 'D', dayrange[1])
r2 = request.security(syminfo.tickerid, 'D', dayrange[2])
r3 = request.security(syminfo.tickerid, 'D', dayrange[3])
r4 = request.security(syminfo.tickerid, 'D', dayrange[4])
r5 = request.security(syminfo.tickerid, 'D', dayrange[5])
r6 = request.security(syminfo.tickerid, 'D', dayrange[6])
r7 = request.security(syminfo.tickerid, 'D', dayrange[7])
r8 = request.security(syminfo.tickerid, 'D', dayrange[8])
r9 = request.security(syminfo.tickerid, 'D', dayrange[9])
r10 = request.security(syminfo.tickerid, 'D', dayrange[10])

adr_10 = (r1+r2+r3+r4+r5+r6+r7+r8+r9+r10) /10
adr_9 = (r1+r2+r3+r4+r5+r6+r7+r8+r9) /9
adr_8 = (r1+r2+r3+r4+r5+r6+r7+r8) /8
adr_7 = (r1+r2+r3+r4+r5+r6+r7) /7
adr_6 = (r1+r2+r3+r4+r5+r6) /6
adr_5 = (r1+r2+r3+r4+r5) /5
adr_4 = (r1+r2+r3+r4) /4
adr_3 = (r1+r2+r3) /3
adr_2= (r1+r2)/2
adr_1 = r1

adr_high_10 = OPENN + (adr_10/2)
adr_low_10 = OPENN - (adr_10/2)

adr_high_5 = OPENN + (adr_5/2)
adr_low_5 =  OPENN - (adr_5/2)

p1 = plot(adr_high_10, title="ADR High10", style = plot.style_circles, color=color.red, linewidth=2)
p2 = plot(adr_low_10, title="ADR LOW10", style = plot.style_circles, color=color.green, linewidth=2)
p3 = plot(adr_high_5, title="ADR High5", style = plot.style_circles, color=color.red, linewidth=2)
p4 = plot(adr_low_5, title="ADR LOW5", style = plot.style_circles, color=color.green, linewidth=2)

fill(p1, p3, color = color.new(color.red, 90))
fill(p2, p4, color = color.new(color.green, 90))

I copied an ADR indicator and converted to v5 from v4. But now I am getting the data 1 day delayed

Need to get this data on current day itself, rather than 1 day delayed

enter image description here


Solution

  • You had several problems with this code.
    For example, you were using [] to access historical data inside request.security(),
    but didn't used barmerge.lookahead_on, which leads to unpredictable results.
    You should read about Repainting, and then about Future Leaks.

    This should be what you trying to archive...

    //@version=5
    indicator(title="Average Daily Maximum Price Fluctuation", shorttitle="AVG MPF", overlay=true)
        
    var open_today = float(na)
    
    if session.isfirstbar
        open_today := open
    
    [avg_mpf5, avg_mpf10]  = request.security(syminfo.tickerid, "1D", [math.sum(high - low, 5)[1] / 5, math.sum(high - low, 10)[1] / 10], lookahead = barmerge.lookahead_on    
        
    float band_upper10 = avg_mpf10 / 2 + open_today
    float band_lower10 = open_today - (avg_mpf10 / 2)
        
    float band_upper5  = avg_mpf5  / 2 + open_today
    float band_lower5  = open_today - (avg_mpf5  / 2)
        
    p1 = plot(band_upper10, title="MPF AVG10 Upper Band", style = plot.style_circles, color=color.red  , linewidth=2)
    p2 = plot(band_lower10, title="MPF AVG10 Lower Band", style = plot.style_circles, color=color.green, linewidth=2)
    p3 = plot(band_upper5 , title="MPF AVG5 Upper Band" , style = plot.style_circles, color=color.red  , linewidth=2)
    p4 = plot(band_lower5 , title="MPF AVG5 Lower Band" , style = plot.style_circles, color=color.green, linewidth=2)
        
    fill(p1, p3, color = color.new(color.red, 90))
    fill(p2, p4, color = color.new(color.green, 90))
    

    I removed the problem by moving the calculations inside the function
    while using [] and barmerge.lookahead_on.
    And I used a tuple to get those values.

    As you may notice, I changed the name into Maximum Price Fluctuation,
    cause this wasn't an Average Daily Range at all.


    An Average Daily Range should look some like this...

    //@version=5
    indicator(title="Average Daily Range", shorttitle="ADR", overlay=true)
    
    var open_today = float(na)
    
    if session.isfirstbar
        open_today := open
    
    [adr5, adr10]  = request.security(syminfo.tickerid, "1D", [ta.atr(5)[1], ta.atr(10)[1]], lookahead = barmerge.lookahead_on)
    
    float band_upper10 = open_today + adr10
    float band_lower10 = open_today - adr10
    
    float band_upper5  = open_today + adr5
    float band_lower5  = open_today - adr5
    
    p1 = plot(band_upper10, title="ADR10 Upper Band", style = plot.style_circles, color=color.red  , linewidth=2)
    p2 = plot(band_lower10, title="ADR10 Lower Band", style = plot.style_circles, color=color.green, linewidth=2)
    p3 = plot(band_upper5 , title="ADR5 Upper Band" , style = plot.style_circles, color=color.red  , linewidth=2)
    p4 = plot(band_lower5 , title="ADR5 Lower Band" , style = plot.style_circles, color=color.green, linewidth=2)
    
    fill(p1, p3, color = color.new(color.red, 90))
    fill(p2, p4, color = color.new(color.green, 90))
    

    Without [1] and barmerge.lookahead_on both examples will work too,
    but live created lines would move and cause repainting.
    On the other hand, this can be wanted.

    In other words:
    Without [1] and lookahead_on you accessing live data,
    while with both active you will access historical data only.