FAQ

How to Fix Pine Script Study Error “Too Many Drawings, Cannot Clean Oldest”

How to Fix Pine Script Study Error “Too Many Drawings, Cannot Clean Oldest”

Drawing objects need to be limited (per study or strategy) because they consume server resources. When you create too many drawings, Pine runtime automatically deletes old ones in a process known as garbage collection (removing data that is no longer required or in use). If something goes wrong with regards to drawings, you may encounter the following error. 

What causes this error?

The “too many drawings, cannot clean oldest’ study error will usually happen when you use max_bars_back parameters together with lines/labels in your Pine Script code. Selectively deleting labels/lines may cause this error. 

Let’s take a look at a sample script that will trigger this error.

//@version=4
study("Pivot Reversal Strategy", overlay = true, max_bars_back = 5000)

leftBars  = input(4)
rightBars = input(2)

swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)

is_ph = not na(swh)
is_pl = not na(swl)

hprice = 0.0
hprice := is_ph ? swh : hprice[1]

lprice = 0.0
lprice := is_pl ? swl : lprice[1]

hl = line(na)
ll = line(na)

hl := line.new(bar_index[rightBars], hprice, bar_index[barssince(is_ph) + rightBars], hprice, color = color.green, width = 2)
ll := line.new(bar_index[rightBars], lprice, bar_index[barssince(is_pl) + rightBars], lprice, color = color.red,   width = 2)

if (not is_ph)
    line.delete(hl[1])

if (not is_pl)
    line.delete(ll[1])

It appears that when max_bars_back is enabled in a study or strategy, Pine Script has a hard time collecting garbage (deleting old drawings) when the number of drawings exceeds the maximum number of lines/labels that are allowed per script. 

How do I fix this error 

Now that we know what causes the “Too many drawings, Cannot clean oldest” in Pine Script, let’s explore some of the things you can do to recover from this fault. 

→ Solution 1

Set max_lines_count / max_labels_count parameter to a maximum (500) in study/or strategy call, for example:

study("Pivot Reversal Strategy", overlay = true, max_bars_back = 5000, max_lines_count = 500, max_labels_count = 500)

This fix usually works. But pretty often, 500 labels/lines are simply not enough to cover the entire history of your instrument. In this case, you could recover from the error by limiting the history for which you need to plot your labels/lines. This is demonstrated in solution 2 below. 

→ Solution 2

You can use the following code to add an input with a date.

plotStartDate  = input(timestamp("2018-01-01T00:00:00"), type = input.time)
plot_cond  = time >= plotStartDate

This essentially identifies a specific first date so you have explicit control for the history you need to plot. You may then use a conditional if-statement to ensure that only bars after that date are plotted. 

Here’s one way to do it. 

hl = line(na)
ll = line(na)

if (plot_cond)
    hl := line.new(bar_index[rightBars], hprice, bar_index[barssince(is_ph) + rightBars], hprice, color = color.green, width = 2)
    ll := line.new(bar_index[rightBars], lprice, bar_index[barssince(is_pl) + rightBars], lprice, color = color.red,   width = 2)

That should settle the issue, and your indicator works as expected.

However, if you still encounter an error even after implementing Solution 2, it means that your specified historical period/range still has too many drawings. In this case, adjusting the history date to a more recent one (closer to the current day) could do the trick. 

Here’s the entire code of the fixed indicator.  

//@version=4
study("Pivot Reversal Strategy", overlay = true, max_bars_back = 5000, max_lines_count = 500, max_labels_count = 500)

plotStartDate  = input(timestamp("2018-01-01T00:00:00"), type = input.time)
plot_cond  = time >= plotStartDate

leftBars  = input(4)
rightBars = input(2)

swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)

is_ph = not na(swh)
is_pl = not na(swl)

hprice = 0.0
hprice := is_ph ? swh : hprice[1]

lprice = 0.0
lprice := is_pl ? swl : lprice[1]

hl = line(na)
ll = line(na)

if (plot_cond)
    hl := line.new(bar_index[rightBars], hprice, bar_index[barssince(is_ph) + rightBars], hprice, color = color.green, width = 2)
    ll := line.new(bar_index[rightBars], lprice, bar_index[barssince(is_pl) + rightBars], lprice, color = color.red,   width = 2)

if (not is_ph)
    line.delete(hl[1])

if (not is_pl)
    line.delete(ll[1])

Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png
Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news