ZigZag/Fib Retracement % Indicator

evanevans

Active member
I'm wondering if someone would be interested in creating/coding a ZigZag/Fib Retracement % indicator?

The premise is this:

A market, stock, for a particular period is "choppy" (ranging/non-trending) if rally retracements exceed a % such as 61.8% (or 50%). The same is considered trending if retracements are short of the same or another % (ie: 49%).

If we can plot those %'s we should be able to get a clear image of trending vs non-trending moments.

It's a starting place at least. I'm sure we get more fancy with it, or if you have any ideas let's try them.

Looking forward to seeing how this comes out!

@Welkin interested? :)
 

evanevans

Active member
I can't seem to find this indicator which I think exists here somewhere. Basically it draws the fib retracement levels for a current trend, the detection and size of which you can define by the zig zag indicator.
 

Lysergic

New member
I can't seem to find this indicator which I think exists here somewhere. Basically it draws the fib retracement levels for a current trend, the detection and size of which you can define by the zig zag indicator.
Did you manage to figure this one out? This would be a good indicator for any trader to have at their disposal .
 

SleepyZ

Well-known member
@Lysergic
This uses TOS's zigzaghighlow indicator, which has inputs to adjust the pivot criteria. There are fib inputs to limit their plot or plot on the whole chart, whether to show bubbles and whether to show fib price in bubbles.

Please remember the zigzaghighlow indicator repaints and thus a pivot can disappear before a pivot in the opposite direction occurs.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2021
#

input priceH = high;
input priceL = low;
input percentageReversal = 3.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1.5;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' must not be zero");

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absoluteReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absoluteReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();


#-----------------------
input limit_plot =  yes;
def fibh   = HighestAll(if IsNaN(close[-1]) then maxPriceH else Double.NaN);
def fibl   = HighestAll(if IsNaN(close[-1]) then minPriceL else Double.NaN);

def fibhbn  = CompoundValue(1, HighestAll(if high == fibh then BarNumber() else Double.NaN), fibh);
def fiblbn  = CompoundValue(1, HighestAll(if low  == fibl then BarNumber() else Double.NaN), fibl);

def range = fibh - fibl;
def f1 = range * .236;
def f2 = range * .382;
def f3 = range * .500;
def f4 = range * .618;
def f5 = range * .764;

plot fibhh = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else fibh;
fibhh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibhh.SetDefaultColor(Color.RED);

plot fibll = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else fibl;
fibll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibll.SetDefaultColor(Color.GREEN);

plot fib1 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f1 else Double.NaN);
fib1.SetPaintingStrategy(PaintingStrategy.DASHES);
fib1.SetLineWeight(2);
fib1.SetDefaultColor(Color.YELLOW);

plot fib2 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f2 else Double.NaN);
fib2.SetPaintingStrategy(PaintingStrategy.DASHES);
fib2.SetLineWeight(2);
fib2.SetDefaultColor(Color.CYAN);

plot fib3 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll(if IsNaN(close[-1]) then minPriceL + f3 else Double.NaN);
fib3.SetPaintingStrategy(PaintingStrategy.DASHES);
fib3.SetDefaultColor(Color.WHITE);
fib3.SetLineWeight(2);

plot fib4 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f4 else Double.NaN);
fib4.SetPaintingStrategy(PaintingStrategy.DASHES);
fib4.SetLineWeight(2);
fib4.SetDefaultColor(Color.CYAN);

plot fib5 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f5 else Double.NaN);
fib5.SetPaintingStrategy(PaintingStrategy.DASHES);
fib5.SetLineWeight(2);
fib5.SetDefaultColor(Color.YELLOW);

#Fib Bubbles--------------------------------------------
input bubblemover  = 8;
def b              = bubblemover;
def b1             = b + 1;
input showbubbles  = yes;
input bubble_price = yes;
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fibh[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(0)
               else AsPercent(1)) + "\n" + (if !bubble_price then "" else AsText(fibh[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fibl[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(1)
               else AsPercent(0)) + "\n" + (if !bubble_price then "" else AsText(fibl[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fib1[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.764)
               else AsPercent(.236)) + "\n" + (if !bubble_price then "" else AsText(fib1[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib2[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.618)
               else AsPercent(.382)) + "\n" + (if !bubble_price then "" else AsText(fib2[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib3[b],
               AsPercent(.500) + "\n" + (if !bubble_price then "" else AsText(fib3[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib4[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.382)
               else AsPercent(.618)) + "\n" + (if !bubble_price then "" else AsText(fib4[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib5[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.236)
               else AsPercent(.764)) + "\n" + (if !bubble_price then "" else AsText(fib5[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);

fibhh.HideBubble();
fibll.HideBubble();
fib1.hidebubble();
fib2.hidebubble();
fib3.hidebubble();
fib4.hidebubble();
fib5.hidebubble();
 

Similar threads

Top