That's a common issue in Pine Script, and it's due to a built-in limit imposed by TradingView to prevent excessive resource usage. By default, Pine Script allows a maximum of 500 box.new() or label.new() objects to be drawn on the chart.
The solution from Magistral Medium uses box.new() to draw rectangles, which is great for visualization, but it can quickly exceed the 500-object limit when dealing with historical data.
Here are a few workarounds to overcome this limitation:
1. Limit the number of boxes drawn
You can add a counter to limit the number of boxes drawn to 500. This way, older boxes will not be drawn, but you'll still get the idea of the pattern.
Add these lines before the box.new() call:
pinescript
var int boxCount = 0
if boxCount < 500
box.new(... // your box.new() code here
boxCount := boxCount + 1
This will stop drawing boxes after 500 have been created.
2. Use barstate.islast to only draw boxes for recent bars
Instead of drawing boxes for all historical bars, only draw them for the last N bars (e.g., 500). This way, you'll only see recent patterns.
pinescript
if barstate.islast and condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime)
box.new(... // your box.new() code here
lastProcessedTime := higherTimeOpen
barstate.islast is true only for the last bar on the chart. This limits the drawing to the most recent bars.
3. Delete older boxes using box.delete()
This approach is more advanced. You'll keep track of all created boxes and delete the oldest ones when the limit is reached.
Add these lines at the top:
pinescript
var box[] boxes = array.new_box()
var int maxBoxes = 500
Then, modify the box.new() section:
pinescript
if condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime)
if array.size(boxes) >= maxBoxes
oldestBox = array.shift(boxes)
box.delete(oldestBox)
newBox = box.new(
left=higherTimeOpen,
right=higherTimeClose,
top=higherHigh,
bottom=higherLow,
border_color=color.red,
border_width=2,
bgcolor=color.new(color.red, 90)
)
array.push(boxes, newBox)
lastProcessedTime := higherTimeOpen
Here, we:
Create an array boxes to store all box objects.
Check if the array has reached the maxBoxes limit (500).
If yes, delete the oldest box using array.shift() and box.delete().
Create a new box and add it to the array using array.push().
4. Plot shapes instead of boxes ( simplest solution)
If you don't need the exact rectangle shape, you can plot a shape (e.g., label or plotshape) on the candle that meets the condition. This won't hit the 500-object limit.
Replace the box.new() section with:
pinescript
if condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime)
label.new(
x=higherTimeOpen,
y=higherHigh,
text="B",
color=color.red,
style=label.style_label_down
)
lastProcessedTime := higherTimeOpen
This will plot a small red "B" label above the candle that breaches the previous candle's high/low and closes within its range.
Choose the workaround that best fits your needs. If you're okay with a simpler visualization, go with 4. If you need the exact box shape, try 1, 2, or 3.
Here's the full modified code for 3. Delete older boxes: