简体   繁体   中英

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?

//@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 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.

What is missing is copying the last kolor's value on every loop, so kolor[1] can have a valid counter.

Replacing that line with kolor := na(kolor[1])? 0: kolor[1] kolor := na(kolor[1])? 0: kolor[1] will do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM