Fractal Pivots with Trend Lines

Chuck

Moderator
Staff member
iI18Pzk.png

If you're having trouble with drawing trend lines, support and resistance levels then this indicator can help. Upon adding the indicator, it will plot several trend lines, support, and resistance channels based on critical pivot points of the stock. You can open the settings and change the value to change the number of trend lines shown. You can toggle this number back and forth to see different views of the trend.

Code:
# Fractal Pivots with Trend Lines
# Mobius
# V01.08.2012

input n = 4;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;

# Internal Script Reference
script LinePlot {
    input BarID = 0;
    input Value = 0;
    input BarOrigin = 0;
    def ThisBar = HighestAll(BarOrigin);
    def ValueLine = if BarOrigin == ThisBar
                then Value
                else Double.NaN;
    plot P = if ThisBar - BarID <= BarOrigin
             then HighestAll(ValueLine)
             else Double.NaN;
}
   def h = high;
   def l = low;
   def bar = barNumber();
   def PH;
   def PL;
   def hh = fold i = 1 to n + 1
            with p = 1
            while p
            do h > getValue(h, -i);
       PH = if (bar > n and
                h == highest(h, n) and
                hh)
            then h
            else double.NaN;
   def ll = fold j = 1 to n + 1
            with q = 1
            while q
            do l < getValue(low, -j);
       PL = if (bar > n and
                l == lowest(l, n) and
                ll)
            then l
            else double.NaN;
   def PHBar = if !isNaN(PH)
               then bar
               else PHBar[1];
   def PLBar = if !isNaN(PL)
               then bar
               else PLBar[1];
   def PHL = if !isNaN(PH)
             then PH
             else PHL[1];
   def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
   def PriorPH = if PHL != PHL[1]
                 then PHL[1]
                 else PriorPH[1];
   def PLL = if !isNaN(PL)
             then PL
             else PLL[1];
   def PriorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
   def PriorPL = if PLL != PLL[1]
                 then PLL[1]
                 else PriorPL[1];
   def HighPivots = bar >= highestAll(priorPHBar);
   def LowPivots = bar >= highestAll(priorPLBar);
   def FirstRpoint = if HighPivots
                     then bar - PHBar
                     else 0;
   def PriorRpoint = if HighPivots
                     then bar - PriorPHBar
                     else 0;
   def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
                       / (PHBar - PriorPHBar);
   def FirstSpoint = if LowPivots
                     then bar - PLBar
                     else 0;
   def PriorSpoint = if LowPivots
                     then bar - PriorPLBar
                     else 0;
   def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
                   / (PLBar - PriorPLBar);
   def RExtend = if bar == highestall(PHBar)
                 then 1
                 else RExtend[1];
   def SExtend = if bar == highestall(PLBar)
                 then 1
                 else SExtend[1];
  plot PriorPHLine = LinePlot(PriorRPoint, PriorPH, PriorPHBar);
       PriorPHLine.SetStyle(Curve.Firm);
       PriorPHLine.SetDefaultColor(Color.Red);
  plot PivotHighLine = LinePlot(FirstRPoint, PH, PHBar);
       PivotHighLine.SetStyle(Curve.Firm);
       PivotHighLine.SetDefaultColor(Color.Red);
  plot pivotHigh = if HighPivots
                   then PH
                   else double.NaN;
       pivotHigh.SetDefaultColor(GetColor(1));
       pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotHigh.setHiding(!showValues);
  plot RLine = pivotHigh;
       RLine.enableApproximation();
       RLine.SetDefaultColor(GetColor(7));
       RLine.SetStyle(Curve.Short_DASH);
  plot RExtension = if RExtend
                    then (bar - PHBar) * RSlope + PHL
                    else double.NaN;
       RExtension.SetStyle(Curve.Short_DASH);
       RExtension.SetDefaultColor(GetColor(7));
  plot PriorPLLine = LinePlot(PriorSPoint, PriorPL, PriorPLBar);
       PriorPLLine.SetStyle(Curve.Firm);
       PriorPLLine.SetDefaultColor(Color.Green);
  plot PivotLowLine = LinePlot(FirstSPoint, PL, PLBar);
       PivotLowLine.SetStyle(Curve.Firm);
       PivotLowLine.SetDefaultColor(Color.Green);
  plot pivotLow = if LowPivots
                   then PL
                   else double.NaN;
       pivotLow.SetDefaultColor(GetColor(1));
       pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
       pivotLow.setHiding(!showValues);
  plot SupportLine = pivotLow;
       SupportLine.enableApproximation();
       SupportLine.SetDefaultColor(GetColor(7));
       SUpportLine.SetStyle(Curve.Short_DASH);
  plot SupportExtension = if SExtend
                          then (bar - PLBar) * SSlope + PLL
                          else double.NaN;
       SupportExtension.SetDefaultColor(GetColor(7));
       SupportExtension.SetStyle(Curve.Short_DASH);
  plot BN = bar;
       BN.SetDefaultColor(GetColor(0));
       BN.setHiding(!showBarNumbers);
       BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
  plot PivotDot = if !isNaN(pivotHigh)
                  then pivotHigh
                  else if !isNaN(pivotLow)
                  then pivotLow
                  else double.NaN;
       pivotDot.SetDefaultColor(GetColor(7));
       pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
       pivotDot.SetLineWeight(3);

# End Code Pivots with Projections
 
Top