Study Error while plotting HTF engulfing candles on LTF chart

Viewed 49

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))
2 Answers

You are requesting bar_index from other timeframes, and you use this as left and right parameters of the box.new(). The problem is, this difference might be quite a lot depending on the timeframe you are on.

For example, on BINANCE:BTCUSDT, 1h, current bar_index is 23426. barIndex and barIndexMinus2 are 11051 and 11049 respectively.

You see, if you use those variables as left and right arguments of your box.new() call, it will cause this issue. Because the distance between the current bar index and your variables are more than 500.

i solved it by using bar_time instead of bar_index.

Related