形状现在显示在条件为真的所有蜡烛上。
这个问题我有详细解答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)