Fib with zigzags

METAL

Well-known member
Plus
I have been using the following HH,HL,LH,LL Auto Fib indicator but would like to change it if plausible.
What I would like to accomplish, Is for the fib lines to be drawn as a new top or bottom is plotted instead of waiting for a Higher High Lower High etc to form. I believe the zig zag code may be the best stating point. Not sure though. @BenTen , What do you think? I typically trade the 2 min TF. Say I have a downtrend and as soon as it starts to pullback at all, I would like the fibs to be placed at that new low from the last high (X # of bars back) that way I may use it for pullbacks and such.

This may be a zig zag code but the only issue is that it has to move beyond the high or low in order to determine the Higher High/Higher LOW etc..

Code:
# HH-HL-LH-LL-50%,6.18%-FIBs Auto
#input ShowBreaks  = yes; # title = "Show Breaks" )
input ShowHhLlLabel = yes;
input src = hl2;
input breakout = {"Volume", "Price",Default "None"};
input RepaintingType   = {default "On", "Off: Candle Confirmation", "Off: High & Low"};# 'Repainting'
input rightBars  = 24;#, title = "Right Bars")
input leftBars  = 20;#(15, title = "Left Bars ")
input volumeThreshold  = 20;# title = "Volume Threshold")


def na = Double.NaN;
def last = isNaN(close);
def VolBreak = breakout==breakout."Volume";
def PriceBreak = breakout==breakout."Price";

#def rTon = input_repType == input_repType."On";
#def rTcc = input_repType == input_repType."Off: Candle Confirmation";
#def rThv = input_repType == input_repType."Off: High & Low";
#---- Colors
DefineGlobalColor("green" , CreateColor(8, 153, 129));
DefineGlobalColor("red"   , CreateColor(255, 82, 82));

script fixnan {
    input source = close;
    def fix = if !IsNaN(source) then source else fix[1];
    plot result = fix;
}
#drawBox(condition, y1, y2, color) =>
script drawBox {
    input condition = yes;
    input y1        = high;
    input y2        = low;
    def boxHi = if condition then y1 else boxHi[1];
    def boxLo = if condition then y2 else boxLo[1];
    plot ph = if !IsNaN(close[5]) then if !boxHi then high else boxHi else Double.NaN;
    plot pl = if !IsNaN(close[5]) then if !boxLo then low else boxLo else Double.NaN;
}
#repaint(c1, c2, c3) =>
script repaint {
    input type = "On";
    input c1 = no;
    input c2 = no;
    input c3 = no;
    def rTon = type == "On";
    def rTcc = type == "Off: Candle Confirmation";
    def rThv = type == "Off: High & Low";
    def repaint = if rTon then c1 else if rThv then c2 else if rTcc then c3 else Double.NaN;
    plot out = repaint;
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def ph = FindPivots(high, 1 , leftBars, rightBars);
def pl = FindPivots(low, -1, leftBars, rightBars);
def highUsePivot = fixnan(ph);
def lowUsePivot  = fixnan(pl);
#// Box Height
def s_yLoc = if low[1]  > low[-1]  then low[-1] else low[1];
def r_yLoc = if high[1] > high[-1] then high[1] else high[-1];

plot r1 = drawBox((highUsePivot - highUsePivot[1]), highUsePivot, r_yLoc).ph;
plot r2 = drawBox((highUsePivot - highUsePivot[1]), highUsePivot, r_yLoc).pl;

plot s1 = drawBox((lowUsePivot - lowUsePivot[1]), s_yLoc, lowUsePivot).ph;
plot s2 = drawBox((lowUsePivot - lowUsePivot[1]), s_yLoc, lowUsePivot).pl;

#plot r1 = if IsNaN(src) or highUsePivot == 0 then na else highUsePivot;
r1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r1.SetDefaultColor(GlobalColor("green"));
r2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r2.SetDefaultColor(GlobalColor("green"));
#plot s1 = if IsNaN(src) or lowUsePivot == 0 then na else lowUsePivot;
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(GlobalColor("red"));
s2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s2.SetDefaultColor(GlobalColor("red"));

#//Volume %
def short = ExpAverage(volume, 5);
def long  = ExpAverage(volume, 10);
def osc = 100 * (short - long) / long;

#//For breaks with volume
def up = (open - close) < (high - open);
def Dn = (open - low)   > (close - open);
def u11 = (VolBreak and close crosses below s2);
def u22 = (VolBreak and low crosses below s2);
def u33 = (VolBreak and close crosses below s2) and !isNaN(close[1]);
def o11 = (VolBreak and close crosses above r1);
def o22 = (VolBreak and high crosses above r1);
def o33 = (VolBreak and close crosses above r1) and !isNaN(close[1]);
def CrossUp = repaint(RepaintingType, u11, u22, u33);
def CrossDn = repaint(RepaintingType, o11, o22, o33);
#def CrossUp = VolBreak and src crosses above highUsePivot;
#def CrossDn = VolBreak and src crosses below lowUsePivot;
def VolThresh = osc > volumeThreshold;

def volDn = CrossDn and !up and VolThresh;
def volUp = CrossUp and !Dn and VolThresh;

#//For bull / bear wicks
def BearWicks = CrossDn and up and VolThresh;
def BullWicks = CrossUp and Dn and VolThresh;
# Support Result breaks
def sBreakDn = CrossDn and up and !VolThresh;
def rBreakUp = CrossUp and Dn and !VolThresh;

#------ Strength
def strgUp = volUp * 3 + BullWicks * 2 + rBreakUp;
def StrgDn = volDn * 3 + BearWicks * 2 + sBreakDn;

AddChartBubble(StrgDn > 2 , low, "Strong", Color.GREEN, no);
AddChartBubble(strgUp > 2 , high, "Strong", Color.RED, yes);
AddChartBubble(StrgDn > 1 and StrgDn <= 2 , low, "Normal", Color.DARK_GREEN, no);
AddChartBubble(strgUp > 1 and strgUp <= 2 , high, "Normal", Color.DARK_RED, yes);
AddChartBubble(StrgDn > 0 and StrgDn <= 1 , low, "Weak", Color.LIGHT_GREEN, no);
AddChartBubble(strgUp > 0 and strgUp <= 1 , high, "Weak", Color.PINK, yes);
#--- HH/LL label

def hh = if highUsePivot > highUsePivot[1] then highUsePivot else na;
def ll = if lowUsePivot  < lowUsePivot[1]  then lowUsePivot else na;
def hl = if lowUsePivot  > lowUsePivot[1]  then lowUsePivot else na;
def lh = if highUsePivot < highUsePivot[1] then highUsePivot else na;

AddChartBubble(ShowHhLlLabel and ll, ll, "LL", Color.RED, no);
AddChartBubble(ShowHhLlLabel and hh, hh, "HH", Color.GREEN, yes);
AddChartBubble(ShowHhLlLabel and hl, hl, "HL", Color.DARK_GREEN, no);
AddChartBubble(ShowHhLlLabel and lh, lh, "LH", Color.DARK_RED, yes);

#// Breakout Event
def sBreak;# = na
def rBreak;# = na
def sLabel;#
def rLabel;
def u1 = (close crosses below s2);
def u2 = (low crosses below s2);
def u3 = (close crosses below s2) and !isNaN(close);
def o1 = (close crosses above r1);
def o2 = (high crosses above r1);
def o3 = (close crosses above r1) and !isNaN(close);
def cu = repaint(RepaintingType, u1, u2, u3);
def co = repaint(RepaintingType, o1, o2, o3);

if cu and isNaN(sBreak[1]) {
        sBreak = yes;
        rBreak = na;
        sLabel = if PriceBreak then s2 else na;
        rLabel = na;
    } else
if co and isNaN(rBreak[1]) {
        sBreak = na;
        rBreak = yes;
        sLabel = na;
        rLabel = if PriceBreak then r1 else na;
    } else {
        sBreak = na;
        rBreak = na;
        sLabel = na;
        rLabel = na;
}

AddChartBubble(!isNaN(sLabel), sLabel, "break", Color.RED, yes);
AddChartBubble(!isNaN(rLabel), rLabel, "break", Color.GREEN, no);

#---- END
input showFiboLines = yes;
input showZigZagLine = yes;
DefineGlobalColor("786", CreateColor(244,67,54));
DefineGlobalColor("618", CreateColor(129,199,132));
DefineGlobalColor("500", CreateColor(76,175,80));
DefineGlobalColor("382", CreateColor(0,150,136));
DefineGlobalColor("236", CreateColor(100,181,246));
DefineGlobalColor("000", CreateColor(95,95,95));

def bar = AbsValue(BarNumber());
def naPh = !isNaN(ph);
def naPl = !isNaN(pl);
def phPvt = highUsePivot;
def plPvt = lowUsePivot;
def phBar = if naPh then bar else phBar[1];
def plBar = if naPl then bar else plBar[1];
def max = max(phBar, plBar);
def maxBar = highestAll(max);
def plotCond = bar < maxBar or last[5] or !showFiboLines;
def ZigCond =  bar > maxBar - leftBars and showZigZagLine;
def upBar = phBar > plBar;

def fib1 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 0.236 else
                           plPvt + (phPvt - plPvt) * 0.236;
def fib2 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 0.382 else
                           plPvt + (phPvt - plPvt) * 0.382;
def fib3 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 0.500 else
                           plPvt + (phPvt - plPvt) * 0.500;
def fib4 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 0.618 else
                           plPvt + (phPvt - plPvt) * 0.618;
def fib5 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 0.786 else
                           plPvt + (phPvt - plPvt) * 0.786;
def fib6 = if plotCond then na else
             if upBar then phPvt - (phPvt - plPvt) * 1.000 else
                           plPvt + (phPvt - plPvt) * 1.000;
def zig = if naPh then high else
          if naPl then low else na;

plot fibo1 = fib1;
plot fibo2 = fib2;
plot fibo3 = fib3;
plot fibo4 = fib4;
plot fibo5 = fib5;
plot fibo6 = fib6;
plot ZigZag = if ZigCond then zig else na;
ZigZag.EnableApproximation();
ZigZag.SetStyle(Curve.SHORT_DASH);
fibo1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibo6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

ZigZag.SetDefaultColor(Color.YELLOW);
fibo1.SetDefaultColor(GlobalColor("000"));
fibo2.SetDefaultColor(GlobalColor("236"));
fibo3.SetDefaultColor(GlobalColor("382"));
fibo4.SetDefaultColor(GlobalColor("500"));
fibo5.SetDefaultColor(GlobalColor("618"));
fibo6.SetDefaultColor(GlobalColor("786"));



#-- END of CODe
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
394 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top