【问题标题】:Switch on the fill between plots only if a condition is met仅当满足条件时才打开绘图之间的填充
【发布时间】:2020-01-07 00:58:38
【问题描述】:

好的。因此,我正在尝试为绘图之间的填充进行切换。

这样一来,填充并不总是出现,而是仅在我需要时出现,并且仅在我想要的地块之间,而不是所有的地块之间。

我认为,第一步是对填充进行切换——真或假。之后再使用“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;..." 等等......

我在这里做错了什么?

还有一个我在做的,它抛出了一个错误,说“fill cannot be used in local...”等,大意是这样。


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

所以我仍然无法想出一个开关,让我:1. 绘制单个 ema,2. 用另一个选择填充单个 ema。

//@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-statement plot switch-statement pine-script


    【解决方案1】:

    错误消息清楚地告诉您您做错了什么。您尚未声明这些变量:p8、p100 和 p200。通过查看您的代码,您不需要这些变量,您可以分别使用ep8、ep100 和ep200。

    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)。

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

    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)
    

    【讨论】:

    • 但我确实在 if 语句 ep8= p8 ep100=p100 ep200=p200 中定义了它们,我猜“if”中的定义不算数?
    • 好的,所以我尝试了一些不同的方法来使它工作,但这会关闭整个情节而没有填充。如果我打开填充,它会绘制它们,但你不能关闭填充。我将在我编辑的帖子中展示我所做的。
    【解决方案2】:
    fill(p8, p100, color=efp ? teal : na)
    fill(p100, p200, color=efp ? olive : na)
    fill(p8, p200, color=efp ? green : na)
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2018-09-24
    • 2020-01-10
    • 2018-01-22
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多