Search code examples
pine-scriptpine-script-v5

Study Error while plotting HTF engulfing candles on LTF chart


I am getting a study error:

Objects positioned using xloc.bar_index cannot be drawn further than 500 bars into the future.

I have no idea where to even begin to fix this. Some help from experts here would be very helpful.

indicator('MTF Engulfing Candles', overlay=true, max_boxes_count=500, max_lines_count=500)

tf = input.timeframe("240", "Timeframe", options=["60","240","480","720","W","M"])

O1 = request.security(syminfo.tickerid, tf, open[1])
H1 = request.security(syminfo.tickerid, tf, high[1])        
L1 = request.security(syminfo.tickerid, tf, low[1])          
C1 = request.security(syminfo.tickerid, tf, close[1])

O2 = request.security(syminfo.tickerid, tf, open[2])
H2 = request.security(syminfo.tickerid, tf, high[2])        
L2 = request.security(syminfo.tickerid, tf, low[2])          
C2 = request.security(syminfo.tickerid, tf, close[2])

barIndex = request.security(syminfo.tickerid, tf, bar_index)
barIndexMinus2 = request.security(syminfo.tickerid, tf, bar_index-2)

// FUNCTIONS
bullE()=>
    r = C2 < O2 and C1 > O1 and C1 > H2 ? 1 : 0
    
bearE()=>
    r = C2 > O2 and C1 < O1 and C1 < L2 ? 1 : 0

if bullE()
    box.new(left=barIndexMinus2, right=barIndex, top=H1 ,bottom=math.min(L2,L1), border_color = color.new(color.black,65), bgcolor = color.new(#66bb6a,75))
    
if bearE()
    box.new(left=barIndexMinus2, right=barIndex, top=math.max(H2,H1) ,bottom=L2, border_color = color.new(color.black,65), bgcolor = color.new(#ffa726, 75))

Solution

  • I solved it by using bar_time instead of bar_index.