but i am still facing same error
- User
but i am still facing same error
- Assistant
Thanks for the update! The error "objects positioned xloc.bar_index cannot be drawn 500 bars in the future" usually happens when you use
bar_indexor a value interpreted asbar_indexthat points to a future bar relative to the current chart's bar.
Why is this happening?
Your code uses:
box.new( left=higherTimeOpen, right=higherTimeClose, ... )But
box.new()expectsleftandrightto be either:- bar indices (if
xloc.bar_indexis used, which is the default), or - timestamps (if
xloc.bar_timeis specified).
Since
higherTimeOpenandhigherTimeCloseare timestamps, but you didn't specifyxloc.bar_time, Pine Script treats them as bar indices, which causes the error.
The fix: specify
xloc.bar_timeinbox.new()You need to tell Pine Script explicitly that your
leftandrightare timestamps, not bar indices.
Corrected code snippet:
newBox = box.new( left = higherTimeOpen, right = higherTimeClose, top = higherHigh, bottom = higherLow, border_color = color.red, border_width = 2, bgcolor = color.new(color.red, 90), xloc = xloc.bar_time // <--- THIS IS THE CRUCIAL PART )
Full corrected script with this fix:
//@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 higherOpen = request.security(syminfo.tickerid, higherTF, open) higherHigh = request.security(syminfo.tickerid, higherTF, high) higherLow = request.security(syminfo.tickerid, higherTF, low) higherClose = request.security(syminfo.tickerid, higherTF, close) higherTimeOpen = request.security(syminfo.tickerid, higherTF, time) higherTimeClose = request.security(syminfo.tickerid, higherTF, time_close) // Calculate the condition on the higher timeframe prevHigh = request.security(syminfo.tickerid, higherTF, high[1]) prevLow = request.security(syminfo.tickerid, higherTF, low[1]) 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 store box IDs for management var box[] boxes = array.new_box(0) // Keep track of the last processed higher timeframe bar's open time var float lastProcessedTime = na // Check if the current higher timeframe bar meets the condition and hasn't been processed yet if condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime) // Create new box with xloc.bar_time to use timestamps for left/right newBox = box.new( left = higherTimeOpen, right = higherTimeClose, top = higherHigh, bottom = higherLow, border_color = color.red, border_width = 2, bgcolor = color.new(color.red, 90), xloc = xloc.bar_time // <--- Important fix here ) // Add the new box to our array array.push(boxes, newBox) // If we exceed the maximum number of boxes, delete the oldest one if array.size(boxes) > maxBoxes oldestBox = array.get(boxes, 0) box.delete(oldestBox) array.remove(boxes, 0) lastProcessedTime := higherTimeOpen
Summary:
- Always specify
xloc = xloc.bar_timewhen using timestamps forleftandrightinbox.new(). - This tells Pine Script to interpret those values as timestamps, not bar indices.
- This will prevent the "cannot draw 500 bars in the future" error.
Try this updated script and let me know if the error persists!
- bar indices (if
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.