Pinescript: 我可以根据当前条形图的数值来开启或关闭情节系列吗?

问题描述 投票:0回答:1

问题:我有两条移动平均线EMA1,EMA2。

  • 我有两个移动平均线EMA1,EMA2。
  • 如果ema1大于ema2,则I.只是 想显示ema1和它的历史值,而隐藏其他。
  • 如果ema1小于ema2,则我 只是 想显示ema2和它的历史价值,隐藏其他。

我已经尝试了无数的方法,但没有任何乐趣。



//@version=4
study("Line test")

ema1 = ema(close,5)
ema2 = ema(close,10)

var plot_ema_1 = false
var plot_ema_2 = false

if ema1[1] > ema2[1]
    plot_ema_1 := true
    plot_ema_2 := false

else
    plot_ema_1 := false
    plot_ema_2 := true  

plot(plot_ema_1 ? ema1 : na, color=color.blue, title="EMA-1", style=plot.style_line)
plot(plot_ema_2 ? ema2 : na, color=color.orange, title="EMA-2", style=plot.style_line)

问题的图片

我在现实生活中的使用是有点棘手的,我已经意识到了。 其实还有第三个变量...correlation_to_asset1和correlation_to_asset2(这是两个独立的相关系数)。

我想满足的条件是

if (correlation_to_asset1 > correlation_to_asset2)
    display_ema1_and_all_its_historical_data 

   // ema2 needs to be completely turned off. A combined line doesn't 
   // work as it messes with the scaling.

else
    display_ema2_and_all_its_historical_data

   // ema1 needs to be completely turned off. A combined line doesn't 
   // work as it messes with the scaling.

如果有任何关于这方面的见解,将非常感谢。我们可以使用ema3ema4作为第34个变量......我只是想通过提到cc来介绍一些背景。

pine-script
1个回答
0
投票

这显示了3种不同的方法。我们更喜欢方法#1。

//@version=4
study("Line test", "", true)

ema1 = ema(close,5)
ema2 = ema(close,10)

// ————— v1: plot the maximum of the 2 emas.
plotEma1 = ema1[1] > ema2[1]
c = plotEma1 ? color.blue : color.orange
mx = max(ema1, ema2)
plot(mx, color=c, title="EMA", style=plot.style_line)
plotchar(plotEma1 != plotEma1[1] ? mx : na, "Dot", "•", location.absolute, c, size = size.tiny)

// var plot_ema_1 = false
// var plot_ema_2 = false

// if ema1[1] > ema2[1]
//     plot_ema_1 := true
//     plot_ema_2 := false

// else
//     plot_ema_1 := false
//     plot_ema_2 := true  

// ————— v2: plot `na` color when the line isn't needed.
// plot(ema1, color=plot_ema_1 ? color.blue : na, title="EMA-1", style=plot.style_line)
// plot(ema2, color=plot_ema_2 ? color.orange : na, title="EMA-2", style=plot.style_line)

// ————— v3: plot using linebreak style.
// plot(plot_ema_1 ? ema1 : na, color=color.blue, title="EMA-1", style=plot.style_linebr)
// plot(plot_ema_2 ? ema2 : na, color=color.orange, title="EMA-2", style=plot.style_linebr)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.