7

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!

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Brett
  • 154
  • 1
  • 2
  • 11

3 Answers3

0

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

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
0

As you know, Pine Script codes run candle by candle, so they must be fast! plot() is designed to be called at every candle and cannot be used in a scope like for loop. So you may change your logic based on the pine limitations (give up some of it) and choose a more possible way. Generally speaking, plotting inside a for is not a good idea since it will generate a lot of lines by each candle. So you may want to call plots one by one...

Now based on the 'resistance' name in the code, I suggest you use an array of lines. When the logic of resistance is met you generate a new line and push it to the array, next time the logic was met, you check the array if there is a line near the current price or not using a for each, if there was you extend the line inside the array. I coded some thing like that here

Emad
  • 465
  • 5
  • 9
-3

The proper way is the long way as in the last part of your code. You can make it somewhat easier to maintain using something like this:

//@version=4
study("", "", true)
noOfPlots = input(12, minval = 0, maxval = 20)
a=10
d=0
plotNo = 0
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
plotNo := plotNo + 1, plot(plotNo <= noOfPlots ? highest(a* plotNo) : na, title="Resistance", color=color.green, linewidth=2, style=plot.style_line, transp=d, offset=-9999, trackprice=true)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21