Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting within a Loop, Cannot use 'plot' in local scope. (TradingView Pine Script)

Tags:

pine-script

I am having trouble figuring out the proper implementation while trying to clean up my code and I found a section that seemed ripe for a For-loop, however, I receive the following error:

Cannot use 'plot' in local scope. 

When trying to do the following example:

a = 10
b = 5
for i = 1 to b
    j = a * i
    plot(highest(j), title="Resistance", color=b, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)

My original code is as follows:

a=10
plot(highest(a*1), title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plot(highest(a*2), title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plot(highest(a*3), title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plot(highest(a*4), title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plot(highest(a*5), title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)

I ultimately would like to have the number of plots(the b variable) adjustable from say 0 to 20 and thus just writting out all the plot lines doesn't really work.

What is the proper way to implement this in Pine?

Thank you!

like image 787
Brett Avatar asked Aug 23 '26 04:08

Brett


1 Answers

Your script draw horizontal lines.
With version 5, you can use line in local scope :

a = 10
b = 5
for i = 1 to b
    j = a * i
    line.new(bar_index-1, ta.highest(j), bar_index, ta.highest(j), extend=extend.both)

and get a result like that : enter image description here

like image 83
G.Lebret Avatar answered Aug 25 '26 21:08

G.Lebret