【问题标题】:How to plotshape only on first candle where condition is true?如何仅在条件为真的第一根蜡烛上绘制形状?
【发布时间】:2019-08-21 10:36:20
【问题描述】:

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

为了学习,我正在尝试建立一个简单的策略,如果蜡烛连续 2 次收于 EMA 上方或下方,那么它将打印一个买入或卖出箭头,但仅在第一个蜡烛上是真的。

正如您从下面的 sn-p 中看到的那样,我已经绘制了形状,但形状现在显示在条件为真的所有蜡烛上。

我也不确定如何编写仅在 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)

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    形状现在显示在条件为真的所有蜡烛上。

    这个问题我有详细解答here

    我也不知道怎么写这个条件 在 EMA 上方或下方的第二根蜡烛出现时显示形状 打印出来的。

    您应该使用带有历史引用运算符[] 的计数器。这样,如果您的条件为真,则将计数器加 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)
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      相关资源
      最近更新 更多