Okay, I understand. The "cannot draw more than 500 bars" error is a common limitation in TradingView scripts. It means your script is trying to draw more than 500 objects (like boxes, labels, or lines) on the chart simultaneously.
The original script draws a new box every time a higher timeframe (HTF) candle meets your criteria. If you scroll back in history or load a chart with many historical bars that satisfy the condition, you quickly exceed the 500-object limit.
The solution is to implement a mechanism that limits the number of boxes drawn by deleting the oldest ones as new ones are created.
Here's the revised Pine Script v6 indicator that includes this box management:
//@version=6
// Declare the indicator with an explicit max_boxes_count for safety,
// though our script's logic will enforce a more controlled limit via an input.
indicator("Candle Breach Highlighter", overlay=true, max_boxes_count = 500)
// --- Inputs ---
// Allows the user to select the Higher Timeframe (e.g., "1D", "4H", "1W")
higherTF = input.timeframe("D", "Higher Timeframe")
// Allows the user to set how many recent boxes they want to see.
// This is the primary control to prevent the 500-object error.
maxBoxes = input.int(50, "Max Boxes to Display", minval=1, maxval=500)
// Color for the highlighting rectangles.
highlightColor = input.color(color.red, "Highlight Color")
// --- Data Fetching ---
// Get the ticker ID for the current chart's symbol.
var string ticker = syminfo.tickerid
// Request data from the higher timeframe.
// `barmerge.lookahead_on` is crucial here. It ensures that when you're on a lower timeframe (e.g., 1 minute),
// the `request.security` calls correctly pull data aligned with the *current unfolding* higher timeframe bar (e.g., 1 hour).
// This allows us to compare the current HTF bar's properties against its predecessor.
// --- Current Higher Timeframe Bar Properties ---
// These variables will hold the properties of the higher timeframe bar that the current lower timeframe bars are part of.
float htfOpen = request.security(ticker, higherTF, open, barmerge.lookahead_on)
float htfHigh = request.security(ticker, higherTF, high, barmerge.lookahead_on)
float htfLow = request.security(ticker, higherTF, low, barmerge.lookahead_on)
float htfClose = request.security(ticker, higherTF, close, barmerge.lookahead_on)
int htfTimeOpen = request.security(ticker, higherTF, time, barmerge.lookahead_on) // Start time of the current HTF bar
int htfTimeClose = request.security(ticker, higherTF, time_close, barmerge.lookahead_on) // End time of the current HTF bar
// --- Previous Higher Timeframe Bar Properties ---
// We use `[1]` inside `request.security` to fetch data from the bar *immediately preceding* the current HTF bar *on the higher timeframe*.
float htfPrevHigh = request.security(ticker, higherTF, high[1], barmerge.lookahead_on)
float htfPrevLow = request.security(ticker, higherTF, low[1], barmerge.lookahead_on)
// `htfPrevClose` is needed to check if the current HTF candle closes within the previous candle's range.
float htfPrevClose = request.security(ticker, higherTF, close[1], barmerge.lookahead_on)
// --- Condition Logic ---
// 1. Breach Condition: The current HTF bar's high must exceed the previous HTF bar's high,
// OR the current HTF bar's low must drop below the previous HTF bar's low.
bool breachHigh = htfHigh > htfPrevHigh
bool breachLow = htfLow < htfPrevLow
bool breachOccurred = breachHigh or breachLow
// 2. Close Within Range Condition: The current HTF bar's close must be within the price range
// of the previous HTF bar (i.e., between the previous bar's low and high).
// The previous bar's range is [htfPrevLow, htfPrevHigh].
bool closeWithinRange = htfClose >= htfPrevLow and htfClose <= htfPrevHigh
// Combined Condition: Both the breach and the close-within-range criteria must be met.
bool conditionMet = breachOccurred and closeWithinRange
// --- Box Management Setup ---
// This array will store the IDs of all the boxes we draw.
var box[] boxes = array.new_box(0)
// --- Drawing Logic ---
// `lastDrawnHtfTimeOpen` tracks the start time of the last higher timeframe bar for which we successfully drew a box.
// This prevents drawing multiple boxes for the same HTF bar if the condition remains true across multiple lower TF ticks.
var int lastDrawnHtfTimeOpen = na
// We check if we've moved to a new higher timeframe bar (`htfTimeOpen != lastDrawnHtfTimeOpen`)
// AND if the condition is met for this new HTF bar (`conditionMet`).
if htfTimeOpen != lastDrawnHtfTimeOpen and conditionMet
// Create the highlighting box for the current HTF bar.
box newBox = box.new(
left=htfTimeOpen,
right=htfTimeClose,
top=htfHigh,
bottom=htfLow,
border_color=highlightColor,
border_width=2,
bgcolor=color.new(highlightColor, 90), // Semi-transparent background
extend=extend.none // Ensure the box only spans the HTF bar, not infinitely.
)
// Add the newly created box's ID to our array.
array.push(boxes, newBox)
// --- Enforce the Maximum Number of Boxes ---
// If the number of boxes in our array exceeds the user-defined limit (`maxBoxes`):
if array.size(boxes) > maxBoxes
// Get the ID of the oldest box (which is at the beginning of the array).
box oldestBoxId = array.get(boxes, 0)
// Delete that oldest box from the chart.
box.delete(oldestBoxId)
// Remove the ID of the deleted box from our array.
array.remove(boxes, 0)
// Update `lastDrawnHtfTimeOpen` to the start time of the HTF bar we just processed.
// This marks this HTF bar as having its box drawn, so we don't re-draw for it.
lastDrawnHtfTimeOpen := htfTimeOpen
How this revised script addresses the error:
maxBoxes Input: You can now control how many recent boxes are displayed via the indicator's settings (default is 50).
boxes Array: A var box[] boxes array is used to keep track of the box.id of every box created.
- Box Creation and Management:
- When a new box is created (
box.new(...)), its ID is added to the boxes array using array.push().
- The script then checks if the size of the
boxes array has exceeded the maxBoxes limit.
- If it has, the script retrieves the ID of the oldest box (the first one added to the array) using
array.get(boxes, 0).
- This oldest box is then deleted from the chart using
box.delete().
- Finally, its ID is removed from the
boxes array using array.remove(boxes, 0).
This process ensures that only the most recent maxBoxes boxes are ever visible on the chart, effectively cycling older ones out and preventing the "more than 500 bars" error.
To use this script:
- Open TradingView and navigate to the Pine Editor.
- Paste the code into the editor.
- Click "Add to Chart".
- Go to the indicator's settings (the gear icon next to its name on the chart).
- In the "Inputs" tab, select your desired "Higher Timeframe" and adjust the "Max Boxes to Display" as needed.
This indicator will now highlight the higher timeframe candles that meet your specified criteria, while respecting the drawing object limits.