首页 > 解决方案 > 仅当满足条件时才打开绘图之间的填充

问题描述

好的。所以我正在尝试为地块之间的填充进行切换。

这样一来,填充并不总是出现,但只有在我想要它的时候才会出现,并且只在我想要的地块之间,而不是所有的地块之间。

我想,第一步是对填充进行切换——真或假。然后在后面使用“if”,如下图。

//@version=3
study("fill ema test")

//---input switches for the emas

ei8 = input(true, title="ema 8")
ei100 = input(true, title="ema 100")
ei200 = input(true, title="ema 200")

//---input switch attempts for the fills. 
// ef8 = input(false, title= "fill e8/e100")
// ef100 = input(false, title= "fill e100/e200")
// ef200 = input(false, title="fill e8/e200")

efp= input(false, title="fill in the emas")

//----ema calc
e8 = ema(close, 8)
e100 = ema(close, 100)
e200 = ema(close, 200)

//----plots. checks if the switch for the emas are on and then plots. 

ep8 = plot(ei8 and e8?e8:na, color= e8>=e100? green:red)
ep100 = plot(ei100 and e100?e100:na, color= e100>=e200? blue:purple)
ep200 = plot(ei200 and e200?e200:na, color= gray)

//---- now if i make this if statement here, then i can 

if efp
    ep8= p8
    ep100=p100
    ep200=p200
    fill(p8, p100, color=teal)
    fill(p100, p200, color=olive)
    fill(p8, p200, color=green)

这会引发错误"Undeclared identifier p8;..."等......

我在这里做错了什么?

我正在研究另一个,它抛出了一个错误,说“填充不能在本地使用......”等等,大意是这样。


编辑:9-4-19 根据 baris 的建议。但是他的想法完全打开/关闭了填充线图,所以我尝试为单个填充设置开关,但这也不起作用。

所以我仍然有问题想出一个让我的开关:1.绘制个人emas,2.用另一个选择填充个人emas。

//@version=3
study("fill ema test")

//---input switches for the emas

ei8 = input(true, title="ema 8")
ei100 = input(true, title="ema 100")
ei200 = input(true, title="ema 200")

//----ema calc
e8 = ema(close, 8)
e100 = ema(close, 100)
e200 = ema(close, 200)

//---input switch attempts for the fills. this added to the plot will turn fills on or off but only along with the plots.  
// ef8 = input(false, title= "fill e8/e100")
// ef100 = input(false, title= "fill e100/e200")
// ef200 = input(false, title="fill e8/e200")

efp= input(true, title="fill in the emas")

//----plots. checks if the switch for the emas are on and then plots. 

ep8 = plot(ei8 and e8 and efp?e8:na, color= e8>=e100? green:red)
ep100 = plot(ei100 and e100 and efp?e100:na, color= e100>=e200? blue:purple)
ep200 = plot(ei200 and e200 and efp?e200:na, color= gray)


fill(ep8, ep100, color=teal)
fill(ep100, ep200, color=olive)
fill(ep8, ep200, color=green)

执行以下操作将绘制所有线条和填充,但同时单独关闭它们。

ef8 = input(true, title= "fill e8/e100")
ef100 = input(true, title= "fill e100/e200")
ef200 = input(true, title="fill e8/e200")

// efp= input(true, title="fill in the emas")

//----plots. checks if the switch for the emas are on and then plots. 

// ep8 = plot(ei8 and e8 and efp?e8:na, color= e8>=e100? green:red)
// ep100 = plot(ei100 and e100 and efp?e100:na, color= e100>=e200? blue:purple)
// ep200 = plot(ei200 and e200 and efp?e200:na, color= gray)

ep8 = plot(ei8 and e8 and ef8?e8:na, color= e8>=e100? green:red)
ep100 = plot(ei100 and e100 and ef100?e100:na, color= e100>=e200? blue:purple)
ep200 = plot(ei200 and e200 and ef200?e200:na, color= gray)

fill(ep8, ep100, color=teal)
fill(ep100, ep200, color=olive)
fill(ep8, ep200, color=green)

理想情况下,线条可以单独关闭和打开,填充也可以单独关闭和打开。

这只是我遇到的一些逻辑问题还是交易视图的限制?

标签: if-statementplotswitch-statementpine-script

解决方案


错误消息清楚地告诉您您做错了什么。您尚未声明这些变量p8p100p200。通过查看您的代码,您不需要这些变量,您可以简单地分别使用ep8,ep100ep200

if efp
    fill(ep8, ep100, color=teal)
    fill(ep100, ep200, color=olive)
    fill(ep8, ep200, color=green)

如果这样做,您将收到以下错误:

line 31: Cannot use 'fill' in local scope.;
line 32: Cannot use 'fill'in local scope.; 
line 33: Cannot use 'fill' in local scope.

您不能fill()在本地范围内调用(和其他一些函数)(此处为 if-block)。

您可以做的是,只需将您的条件添加到series函数的参数中plot()

ep8 = plot(ei8 and e8 and efp?e8:na, color= e8>=e100? green:red)
ep100 = plot(ei100 and e100 and efp?e100:na, color= e100>=e200? blue:purple)
ep200 = plot(ei200 and e200 and efp?e200:na, color= gray)

注意and efp条件。

所以,两者在一起:

//----plots. checks if the switch for the emas are on and then plots. 

ep8 = plot(ei8 and e8 and efp?e8:na, color= e8>=e100? green:red)
ep100 = plot(ei100 and e100 and efp?e100:na, color= e100>=e200? blue:purple)
ep200 = plot(ei200 and e200 and efp?e200:na, color= gray)

//---- now if i make this if statement here, then i can 

fill(ep8, ep100, color=teal)
fill(ep100, ep200, color=olive)
fill(ep8, ep200, color=green)

推荐阅读