在循环内绘图,不能在本地范围内使用“绘图”。 (TradingView Pine脚本)

时间:2020-05-03 18:49:38

标签: pine-script

在尝试清理代码时,我很难弄清楚正确的实现,并且发现对于For循环来说似乎已经成熟了,但是,我收到以下错误:

Cannot use 'plot' in local scope. 

尝试执行以下示例时:

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)

我的原始代码如下:

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)

我最终希望将图的数量(b变量)从0调整为20,因此只写出所有的图线实际上是行不通的。

在Pine中实现此功能的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

正确的方法是代码最后部分中的漫长过程。您可以使用以下类似的方法使维护变得更容易:

//@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)