FAQ – Study Error – Too many drawings, cannot clean oldest. – how to fix it?

Time to time you can see following error in TradingView: Study Error; Too many drawings, cannot clean oldest.

This is usually happening because you’re using max_bars_back parameters together with lines/labes in your PineScript. If you’re selectively deleting your labels/lines you might see this error. Here is an example of script causing 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 seems like when max_bars_back is enabled in study or strategy PineScript has issues with deleting old drawings when number of these drawing exceeds max number of lines / labels allowed per scritps.

To fix this error in your script you can do 2 things:

  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)

It might help you, but pretty oftern 500 labels/lines might not be enough to cover entire history of your instrument. To fix that you can limit history for which you plot your labels / lines:

2. Add an input with a starting date for your drawings and plot labels / lines only starting from that date.

To add an input with a date you can use following code:

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

It will create a day input and will compute a bollean variable that will be true only for bars after that date. Now you can use this condition to create new labels / lines only when condition is true:

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)

This works well, after that indicator works as expected:

If after doing that you still see ” Too many drawings, cannot clean oldest. ” error – adjust the date to a more recent one, closed to today.

Here is 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.