# Wolf Waves
# Mobius
# V01.05.22.2018
#Modifiede by Cwparker23
#https://youtu.be/oYo3i7m5Js0
# User Inputs
input n = 13;
input OUTSIDE_FIBS_hide = no;
input SHOW_LabelS = NO;
DefineGlobalColor("OUTER_FIBS",Color.LIGHT_GRAY);
def today = GetLastDay() == GetDay();
input averageType = AverageType.SIMPLE;
def ATR = MovingAverage(averageType, TrueRange(high, close, low), n);
# 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 x = BarNumber();
def xN = x == HighestAll(x);
# R1
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PivotH = if (x > 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 x
                  else PHBarOrigin[1];
def PHBarID = x - PHBarOrigin;
# R2
def R2PHValue = if PHBarOrigin != PHBarOrigin[1]
                then PHValue[1]
                else R2PHValue[1];
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
                    then PHBarOrigin[1]
                    else R2PHBarOrigin[1];
def R2PHBarID = x - R2PHBarOrigin;
# R3
def R3PHValue = if R2PHBarOrigin != R2PHBarOrigin[1]
                then R2PHValue[1]
                else R3PHValue[1];
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
                    then R2PHBarOrigin[1]
                    else R3PHBarOrigin[1];
def R3PHBarID = x - R3PHBarOrigin;
# S1
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PivotL = if (x > 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 x
                  else PLBarOrigin[1];
def PLBarID = x - PLBarOrigin;
# S2
def S2PLValue = if PLBarOrigin != PLBarOrigin[1]
                then PLValue[1]
                else S2PLValue[1];
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
                    then PLBarOrigin[1]
                    else S2PLBarOrigin[1];
def S2PLBarID = x - S2PLBarOrigin;
# S3
def S3PLValue = if S2PLBarOrigin != S2PLBarOrigin[1]
                then S2PLValue[1]
                else S3PLValue[1];
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
                    then S2PLBarOrigin[1]
                    else S3PLBarOrigin[1];
def S3PLBarID = x - S3PLBarOrigin;
# S4
def S4PLValue = if S3PLBarOrigin != S3PLBarOrigin[1]
                then S3PLValue[1]
                else S4PLValue[1];
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
                    then S3PLBarOrigin[1]
                    else S4PLBarOrigin[1];
def S4PLBarID = x - S4PLBarOrigin;
# S5
def S5PLValue = if S4PLBarOrigin != S4PLBarOrigin[1]
                then S4PLValue[1]
                else S5PLValue[1];
def S5PLBarOrigin = if S4PLBarOrigin != S4PLBarOrigin[1]
                    then S4PLBarOrigin[1]
                    else S5PLBarOrigin[1];
def S5PLBarID = x - S5PLBarOrigin;
# Plots
plot R1 = LinePlot(BarID = PHBarID,
                   Value = PHValue,
                   BarOrigin = PHBarOrigin);
R1.SetDefaultColor(Color.RED);
AddChartBubble(x == HighestAll(PHBarOrigin), PHValue, "R1", Color.RED, 1);
R1.SetLineWeight(3);
plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);
R2.SetDefaultColor(Color.RED);
AddChartBubble(x == HighestAll(R2PHBarOrigin), PHValue, "R2", Color.RED, 1);
plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
R3.SetDefaultColor(Color.RED);
AddChartBubble(x == HighestAll(R3PHBarOrigin), PHValue, "R3", Color.RED, 1);
plot S1 = LinePlot(BarID = PLBarID,
                   Value = PLValue,
                   BarOrigin = PLBarOrigin);
S1.SetDefaultColor(Color.WHITE);
S1.SetLineWeight(3);
AddChartBubble(x == HighestAll(PLBarOrigin), PLValue, "S1", Color.WHITE, 0);
plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
S2.SetDefaultColor(Color.WHITE);
AddChartBubble(x == HighestAll(S2PLBarOrigin), PLValue, "S2", Color.WHITE, 0);
plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
S3.SetDefaultColor(Color.WHITE);
AddChartBubble(x == HighestAll(S3PLBarOrigin), PLValue, "S3", Color.WHITE, 0);
# Trend Line
plot SupportLine2 = if x == HighestAll(S2PLBarOrigin)
                    then S2
                    else if x == HighestAll(PLBarOrigin)
                         then S1  #Parentlow
                         else Double.NaN;
SupportLine2.EnableApproximation();
SupportLine2.SetDefaultColor(Color.GRAY);
SupportLine2.SetLineWeight(1);
SupportLine2.SetStyle(Curve.LONG_DASH);
def slope2 = (S2 - S1) /
             (HighestAll(S2PLBarOrigin) - PLBarOrigin);
plot ExtLine2 = if x >= S2PLBarOrigin
                then (x - HighestAll(S2PLBarOrigin)) * slope2 + S2
                else Double.NaN;
ExtLine2.EnableApproximation();
ExtLine2.SetDefaultColor(Color.GRAY);
ExtLine2.SetLineWeight(1);
ExtLine2.SetStyle(Curve.LONG_DASH);
plot SupportLine3 = if x == HighestAll(R3PHBarOrigin)
                    then R3
                    else if x == PHBarOrigin
                         then R1
                         else Double.NaN;
SupportLine3.EnableApproximation();
SupportLine3.SetDefaultColor(Color.GRAY);
SupportLine3.SetLineWeight(1);
SupportLine3.SetStyle(Curve.LONG_DASH);
def slope3 = (R1 - R3) /
             (HighestAll(PHBarOrigin) - R3PHBarOrigin);
plot ExtLine3 = if x >= R3PHBarOrigin
                then (x - HighestAll(R3PHBarOrigin)) * slope3 + R3
                else Double.NaN;
ExtLine3.EnableApproximation();
ExtLine3.SetDefaultColor(Color.GRAY);
ExtLine3.SetLineWeight(1);
ExtLine3.SetStyle(Curve.LONG_DASH);
input HideCloud = YES;
DefineGlobalColor("ORB", Color.DARK_GRAY);
AddCloud( if !HideCloud then R1   else Double.NaN, S1 , GlobalColor("ORB"), GlobalColor("ORB"));
DEF LOWER_LOW = IF S1 < S2 THEN S1 ELSE IF S1 > S2 THEN S2 ELse LOWER_LOW[1];
DEF HIGHER_HIGH = IF R1 < R2 THEN R2 ELSE IF R1 > R2 THEN R1 ELSE HIGHER_HIGH[1];
PLOT HIGHER_HIGH1 = HIGHER_HIGH;
PLOT LOWER_LOW1 = LOWER_LOW;
def range =  HIGHER_HIGH - LOWER_LOW ;
PLOT FIFTY = HIGHER_HIGH - (range*.50);
plot f10 = HIGHER_HIGH - (RANGE * 0.10);
plot F90 =  HIGHER_HIGH -(RANGE *.90);
plot F236 =  HIGHER_HIGH -(RANGE *.764);
plot F786 =  HIGHER_HIGH -(RANGE *.214);
F786.setlineWeight(3);
F236.setlineWeight(3);
plot F618 =  HIGHER_HIGH -(RANGE *.382);
F618.setlineWeight(1);
F618.SetDefaultColor(Color.MAGENTA);
plot F382 =  HIGHER_HIGH -(RANGE *.618);
F382.setlineWeight(1);
F382.SetDefaultColor(Color.MAGENTA);
f10.SetPaintingStrategy(PaintingStrategy.LINE);
f10.SetDefaultColor(Color.CYAN);
f10.setlineWeight(3);
FIFTY.SetPaintingStrategy(PaintingStrategy.LINE);
FIFTY.SetDefaultColor(Color.ORANGE);
FIFTY.setlineWeight(3);
F90.SetPaintingStrategy(PaintingStrategy.LINE);
F90.SetDefaultColor(Color.CYAN);
F90.setlineWeight(3);
F236.SetPaintingStrategy(PaintingStrategy.LINE);
F236.SetDefaultColor(Color.lIME);
F236.setlineWeight(3);
F786.SetPaintingStrategy(PaintingStrategy.LINE);
F786.SetDefaultColor(Color.lIME);
F786.setlineWeight(3);
####################################################
PLOT FIFTYU = HIGHER_HIGH + (RANGE * 0.50);
plot f10U = HIGHER_HIGH + (RANGE * 0.10);
plot F90U =  HIGHER_HIGH +(RANGE *.90);
plot F236U =  HIGHER_HIGH +(RANGE *.764);
plot F786U =  HIGHER_HIGH +(RANGE *.214);
F786U.setlineWeight(3);
F236U.setlineWeight(3);
plot F618U =  HIGHER_HIGH +(RANGE *.382);
F618U.setlineWeight(1);
F618U.SetDefaultColor(Color.MAGENTA);
plot F382U =  HIGHER_HIGH +(RANGE *.618);
F382U.setlineWeight(1);
F382U.SetDefaultColor(Color.MAGENTA);
f10U.SetPaintingStrategy(PaintingStrategy.LINE);
f10U.SetDefaultColor(Color.CYAN);
f10U.setlineWeight(3);
FIFTYU.SetPaintingStrategy(PaintingStrategy.LINE);
FIFTYU.SetDefaultColor(Color.ORANGE);
FIFTYU.setlineWeight(3);
F90U.SetPaintingStrategy(PaintingStrategy.LINE);
F90U.SetDefaultColor(Color.CYAN);
F90U.setlineWeight(3);
F236U.SetPaintingStrategy(PaintingStrategy.LINE);
F236U.SetDefaultColor(Color.lIME);
F236U.setlineWeight(3);
F786U.SetPaintingStrategy(PaintingStrategy.LINE);
F786U.SetDefaultColor(Color.lIME);
F786U.setlineWeight(3);
####################################################
PLOT FIFTYL = LOWER_LOW - (RANGE * 0.50);
plot f10L = LOWER_LOW - (RANGE * 0.10);
plot F90L =  LOWER_LOW - (RANGE *.90);
plot F236L =  LOWER_LOW - (RANGE *.764);
plot F786L =  LOWER_LOW -(RANGE *.214);
F786L.setlineWeight(3);
F236L.setlineWeight(3);
plot F618L =  LOWER_LOW -(RANGE *.382);
F618L.setlineWeight(1);
F618L.SetDefaultColor(Color.MAGENTA);
plot F382L =  LOWER_LOW -(RANGE *.618);
F382L.setlineWeight(1);
F382L.SetDefaultColor(Color.MAGENTA);
f10L.SetPaintingStrategy(PaintingStrategy.LINE);
f10L.SetDefaultColor(Color.CYAN);
f10L.setlineWeight(3);
FIFTYL.SetPaintingStrategy(PaintingStrategy.LINE);
FIFTYL.SetDefaultColor(Color.ORANGE);
FIFTYL.setlineWeight(3);
F90L.SetPaintingStrategy(PaintingStrategy.LINE);
F90L.SetDefaultColor(Color.CYAN);
F90L.setlineWeight(3);
F236L.SetPaintingStrategy(PaintingStrategy.LINE);
F236L.SetDefaultColor(Color.lIME);
F236L.setlineWeight(3);
F786L.SetPaintingStrategy(PaintingStrategy.LINE);
F786L.SetDefaultColor(Color.lIME);
F786L.setlineWeight(3);
####################################################
DEF F5U = range * .05;
def t3u = FIFTY + F5U;
def t3d = FIFTY - F5U;
input HideCloud2 = no;
DefineGlobalColor("NINTY", Color.DARK_RED);
AddCloud( if !HideCloud2 then FIFTY    else Double.NaN, t3d , GlobalColor("NINTY"), GlobalColor("NINTY"));
DefineGlobalColor("TEN", Color.DARK_GREEN);
AddCloud( if !HideCloud2 then FIFTY   else Double.NaN, t3u  , GlobalColor("TEN"), GlobalColor("TEN"));
####################################################
def PIVOT_RANGE = HIGHER_HIGH - LOWER_LOW;
def DTRpct = Round((PIVOT_RANGE / ATR) * 100, 0);
AddLabel (yes, "PIVOT_TR " + Round (PIVOT_RANGE , 2) + " vs ATR " + Round (ATR, 2) + "  " + Round (DTRpct, 0) + "%", (if DTRpct <= 70 then Color.GREEN else if DTRpct >= 90 then Color.RED else Color.ORANGE));
def ADR = Average(high (period = "DAY")[1], n) - Average(low (period = "DAY")[1], n);