```
# Fractal Pivot Array 11 deep - (Start For Elliott Wave Code)
# Mobius
# V02.01.2017
# Added some information on Basic Wave Structure and labels to hold wave data started
# User Inputs
input n = 10;
input WaveLabel = yes;
input ShowWaves = yes;
# 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;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);
# Parent High
def ParentHigh = HighestAll(h);
def ParentHBarOrigin = if h == ParentHigh
then bar
else ParentHBarOrigin[1];
def ParentHBarID = bar - ParentHBarOrigin;
# R1
def hh = fold i = 1 to n + 1
with p = 1
while p
do h > GetValue(h, -i);
def PivotH = if (bar > n and
h == Highest(h, n) and
hh)
then h
else Double.NaN;
def PHValue = if !IsNaN(PivotH)
then PivotH
else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
then bar
else PHBarOrigin[1];
def PHBarID = bar - PHBarOrigin;
# R2
def R2PHValue = if PHValue != PHValue[1]
then PHValue[1]
else R2PHValue[1];
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
then PHBarOrigin[1]
else R2PHBarOrigin[1];
def R2PHBarID = bar - R2PHBarOrigin;
# R3
def R3PHValue = if R2PHValue != R2PHValue[1]
then R2PHValue[1]
else R3PHValue[1];
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
then R2PHBarOrigin[1]
else R3PHBarOrigin[1];
def R3PHBarID = bar - R3PHBarOrigin;
# R4
def R4PHValue = if R3PHValue != R3PHValue[1]
then R3PHValue[1]
else R4PHValue[1];
def R4PHBarOrigin = if R3PHBarOrigin != R3PHBarOrigin[1]
then R3PHBarOrigin[1]
else R4PHBarOrigin[1];
def R4PHBarID = bar - R4PHBarOrigin;
# R5
def R5PHValue = if R4PHValue != R4PHValue[1]
then R4PHValue[1]
else R5PHValue[1];
def R5PHBarOrigin = if R4PHBarOrigin != R4PHBarOrigin[1]
then R4PHBarOrigin[1]
else R5PHBarOrigin[1];
def R5PHBarID = bar - R5PHBarOrigin;
# R6
def R6PHValue = if R5PHValue != R5PHValue[1]
then R5PHValue[1]
else R6PHValue[1];
def R6PHBarOrigin = if R5PHBarOrigin != R5PHBarOrigin[1]
then R5PHBarOrigin[1]
else R6PHBarOrigin[1];
def R6PHBarID = bar - R6PHBarOrigin;
# R5
def R7PHValue = if R6PHValue != R6PHValue[1]
then R6PHValue[1]
else R7PHValue[1];
def R7PHBarOrigin = if R6PHBarOrigin != R6PHBarOrigin[1]
then R6PHBarOrigin[1]
else R7PHBarOrigin[1];
def R7PHBarID = bar - R7PHBarOrigin;
# R8
def R8PHValue = if R7PHValue != R7PHValue[1]
then R7PHValue[1]
else R8PHValue[1];
def R8PHBarOrigin = if R7PHBarOrigin != R7PHBarOrigin[1]
then R7PHBarOrigin[1]
else R8PHBarOrigin[1];
def R8PHBarID = bar - R8PHBarOrigin;
# R9
def R9PHValue = if R8PHValue != R8PHValue[1]
then R8PHValue[1]
else R9PHValue[1];
def R9PHBarOrigin = if R8PHBarOrigin != R8PHBarOrigin[1]
then R8PHBarOrigin[1]
else R9PHBarOrigin[1];
def R9PHBarID = bar - R9PHBarOrigin;
# R10
def R10PHValue = if R9PHValue != R9PHValue[1]
then R9PHValue[1]
else R10PHValue[1];
def R10PHBarOrigin = if R9PHBarOrigin != R9PHBarOrigin[1]
then R9PHBarOrigin[1]
else R10PHBarOrigin[1];
def R10PHBarID = bar - R10PHBarOrigin;
# Parent Low
def ParentLow = LowestAll(l);
def ParentLBarOrigin = if l == ParentLow
then bar
else ParentLBarOrigin[1];
def ParentLBarID = bar - ParentLBarOrigin;
# S1
def ll = fold j = 1 to n + 1
with q = 1
while q
do l < GetValue(l, -j);
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;
# S2
def S2PLValue = if PLValue != PLValue[1]
then PLValue[1]
else S2PLValue[1];
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
then PLBarOrigin[1]
else S2PLBarOrigin[1];
def S2PLBarID = bar - S2PLBarOrigin;
# S3
def S3PLValue = if S2PLValue != S2PLValue[1]
then S2PLValue[1]
else S3PLValue[1];
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
then S2PLBarOrigin[1]
else S3PLBarOrigin[1];
def S3PLBarID = bar - S3PLBarOrigin;
# S4
def S4PLValue = if S3PLValue != S3PLValue[1]
then S3PLValue[1]
else S4PLValue[1];
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
then S3PLBarOrigin[1]
else S4PLBarOrigin[1];
def S4PLBarID = bar - S4PLBarOrigin;
# S5
def S5PLValue = if S4PLValue != S4PLValue[1]
then S4PLValue[1]
else S5PLValue[1];
def S5PLBarOrigin = if S4PLBarOrigin != S4PLBarOrigin[1]
then S4PLBarOrigin[1]
else S5PLBarOrigin[1];
def S5PLBarID = bar - S5PLBarOrigin;
# S6
def S6PLValue = if S5PLValue != S5PLValue[1]
then S5PLValue[1]
else S6PLValue[1];
def S6PLBarOrigin = if S5PLBarOrigin != S5PLBarOrigin[1]
then S5PLBarOrigin[1]
else S6PLBarOrigin[1];
def S6PLBarID = bar - S6PLBarOrigin;
# S7
def S7PLValue = if S6PLValue != S6PLValue[1]
then S6PLValue[1]
else S7PLValue[1];
def S7PLBarOrigin = if S6PLBarOrigin != S6PLBarOrigin[1]
then S6PLBarOrigin[1]
else S7PLBarOrigin[1];
def S7PLBarID = bar - S7PLBarOrigin;
# S8
def S8PLValue = if S7PLValue != S7PLValue[1]
then S7PLValue[1]
else S8PLValue[1];
def S8PLBarOrigin = if S7PLBarOrigin != S7PLBarOrigin[1]
then S7PLBarOrigin[1]
else S8PLBarOrigin[1];
def S8PLBarID = bar - S8PLBarOrigin;
# S9
def S9PLValue = if S8PLValue != S8PLValue[1]
then S8PLValue[1]
else S9PLValue[1];
def S9PLBarOrigin = if S8PLBarOrigin != S8PLBarOrigin[1]
then S8PLBarOrigin[1]
else S9PLBarOrigin[1];
def S9PLBarID = bar - S9PLBarOrigin;
# S10
def S10PLValue = if S9PLValue != S9PLValue[1]
then S9PLValue[1]
else S10PLValue[1];
def S10PLBarOrigin = if S9PLBarOrigin != S9PLBarOrigin[1]
then S9PLBarOrigin[1]
else S10PLBarOrigin[1];
def S10PLBarID = bar - S10PLBarOrigin;
# Plots
plot PR1 = LinePlot(BarID = ParentHBarID,
Value = ParentHigh,
BarOrigin = ParentHBarOrigin);
PR1.SetDefaultColor(Color.GREEN);
AddChartBubble(Bar == HighestAll(ParentHBarOrigin), ParentHigh, "B", Color.Yellow, 1);
##############################################
#Edit by Svanoy
#Plot arrow at Parent Bar "B".
plot PR1P = if Bar == HighestAll(ParentHBarOrigin) then ParentHigh else Double.NaN;
PR1P.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PR1P.SetDefaultColor(Color.Yellow);
PR1P.SetLineWeight(3);
##############################################
plot R1 = LinePlot(BarID = PHBarID,
Value = PHValue,
BarOrigin = PHBarOrigin);
R1.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(PHBarOrigin), PHvalue, "R1", color.green, 1);
##############################################
#Edit by Svanoy
#Plot arrow atan "R1".
plot R1P = if Bar == HighestAll(PHBarOrigin) then PHvalue else Double.NaN;
R1P.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
R1P.SetDefaultColor(Color.GREEN);
R1P.SetLineWeight(3);
##############################################
plot R2 = LinePlot(BarID = R2PHBarID,
Value = R2PHValue,
BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R2PHBarOrigin), PHvalue, "R2", color.green, 1);
plot R3 = LinePlot(BarID = R3PHBarID,
Value = R3PHValue,
BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R3PHBarOrigin), PHvalue, "R3", color.green, 1);
plot R4 = LinePlot(BarID = R4PHBarID,
Value = R4PHValue,
BarOrigin = R4PHBarOrigin);
R4.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R4PHBarOrigin), PHvalue, "R4", color.green, 1);
plot R5 = LinePlot(BarID = R5PHBarID,
Value = R5PHValue,
BarOrigin = R5PHBarOrigin);
R5.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R5PHBarOrigin), PHvalue, "R5", color.green, 1);
plot R6 = LinePlot(BarID = R6PHBarID,
Value = R6PHValue,
BarOrigin = R6PHBarOrigin);
R6.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R6PHBarOrigin), PHvalue, "R6", color.green, 1);
plot R7 = LinePlot(BarID = R7PHBarID,
Value = R7PHValue,
BarOrigin = R7PHBarOrigin);
R7.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R7PHBarOrigin), PHvalue, "R7", color.green, 1);
plot R8 = LinePlot(BarID = R8PHBarID,
Value = R8PHValue,
BarOrigin = R8PHBarOrigin);
R8.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R8PHBarOrigin), PHvalue, "R8", color.green, 1);
plot R9 = LinePlot(BarID = R9PHBarID,
Value = R9PHValue,
BarOrigin = R9PHBarOrigin);
R9.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R9PHBarOrigin), PHvalue, "R9", color.green, 1);
plot R10 = LinePlot(BarID = R10PHBarID,
Value = R10PHValue,
BarOrigin = R10PHBarOrigin);
R10.SetDefaultColor(Color.GREEN);
addChartBubble(Bar == HighestAll(R10PHBarOrigin), PHvalue, "R10", color.green, 1);
plot PS1 = LinePlot(BarID = ParentLBarID,
Value = ParentLow,
BarOrigin = ParentLBarOrigin);
PS1.SetDefaultColor(Color.RED);
AddChartBubble(Bar == HighestAll(ParentLBarOrigin), ParentLow, "A", Color.Yellow, 0);
##############################################
#Edit by Svanoy
#Plot arrow at Parent Bar "A".
plot PS1P = if Bar == HighestAll(ParentLBarOrigin) then ParentLow else Double.NaN;
PS1P.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
PS1P.SetDefaultColor(Color.YELLOW);
PS1P.SetLineWeight(3);
##############################################
plot S1 = LinePlot(BarID = PLBarID,
Value = PLValue,
BarOrigin = PLBarOrigin);
S1.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(PLBarOrigin), PLvalue, "S1", color.red, 0);
##############################################
#Edit by Svanoy
#Plot arrow at "S1"
plot S1P = if Bar == HighestAll(PLBarOrigin) then PLvalue else Double.NaN;
S1P.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
S1P.SetDefaultColor(Color.RED);
S1P.SetLineWeight(3);
##############################################
plot S2 = LinePlot(BarID = S2PLBarID,
Value = S2PLValue,
BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S2PLBarOrigin), PLvalue, "S2", color.red, 0);
plot S3 = LinePlot(BarID = S3PLBarID,
Value = S3PLValue,
BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S3PLBarOrigin), PLvalue, "S3", color.red, 0);
plot S4 = LinePlot(BarID = S4PLBarID,
Value = S4PLValue,
BarOrigin = S4PLBarOrigin);
S4.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S4PLBarOrigin), PLvalue, "S4", color.red, 0);
plot S5 = LinePlot(BarID = S5PLBarID,
Value = S5PLValue,
BarOrigin = S5PLBarOrigin);
S5.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S5PLBarOrigin), PLvalue, "S5", color.red, 0);
plot S6 = LinePlot(BarID = S6PLBarID,
Value = S6PLValue,
BarOrigin = S6PLBarOrigin);
S6.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S6PLBarOrigin), PLvalue, "S6", color.red, 0);
plot S7 = LinePlot(BarID = S7PLBarID,
Value = S7PLValue,
BarOrigin = S7PLBarOrigin);
S7.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S7PLBarOrigin), PLvalue, "S7", color.red, 0);
plot S8 = LinePlot(BarID = S8PLBarID,
Value = S8PLValue,
BarOrigin = S8PLBarOrigin);
S8.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S8PLBarOrigin), PLvalue, "S8", color.red, 0);
plot S9 = LinePlot(BarID = S9PLBarID,
Value = S9PLValue,
BarOrigin = S9PLBarOrigin);
addChartBubble(Bar == HighestAll(S9PLBarOrigin), PLvalue, "S9", color.red, 0);
S9.SetDefaultColor(Color.RED);
plot S10 = LinePlot(BarID = S10PLBarID,
Value = S10PLValue,
BarOrigin = S10PLBarOrigin);
S10.SetDefaultColor(Color.RED);
addChartBubble(Bar == HighestAll(S10PLBarOrigin), PLvalue, "S10", color.red, 0);
#######################################################
#Edit by Svanoy
#Labels switching between last parent support or resistance plotted
input Lookback_Period = 15;#<=== (Must be 11 or higher).
#Labels switching between last support or resistance plotted.
def SRSwitch = If PHBarOrigin == PHBarOrigin[1] and PLBarOrigin != PLBarOrigin[1] within Lookback_Period bars then 1
else If PHBarOrigin != PHBarOrigin[1] within Lookback_Period bars and PLBarOrigin == PLBarOrigin[1] then 0
else 2;
def PSRSwitch = If ParentHBarOrigin == ParentHBarOrigin[1] and ParentLBarOrigin != ParentLBarOrigin[1] within Lookback_Period bars and SRSwitch==1 then 1
else If ParentHBarOrigin != ParentHBarOrigin[1] within Lookback_Period bars and ParentLBarOrigin == ParentLBarOrigin[1] and SRSwitch==0 then 0
else 2;
AddLabel(yes, If PSRSwitch==1 then "A" else If PSRSwitch==0 then "B" else "Waiting", If PSRSwitch==1 or PSRSwitch==0 then color.YELLOW else color.BLACK);
AddLabel(yes, If PSRSwitch==1 then ParentLow else If PSRSwitch==0 then ParentHigh else 0, If PSRSwitch==2 then color.BLACK else color.YELLOW);
AddLabel(yes, If SRSwitch==1 then "S1" else If SRSwitch==0 then "R1" else "Waiting", If SRSwitch==1 then color.RED else If SRSwitch==0 then color.GREEN else color.BLACK);
AddLabel(yes, If SRSwitch==1 then PLValue else If SRSwitch==0 then PHValue else 0, If SRSwitch==1 then color.RED else If SRSwitch==0 then color.GREEN else color.BLACK);
#######################################################
# End Code Fractal Array 11 Deep
# Trend Line
def MaxSlopeBar = Max(HighestAll(PLBarOrigin), ParentLBarOrigin);
def MinSlopeBar = Min(LowestAll(PLBarOrigin), ParentLBarOrigin);
def SSlope = (HighestAll(PLValue) - ParentLow) /
(MaxSlopeBar - MinSlopeBar);
# (HighestAll(PLBarOrigin) - ParentLBarOrigin);
def SExtend = if bar == MaxSlopeBar #HighestAll(PLBarOrigin)
then 1
else SExtend[1];
plot SupportLine = if low == ParentLow
then ParentLow
else if low == HighestAll(PLvalue)
then PLvalue
else double.nan;
SupportLine.EnableApproximation();
SupportLine.SetDefaultColor(Color.Gray);
SupportLine.SetLineWeight(2);
SupportLine.SetStyle(Curve.LONG_DASH);
plot SupportExtension = if SExtend
then (bar - PLBarOrigin) * SSlope + PLvalue
else Double.NaN;
SupportExtension.SetDefaultColor(Color.Gray);
SupportExtension.SetStyle(Curve.LONG_DASH);
SupportExtension.SetLineWeight(2);
#AddChartBubble(WaveLabel and bar == HighestAll(bar),
# HighestAll(High),
#"Basic Elliott Wave Rules:" + "\n" +
#"Wave (2) cannot retrace past the start of Wave (1)" + "\n" +
#"Wave (3) cannot be the shortest wave in a 5 wave sequence." + "\n" +
#"Wave (4) cannot retrace into Wave (1)" + "\n",
#color.white, 1);
plot waves = if !isNaN(PivotH)
then PivotH
else if !isNaN(PivotL)
then PivotL
else double.nan;
waves.SetStyle(Curve.Firm);
waves.EnableApproximation();
waves.SetDefaultColor(Color.Cyan);
waves.SetLineWeight(2);
waves.SetHiding(!ShowWaves);
```