如何仅在条件为真的情况下仅在第一个蜡烛上绘制形状?

时间:2019-08-21 10:36:20

标签: pine-script

我刚开始学习PineScript,已经碰壁了……

要学习,我正在尝试建立一个简单的策略,如果蜡烛连续两次收盘高于或低于EMA,那么它将打印买入或卖出箭头,但只会在此蜡烛的第一根蜡烛上是真的。

从下面的代码段中可以看到,我已经绘制了形状,但现在在所有条件为真的蜡烛上都显示了形状。

我也不知道如何写这样的条件,即仅在EMA上方或下方的第二根蜡烛打印后才显示形状。

我非常感谢我要实现的语法和逻辑方面的一些指针(作为菜鸟,仅显示代码可能会使我感到困惑!)

// @version=4
study("EMA Close Strat", shorttitle="EMA Close Strat", overlay=true)

// EMA
EMA_Checkbox = input(title="EMA", type=input.bool, defval=true)
EMA_Length = input(title="EMA Length", type=input.integer, defval=13, minval=1)
EMA_Out = ema(close, EMA_Length)
plot(EMA_Out, title="EMA", color=#fc4c2a, linewidth=2, transp=0)
Buy = close >= EMA_Out
plotshape(Buy, style=shape.triangleup)
Sell = close < EMA_Out
plotshape(Sell, style=shape.triangledown, location=location.belowbar)

1 个答案:

答案 0 :(得分:0)

  

现在,条件成立的所有蜡烛上都会显示形状。

我对此问题here有详细的答案。

  

我也不确定如何写这样的条件   一旦EMA上方或下方的第二根蜡烛具有   

您应该将计数器与历史记录引用运算符[]一起使用。这样,如果您的条件为true,则将计数器加1。但是,您应该使用历史记录引用运算符来访问计数器的先前值。

cns_up = 0                  // Declare the up counter
cns_up := nz(cns_up[1])     // Get the previous value of it

cns_dwn = 0                 // Declare the up counter
cns_dwn := nz(cns_dwn[1])   // Get the previous value of it

cns_up := close >= EMA_Out ? cns_up + 1 : 0     // Only increment the counter, if the condition is TRUE. Reset it otherwise
cns_dwn := close < EMA_Out ? cns_dwn + 1 : 0    // Only increment the counter, if the condition is TRUE. Reset it otherwise

完整代码:

// @version=4
study("EMA Close Strat", shorttitle="EMA Close Strat", overlay=true)

EMA_Checkbox = input(title="EMA", type=input.bool, defval=true)
EMA_Length = input(title="EMA Length", type=input.integer, defval=13, minval=1)
cns_len = input(title="Consecutive up/down length", type=input.integer, defval=2, minval=1)

cns_up = 0                  // Declare the up counter
cns_up := nz(cns_up[1])     // Get the previous value of it

cns_dwn = 0                 // Declare the up counter
cns_dwn := nz(cns_dwn[1])   // Get the previous value of it

isLong = false              // A flag for going LONG
isLong := nz(isLong[1])     // Get the previous value of it

isShort = false             // A flag for going SHORT
isShort := nz(isShort[1])   // Get the previous value of it

// EMA
EMA_Out = ema(close, EMA_Length)
plot(EMA_Out, title="EMA", color=#fc4c2a, linewidth=2, transp=0)

cns_up := close >= EMA_Out ? cns_up + 1 : 0     // Only increment the counter, if the condition is TRUE. Reset it otherwise
cns_dwn := close < EMA_Out ? cns_dwn + 1 : 0    // Only increment the counter, if the condition is TRUE. Reset it otherwise

buySignal = not isLong and (cns_up >= cns_len)      // Check the BUY condition
sellSignal = not isShort and (cns_dwn >= cns_len)   // Check the SELL condition

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

plotshape(buySignal, style=shape.triangleup, color=color.green, transp=40, text="BUY", editable=false, location=location.belowbar, size=size.small)
plotshape(sellSignal, style=shape.triangledown, color=color.red, transp=40, text="SELL", editable=false, location=location.abovebar, size=size.small)

enter image description here