Drawing Memory Test Date Filter

🧠 The Complete Lesson: The Date Filter System for Managing Drawings in Pine Script
From the core idea to applying it on any indicator: line by line, setting by setting
---
📚 Table of Contents
---
1. The Problem: Why We Need This System
Every indicator that draws boxes, lines and labels on TradingView has a cap on how many objects it can keep: 500 per type (the default is 50 unless you raise it). Once the cap is reached, TradingView automatically deletes the oldest object to make room for the new one.
What happens in practice:
---
2. The Idea (The Discovery)
We add a date gate before every drawing operation. Any object that doesn't pass through the gate is never created at all.
The practical benefit: all 500 object slots are spent on the period you care about, the chart stays clean, and you control exactly which period you're testing.
An honest note: the system does not raise the 500 limit. It lets you spend that limit in the right place.
---
3. Pine Script Basics You Need to Read the Code
If you're a beginner, these concepts unlock the code.
a) The version line
Pine Script®
The first line of the script. It declares that the code is written in Pine Script version 6. Any line starting with // is a comment and is not executed.
b) Core built-in variables (series)
c) Looking back with square brackets
Pine Script®
d) The if statement
Pine Script®
The indentation (4 spaces) matters: it defines what belongs inside the condition. If showBoxes is true, the indented line runs.
e) The ternary operator ? :
Pine Script®
Syntax: condition ? value_if_true : value_if_false.
Here: if showPrevious is true, allowed = (pivotTime < startDate). Otherwise, allowed = (pivotTime >= startDate).
f) The na value
Pine Script®
na means "no value". ta.pivothigh returns na on most bars and returns a number only when a pivot high is confirmed. So the condition reads: "if we detected a pivot high".
g) Variable types
---
4. Indicator Structure: The indicator() Declaration and Object Limits
Pine Script®
Important: if you don't raise these values, the limit stays at 50 and older objects are deleted quickly. Each type has its own independent limit, so the possible total is 1500 objects.
---
5. Every Input Explained
Inputs are organized into groups (the group parameter) so they appear neatly in the settings window.
📅 The Drawing Control group
Drawing Start Date
Pine Script®
Show Previous Drawings
Show Boxes / Show Lines / Show Labels
📍 The Pivot Detection group
Pivot Left Bars (default 5)
Pivot Right Bars (default 5)
Tuning guide:
---
6. How Pivots Work Under the Hood
Pine Script®
A Pivot High: a bar whose high is higher than the highs of leftBars bars before it, and higher than the highs of rightBars bars after it.
Example with Left = 2 and Right = 2 (the high of each bar):
The key consequence:
This is exactly why all the drawing code uses bar_index[rightBars] to locate the real position of the pivot.
The Pivot Low follows the same logic, reversed:
Pine Script®
A note on repainting: once a pivot is confirmed, it doesn't change or disappear, because its confirmation conditions are complete. The delay is the price you pay for stability.
---
7. The Time Gate: Why time[rightBars]
Pine Script®
If we filtered by time (the current bar's time), we'd be judging the date of the confirmation, not the date of the pivot.
Example: the start date is 1 September, the pivot formed on 31 August and was confirmed on 1 September.
The golden rule: ask "when was this object born?" and filter by its real birth time.
---
8. Drawing Functions, Parameter by Parameter
📦 box.new
Pine Script®
🎨 color.new(color.red, 85)
The second number is the transparency, from 0 to 100: 0 = fully solid, 100 = fully transparent. 85 gives a very light fill that doesn't cover the candles.
For a pivot low the box is mirrored: top = pivotLow + 20 ticks and bottom = pivotLow.
📏 line.new
Pine Script®
🏷️ label.new
Pine Script®
---
9. The START Marker
Pine Script®
---
10. The Code at a Glance
The logical order:
Note: the original code has a variable called canDraw that is defined but never used, because the real filter is allowed inside each pivot block. You can delete it with no effect. Also, pivotTime and allowed are declared twice (once for highs and once for lows), which is fine, because each sits inside its own separate if block.
---
11. Historical Data Limits (Lookback)
TradingView loads a limited number of bars for each chart, and that number depends on your subscription plan and the timeframe.
---
12. How to Test the System
---
13. What It Solves and What It Doesn't
✅ Solves:
⚠️ Does not solve:
---
14. Applying the System to Any Indicator
Step 1: Add the inputs and the function at the top of your indicator
Pine Script®
The inRange function takes a time and returns true if that time is allowed.
Step 2: Raise the object limits
Pine Script®
Step 3: Add the condition before every drawing, using the event's real time
Example 1: Fair Value Gap (FVG)
Pine Script®
The gap starts two bars back, so we filter with time[2].
Example 2: Break of Structure (BOS)
Pine Script®
Here we filter by the time of the swing that was broken, not the time of the break.
Example 3: Buy or sell signal
Pine Script®
The signal belongs to the current bar, so we use time directly.
Example 4: Order Block indicator
Pine Script®
We filter by the time of the Order Block candle itself.
Step 4: Special cases
---
15. Advanced Upgrades
a) An end date (a fixed window)
Pine Script®
You select a period from one date to another.
b) Last N days without a manual date
Pine Script®
timenow is the current time, and 86400000 is the number of milliseconds in a day. This makes the window move automatically with time.
c) A master switch for the whole filter
Pine Script®
If you turn it off, the indicator draws everything as usual.
---
16. Common Mistakes
---
📌 Summary
The Date Filter System is a simple and powerful idea: a time gate before every drawing operation. The indicator only draws what serves the period you care about, so the 500-object slots are spent on what matters, the chart stays clean, and you can review the past or follow the present precisely. You can install it on almost any indicator with an inRange function and one condition before each drawing, always keeping the object's birth time in mind.
Draw what matters, ignore the rest.
---
⚠️ Disclaimer
This lesson is intended for educational and programming purposes only. Nothing in it constitutes financial advice or an invitation to buy or sell any asset. Risk management and capital preservation remain entirely the responsibility of the trader.
Trade with a plan. Trade with discipline. 📈
From the core idea to applying it on any indicator: line by line, setting by setting
---
📚 Table of Contents
- 1. The Problem: Why We Need This System
- 2. The Idea (The Discovery)
- 3. Pine Script Basics You Need to Read the Code
- 4. Indicator Structure: The indicator() Declaration and Object Limits
- 5. Every Input Explained
- 6. How Pivots Work Under the Hood
- 7. The Time Gate: Why time[rightBars]
- 8. Drawing Functions, Parameter by Parameter
- 9. The START Marker
- 10. The Code at a Glance
- 11. Historical Data Limits (Lookback)
- 12. How to Test the System
- 13. What It Solves and What It Doesn't
- 14. Applying the System to Any Indicator (With Examples)
- 15. Advanced Upgrades
- 16. Common Mistakes
---
1. The Problem: Why We Need This System
Every indicator that draws boxes, lines and labels on TradingView has a cap on how many objects it can keep: 500 per type (the default is 50 unless you raise it). Once the cap is reached, TradingView automatically deletes the oldest object to make room for the new one.
What happens in practice:
- The indicator runs from the very first bar of the chart's history and draws in old areas you don't care about.
- The 500 slots get spent on the distant past. When signals are frequent, older objects get deleted, sometimes the very ones you wanted.
- The chart gets cluttered with lines and zones that serve no purpose.
- You can't tell the indicator "start from this day" or "only analyze this period".
---
2. The Idea (The Discovery)
We add a date gate before every drawing operation. Any object that doesn't pass through the gate is never created at all.
- You choose a start date.
- Default behavior: the indicator draws from that date forward only.
- With Show Previous enabled: it draws only what happened before that date.
The practical benefit: all 500 object slots are spent on the period you care about, the chart stays clean, and you control exactly which period you're testing.
An honest note: the system does not raise the 500 limit. It lets you spend that limit in the right place.
---
3. Pine Script Basics You Need to Read the Code
If you're a beginner, these concepts unlock the code.
a) The version line
The first line of the script. It declares that the code is written in Pine Script version 6. Any line starting with // is a comment and is not executed.
b) Core built-in variables (series)
- time: the open time of the current bar, in milliseconds since 1970.
- bar_index: the number of the current bar, starting at 0 on the first bar of the chart.
- high / low: the highest and lowest price of the bar.
c) Looking back with square brackets
d) The if statement
The indentation (4 spaces) matters: it defines what belongs inside the condition. If showBoxes is true, the indented line runs.
e) The ternary operator ? :
Syntax: condition ? value_if_true : value_if_false.
Here: if showPrevious is true, allowed = (pivotTime < startDate). Otherwise, allowed = (pivotTime >= startDate).
f) The na value
na means "no value". ta.pivothigh returns na on most bars and returns a number only when a pivot high is confirmed. So the condition reads: "if we detected a pivot high".
g) Variable types
- float: a decimal number (prices).
- int: a whole number (times, counts).
- bool: true or false.
---
4. Indicator Structure: The indicator() Declaration and Object Limits
- Title: the name shown on the chart.
- overlay=true: draws on top of the main chart (not in a separate pane below it).
- max_boxes_count=500: the maximum number of boxes the script keeps. The default is 50 and the upper limit is 500.
- max_lines_count=500: the same for lines.
- max_labels_count=500: the same for labels.
Important: if you don't raise these values, the limit stays at 50 and older objects are deleted quickly. Each type has its own independent limit, so the possible total is 1500 objects.
---
5. Every Input Explained
Inputs are organized into groups (the group parameter) so they appear neatly in the settings window.
📅 The Drawing Control group
Drawing Start Date
- The input type is input.time: it gives you a date and time picker in the settings.
- The default is 1 September 2026 at 00:00 UTC (+0000).
- This is the foundation the gate is built on.
- Change it to the beginning of the period you're studying.
Show Previous Drawings
- OFF (default): draws from the start date forward.
- ON: draws only what came before the date.
- Useful for reviewing the past or comparing two periods (run once with OFF and once with ON).
Show Boxes / Show Lines / Show Labels
- Three switches that turn each drawing type on or off.
- If a type is off, no objects of that type are created, so none of its limit is consumed.
- Tip: if the chart gets crowded, keep only the lines and turn off the rest.
📍 The Pivot Detection group
Pivot Left Bars (default 5)
- The number of bars on the left of the point that must be lower (for a high) or higher (for a low).
- minval=1: you can't go below 1.
Pivot Right Bars (default 5)
- The number of bars on the right of the point used for confirmation.
- Important: the pivot isn't confirmed until this many bars have passed, so the drawing appears delayed by the Right value.
Tuning guide:
- 2-3: many sensitive points with plenty of noise, and they consume the limit fast.
- 5-8: a good balance for most timeframes.
- 10-15: fewer, stronger points, but confirmation is slower.
---
6. How Pivots Work Under the Hood
A Pivot High: a bar whose high is higher than the highs of leftBars bars before it, and higher than the highs of rightBars bars after it.
Example with Left = 2 and Right = 2 (the high of each bar):
- Bars: 10, 12, 15, 13, 11
- The bar with a high of 15 is higher than the two bars before it and the two bars after it, so it is a pivot high.
- But the function can't know that until the second bar after it appears (the last one, 11).
The key consequence:
- The function executes on the current bar (the last one, 11).
- But the pivot itself sits rightBars bars back.
- The returned value is the pivot's price (15), and everything else is na.
This is exactly why all the drawing code uses bar_index[rightBars] to locate the real position of the pivot.
The Pivot Low follows the same logic, reversed:
A note on repainting: once a pivot is confirmed, it doesn't change or disappear, because its confirmation conditions are complete. The delay is the price you pay for stability.
---
7. The Time Gate: Why time[rightBars]
If we filtered by time (the current bar's time), we'd be judging the date of the confirmation, not the date of the pivot.
Example: the start date is 1 September, the pivot formed on 31 August and was confirmed on 1 September.
- Filtering by time: the pivot is accepted (because the confirmation is after the start). That's wrong, since it belongs to August.
- Filtering by time[rightBars]: the pivot is rejected, which is correct.
The golden rule: ask "when was this object born?" and filter by its real birth time.
---
8. Drawing Functions, Parameter by Parameter
📦 box.new
- left: the left edge, at the real pivot bar.
- right: the right edge, at the current bar (the confirmation bar). So the box is exactly Right bars wide and does not extend into the future.
- top / bottom: the upper and lower edges. For a high: from the pivot price down 20 ticks.
- syminfo.mintick: the smallest price movement of the symbol. We multiplied it by 20 so the box height scales to the symbol.
- border_color: the outline color.
- bgcolor: the fill color.
🎨 color.new(color.red, 85)
The second number is the transparency, from 0 to 100: 0 = fully solid, 100 = fully transparent. 85 gives a very light fill that doesn't cover the candles.
For a pivot low the box is mirrored: top = pivotLow + 20 ticks and bottom = pivotLow.
📏 line.new
- x1, y1: the starting point (the pivot bar and its price).
- x2, y2: the end point (the current bar at the same price, so the line is horizontal).
- extend.right: extends the line to the right indefinitely, so it works as a support, resistance or liquidity level.
- width: the line thickness.
🏷️ label.new
- x, y: the label position.
- text: the displayed text.
- style_label_down: a label with a downward pointer, so it sits above the high. For lows we use style_label_up, so the label sits below the low.
- textcolor: the text color.
- color: the label background color.
---
9. The START Marker
- The condition becomes true on the first bar that crosses the start date: it is after the date (time >= startDate) while the bar before it was earlier (time[1] < startDate).
- plotshape draws a yellow diamond above that bar with the text START.
- Its purpose is visual: to confirm the filter is anchored where you expect.
---
10. The Code at a Glance
The logical order:
- 1) Indicator declaration and object limits.
- 2) Inputs (date, switches, pivot values).
- 3) Detect pivot highs. If one exists: compute its birth time, pass it through the gate, then draw.
- 4) Do the same for pivot lows.
- 5) The START marker.
Note: the original code has a variable called canDraw that is defined but never used, because the real filter is allowed inside each pivot block. You can delete it with no effect. Also, pivotTime and allowed are declared twice (once for highs and once for lows), which is fine, because each sits inside its own separate if block.
---
11. Historical Data Limits (Lookback)
TradingView loads a limited number of bars for each chart, and that number depends on your subscription plan and the timeframe.
- If you set a start date older than the first loaded bar, nothing will appear before that first bar.
- On small timeframes (1 minute, 5 minutes) the loaded bars cover only a few days, so an old date won't work.
- The fix: use a larger timeframe, move the start date closer to today, or scroll the chart left so more history loads.
---
12. How to Test the System
- 1) Add the indicator to a daily or 4-hour chart (long history).
- 2) Change Start Date to a date you can see on the chart and confirm the START diamond appears on the first bar of that date.
- 3) With OFF: confirm all drawings appear only after the diamond.
- 4) Enable Show Previous: confirm all drawings appear only before the diamond.
- 5) Try turning Boxes, Lines and Labels off one at a time.
- 6) Change Left and Right and watch how the number of points changes.
- 7) Try a Start Date in the future: nothing will appear, which is expected.
---
13. What It Solves and What It Doesn't
✅ Solves:
- Spending the 500-object limit on old periods you don't care about.
- Chart clutter.
- Precisely defining a testing or review period.
- Comparing "before" and "after" a specific date.
⚠️ Does not solve:
- It does not raise the maximum (500 stays 500).
- It does not reduce the indicator's calculations, since they still run on every bar. The filter controls drawing only.
---
14. Applying the System to Any Indicator
Step 1: Add the inputs and the function at the top of your indicator
The inRange function takes a time and returns true if that time is allowed.
Step 2: Raise the object limits
Step 3: Add the condition before every drawing, using the event's real time
Example 1: Fair Value Gap (FVG)
The gap starts two bars back, so we filter with time[2].
Example 2: Break of Structure (BOS)
Here we filter by the time of the swing that was broken, not the time of the break.
Example 3: Buy or sell signal
The signal belongs to the current bar, so we use time directly.
Example 4: Order Block indicator
We filter by the time of the Order Block candle itself.
Step 4: Special cases
- If your indicator stores drawings in arrays, apply inRange when adding to the array, not when drawing later.
- If the indicator updates an existing object (such as extending a box), you don't need a filter on the update, because the object was never created if it failed the gate.
- If one drawing has several events (a start and an end), filter by the start time.
---
15. Advanced Upgrades
a) An end date (a fixed window)
You select a period from one date to another.
b) Last N days without a manual date
timenow is the current time, and 86400000 is the number of milliseconds in a day. This makes the window move automatically with time.
c) A master switch for the whole filter
If you turn it off, the indicator draws everything as usual.
---
16. Common Mistakes
- Filtering by time when it should be time[offset], so results appear late or incomplete.
- Forgetting to raise max_boxes_count, max_lines_count and max_labels_count.
- Setting the date after the last bar on the chart, so nothing shows.
- Setting a date older than the loaded data (see the Lookback section).
- Mixing up the Show Previous direction: OFF is from the date forward, ON is before the date.
- Using very small Pivot values, which fills the limit with weak points.
- Forgetting that a pivot is confirmed late by the Right value, and assuming the indicator is broken.
- Expecting the filter to speed up the indicator, when it only controls drawing.
---
📌 Summary
The Date Filter System is a simple and powerful idea: a time gate before every drawing operation. The indicator only draws what serves the period you care about, so the 500-object slots are spent on what matters, the chart stays clean, and you can review the past or follow the present precisely. You can install it on almost any indicator with an inRange function and one condition before each drawing, always keeping the object's birth time in mind.
Draw what matters, ignore the rest.
---
⚠️ Disclaimer
This lesson is intended for educational and programming purposes only. Nothing in it constitutes financial advice or an invitation to buy or sell any asset. Risk management and capital preservation remain entirely the responsibility of the trader.
Trade with a plan. Trade with discipline. 📈
My personal account, contact me t.me/pine_astro
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.
My personal account, contact me t.me/pine_astro
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.