Fractal Pivots

Chuck

Moderator
Staff member
I1qbm10.png

Code:
#  
# Simple Fractal Pivot Study - (base for any pivot study)
# Mobius
# Posted to Chat Window First time 05.07.2011

# User Inputs
input n = 21;

# 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
             then ValueLine
             else Double.NaN;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
# Pivot High Variables
def hh = fold i = 1 to n + 1 
         with p = 1
         while p 
         do h > GetValue(h, -1);
def PivotH = if (bar > n and 
                 h == Highest(h, n) and 
                 hh)
            then h 
            else Double.NaN;
def PivotHL = if PivotH
              then l
              else PivotHL[1];
def PHValue = if !IsNaN(PivotH) 
              then PivotH 
              else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH) 
                  then bar 
                  else PHBarOrigin[1];
def PHBarID = bar - PHbarOrigin;
# Pivot Low Variables
def ll = fold j = 1 to n + 1 
         with q = 1
         while q 
         do l < GetValue(l, -1);
def PivotL = if (bar > n and 
                 l == Lowest(l, n) and
                 ll)
             then l 
             else Double.NaN;
def PLValue = if !IsNaN(PivotL) 
              then PivotL 
              else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL) 
                  then bar 
                  else PLBarOrigin[1];
def PLBarID = bar - PLBarOrigin;
# Plots
plot R1 = LinePlot(BarID = PHBarID, 
                   Value = PHValue, 
                   BarOrigin = PHBarOrigin);
     R1.SetDefaultColor(Color.Green);
plot S1 = LinePlot(BarID = PLBarID, 
                   Value = PLValue, 
                   BarOrigin = PLBarOrigin).P;
     S1.SetDefaultColor(Color.Red);
# End Code Simple Fractal Pivot Study

#
 
Last edited:
Top