[英]PineScript - zigzag trend change counter
I've made an indicator that is similiar to zigzag.我制作了一个类似于锯齿形的指标。 I want to write a formula that will count number of up trends or number of trend changes (from up to down and from down to up).我想写一个公式来计算上升趋势的数量或趋势变化的数量(从上到下和从下到上)。 I have problem with it, because my variable is still setting to 0. Could you help me to correct it?我有问题,因为我的变量仍然设置为 0。你能帮我改正吗?
//@version=3
study("ZigZag Poker", overlay=true)
//INPUTS
trend = 0
trend := na(trend[1]) ? 1 : trend[1] //Beggining trend set to up
LL = 0.0
LL := na(LL[1]) ? low : LL[1] //LastLow
HH = 0.0
HH := na(HH[1]) ? high : HH[1] //LastHigh
LO = 0.0
LO := na(LO[1]) ? open : LO[1] //LastOpen
LC = 0.0
LC := na(LC[1]) ? close : LC[1] //LastClose
LOLO = 0.0
LOLO := na(LOLO[1]) ? low : LOLO[1] //LowestLow
HIHI = 0.0
HIHI := na(HIHI[1]) ? high : HIHI[1] //HighestHigh
zigzag = na
kolor = 0 //variable that counts number of trend changes
imp = input(true, "Alt imp")
kolor := imp == true ? 2 : 0
if (trend > 0) // trend is up, look for new swing low
if close >= min(LO, LC)
LC := close
LL := low
LO := open
LOLO := low
HIHI := high
else
zigzag := HIHI
trend := -1
HH := high
HIHI := high > HIHI ? high : HIHI
LC := close
LL := low
LO := open
LOLO := low
kolor := kolor[1] + 1
else // trend is down, look for new swing high
if close <= max(LO, LC)
HH := high
HIHI := high
LC := close
LL := low
LO := open
LOLO := low < LOLO ? low : LOLO
else
zigzag := LOLO
trend := 1
HH := high
LC := close
LL := low
LO := open
kolor: = kolor[1] + 1
plot(kolor)
plot(zigzag, color = trend < 0 ? blue : orange, linewidth=2, offset=-1)
I know it's too late to help the OP, but the error is in the line kolor := imp == true ? 2 : 0
我知道现在帮助 OP 为时已晚,但错误在于kolor := imp == true ? 2 : 0
kolor := imp == true ? 2 : 0
, that always sets the value of kolor
to 2 or 0, for all the candles that are in the current trend. kolor := imp == true ? 2 : 0
,对于当前趋势中的所有蜡烛,始终将kolor
的值设置为 2 或 0。
What is missing is copying the last kolor's value on every loop, so kolor[1] can have a valid counter.缺少的是在每个循环中复制最后一个 kolor 的值,因此 kolor[1] 可以有一个有效的计数器。
Replacing that line with kolor := na(kolor[1])? 0: kolor[1]
用kolor := na(kolor[1])? 0: kolor[1]
替换该行kolor := na(kolor[1])? 0: kolor[1]
kolor := na(kolor[1])? 0: kolor[1]
will do it. kolor := na(kolor[1])? 0: kolor[1]
会做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.