To resolve the "objects positioned xloc.bar_index cannot be drawn 500 bars in the future" error, you need to adjust how the script interprets timestamps when drawing boxes. The issue arises because Pine Script treats the left and right parameters of box.new() as bar indices by default (xloc.bar_index). When you pass Unix timestamps (e.g., higherTimeOpen, higherTimeClose) directly, Pine Script tries to interpret them as bar indices, which leads to invalid future bar numbers (e.g., 1698768000 milliseconds = ~19 million seconds = ~19 million bars ahead).
✅ Solution: Use xloc.bar_time for Time-Based Coordinates
To fix this, explicitly tell Pine Script to treat left and right as timestamps by setting the xloc parameter to xloc.bar_time in the box.new() function. This ensures that Pine Script correctly maps the box to the appropriate time range, not bar indices.
🛠 Updated Code Fix
Here’s the corrected version of your script with the necessary changes:
//@version=6
indicator("Candle Breach Indicator", overlay=true, max_boxes_count=400)
// Input for higher timeframe and max boxes
higherTF = input.timeframe("60", "Higher Timeframe")
maxBoxes = input.int(50, "Max Boxes to Display", minval=1, maxval=400)
// Request higher timeframe data with lookahead_on for accurate current HTF info
higherOpen = request.security(syminfo.tickerid, higherTF, open, barmerge.lookahead_on)
higherHigh = request.security(syminfo.tickerid, higherTF, high, barmerge.lookahead_on)
higherLow = request.security(syminfo.tickerid, higherTF, low, barmerge.lookahead_on)
higherClose = request.security(syminfo.tickerid, higherTF, close, barmerge.lookahead_on)
higherTimeOpen = request.security(syminfo.tickerid, higherTF, time, barmerge.lookahead_on)
higherTimeClose = request.security(syminfo.tickerid, higherTF, time_close, barmerge.lookahead_on)
// Calculate the condition on the higher timeframe
prevHigh = request.security(syminfo.tickerid, higherTF, high[1], barmerge.lookahead_on)
prevLow = request.security(syminfo.tickerid, higherTF, low[1], barmerge.lookahead_on)
currentHigh = higherHigh
currentLow = higherLow
currentClose = higherClose
breachHigh = currentHigh > prevHigh
breachLow = currentLow < prevLow
closeWithinRange = currentClose <= prevHigh and currentClose >= prevLow
condition = (breachHigh or breachLow) and closeWithinRange
// Array to manage boxes
var box[] boxes = array.new_box(0)
var int lastProcessedTime = na
// Check for new HTF bar and condition
if condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime)
// Create box using xloc.bar_time to interpret left/right as timestamps
newBox = box.new(
left=higherTimeOpen,
right=higherTimeClose,
top=higherHigh,
bottom=higherLow,
xloc=xloc.bar_time, // Critical fix: interpret left/right as time
border_color=color.red,
border_width=2,
bgcolor=color.new(color.red, 90)
)
array.push(boxes, newBox)
// Limit box count
if array.size(boxes) > maxBoxes
oldestBox = array.shift(boxes)
box.delete(oldestBox)
lastProcessedTime := higherTimeOpen