Automatic Long and Short Fibonacci Level Indicators for ThinkorSwim

chewie76

Well-known member
VIP Enthusiast
All other Automatic Fib indicators look at the past, measure a previous high and previous low and give you levels. This indicator looks into the future and gives you possible high and low price levels. The Automatic Fibonacci Levels are two indicators, one for long positions and one for short positions. Up and Down arrows show entry signals when price crosses the 78.6% level. Entry signal alerts are included. This can be used for both swing trading and day trading based on the timeframe of your chart.

If price continues and breaks the 100% level, a new set of lines will be automatically drawn. Note, no guarantee price will hit the target.

Here are some examples.

Long position in /RTY 4 hr.
TIpDhV0.jpg


Long position in WMT daily.
gEGf6ry.jpg


Long position in JNJ daily.
mjvPGHG.jpg


Short position in /SI daily.
bIETPeg.jpg


Short position in HRZN 4 hr.
pGZPUTM.jpg


Shareable Link:
Long position:

http://tos.mx/AyuB4pQ

Short position:
http://tos.mx/1IJ4a4L

Long Position Code:
Code:
#Automatic Fibonacci Long Levels
#based on Mobius's Fractal Pivot Strategy
#developed by Chewie76 on 8/27/2021

# User Inputs
input n = 20;
input FractalEnergyLength = 8;
input FractalEnergyThreshold = .68;
input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
input LabelsOn = yes;
input AlertsOn = yes;

# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def TS = TickSize();
def nan = double.nan;
def ATR = Round((MovingAverage(AvgType, TrueRange(h, c, l), nATR)) / TS, 0) * TS;
def risk = if Between(c, 0, 1500)
           then ATR
           else if Between(c, 1500, 3500)
           then 2
           else if Between(c, 3500, 5500)
                then 4
           else 6;
def FE = Log(Sum((Max(h, c[1]) - Min(l, c[1])), FractalEnergyLength) /
        (Highest(h, FractalEnergyLength) - Lowest(l, FractalEnergyLength)))
            / Log(FractalEnergyLength);
# Parent Aggregation Pivot High
# Pivot High Variables
def p_hh = fold i = 1 to n + 1
           with p = 1
           while p
           do h > GetValue(h, -1);
def p_PivotH = if (bar > n and
                   h == Highest(h, n) and
                   p_hh)
               then h
               else NaN;
def p_PHValue = if !IsNaN(p_PivotH)
                then p_PivotH
                else p_PHValue[1];
def p_PHBar = if !IsNaN(p_PivotH)
                    then bar
                    else nan;
# Pivot High and Pivot High Exit Variables
# Pivot High Variables
def hh = fold ii = 1 to n + 1
         with pp = 1
         while pp
         do h > GetValue(h, -1);
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 PHBar = if !IsNaN(PivotH)
                  then bar
                  else nan;
# Pivot High Exit Variables
def PHExit = if (bar > n and
              h == Highest(h, n) and
              hh)
             then if l[1] < l
                  then l[1]
                  else fold r = 0 to 20
                       with a = NaN
                       while IsNaN(a)
                       do if GetValue(l[1], r) < l
                          then GetValue(l[1], r)
                          else NaN
            else Double.NaN;
def PHExitValue = if !IsNaN(PHExit)
                  then PHExit
                  else PHExitValue[1];
def PHExitBar = if (bar > n and
                    h == Highest(h, n) and
                    hh)
                then if l[1] < l
                then bar - 1
                else fold d = 0 to 20
                     with y = NaN
                     while IsNaN(y)
                     do if GetValue(l[1], d) < l
                        then GetValue(bar - 1, d)
                        else NaN
                else NaN;
# Pivot Low and Pivot Low Entry Variables
# Parent Pivot Low Variables
def p_ll = fold j = 1 to n + 1
           with q = 1
           while q
           do l < GetValue(l, -1);
def p_PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 p_ll)
             then l
             else NaN;
def p_PLValue = if !IsNaN(p_PivotL)
              then p_PivotL
              else p_PLValue[1];
def p_PLBar = if !IsNaN(p_PivotL)
              then bar
              else nan;
# Pivot Low Variables
def ll = fold jj = 1 to n + 1
         with qq = 1
         while qq
         do l < GetValue(l, -1);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBar = if !IsNaN(PivotL)
            then bar
            else nan;
# Pivot Low Entry Variables
def PLEntry = if (bar > n and
                  l == Lowest(l, n) and
                  ll)
              then if h[1] > h
              then h[1]
              else fold t = 0 to 20
                   with w = NaN
                   while IsNaN(w)
                   do if GetValue(h[1], t) > h
                      then GetValue(h[1], t)
                      else NaN
              else NaN;
def PLEntryValue = if !IsNaN(PLEntry)
                   then PLEntry
                   else PLEntryValue[1];
def PLEntryBar =  if (bar > n and
                  l == Lowest(l, n) and
                  ll)
                  then if h[1] > h
                       then bar - 1
                       else fold u = 0 to 20
                            with z = NaN
                            while IsNaN(z)
                            do if GetValue(h[1], u) > h
                               then GetValue(bar - 1, u)
                               else NaN
              else NaN;
# Plots

plot F_100 =  if bar >= HighestAll(PLBar)
          then HighestAll(if isNaN(c[-1])
                          then PLValue
                          else nan)
          else nan;
F_100.SetDefaultColor(Color.dark_red);
F_100.SetLineWeight(2);
def F_0100 =(if isNaN(F_100[1]) then F_100 else Double.NaN);
addchartBubble(LabelsOn and F_100, F_0100,"100%",color.dark_red);

plot F_786 = if bar >= HighestAll(PLEntryBar)
                    then HighestAll(if isNaN(c[-1])
                                    then PLEntryValue
                                    else nan)
                    else nan;
F_786.SetDefaultColor(Color.light_red);
F_786.SetLineWeight(2);
def F_0786 =(if isNaN(F_786[1]) then F_786 else Double.NaN);
addchartBubble(LabelsOn and F_786, F_0786,"78.6%",color.light_red);

plot priceLine = HighestAll(if IsNaN(c[-1])
                            then c
                            else Double.NaN);
priceLine.SetStyle(Curve.SHORT_DASH);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.CYAN);
plot UpArrow = if c crosses above F_786 and FE > .5
               then l
               else Double.NaN;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow.SetLineWeight(5);
UpArrow.SetDefaultColor(Color.GREEN);

#Fib levels

plot F_0 = (F_786-F_100) * 4.673 + F_100;
F_0.setdefaultcolor(color.red);
F_0.setlineweight(2);
def F_00 =(if isNaN(F_0[1]) then F_0 else Double.NaN);
addchartBubble(LabelsOn and F_0, F_00,"0%: " + asDollars(F_00),color.red);

plot F_124 = (F_0 - F_100)/1.14155+ F_100;
F_124.setdefaultcolor(color.dark_orange);
F_124.setlineweight(2);
def F_0124 =(if isNaN(F_124[1]) then F_124 else Double.NaN);
addchartBubble(LabelsOn and F_124, F_0124,"12.4%",color.dark_orange);

plot F_236 = (F_0 - F_100)/1.3089+ F_100;
F_236.setdefaultcolor(color.plum);
F_236.setlineweight(2);
def F_0236 = (if isNaN(F_236[1]) then F_236 else Double.NaN);
addchartBubble(LabelsOn and F_236, F_0236,"23.6%",color.plum);

plot F_382 = (F_0 - F_100)/1.618+ F_100;
F_382.setdefaultcolor(color.cyan);
F_382.setlineweight(2);
def F_0382 =(if isNaN(F_382[1]) then F_382 else Double.NaN);
addchartBubble(LabelsOn and F_382, F_0382,"38.2%",color.cyan);

plot F_50 = (F_0 - F_100)/2 + F_100;
F_50.setdefaultcolor(color.green);
F_50.setlineweight(2);
def F_050 =(if isNaN(F_50[1]) then F_50 else Double.NaN);
addchartBubble(LabelsOn and F_50, F_050,"50%",color.green);

plot F_618 = (F_0 - F_100)/2.618+ F_100;
F_618.setdefaultcolor(color.yellow);
F_618.setlineweight(2);
def F_0618 =(if isNaN(F_618[1]) then F_618 else Double.NaN);
addchartBubble(LabelsOn and F_618, F_0618,"61.8%",color.yellow);

plot F_886 = (F_786 - F_100)/1.88+ F_100;
F_886.setdefaultcolor(color.violet);
F_886.setlineweight(2);
def F_0886 =(if isNaN(F_886[1]) then F_886 else Double.NaN);
addchartBubble(LabelsOn and F_886, F_0886,"88.6%",color.violet);

plot FE_272 = (F_0 - F_100)*1.272 + F_100;
FE_272.setdefaultcolor(color.yellow);
FE_272.SetStyle(Curve.LONG_DASH);
FE_272.setlineweight(2);
def F_0272 =(if isNaN(FE_272[1]) then FE_272 else Double.NaN);
addchartBubble(LabelsOn and FE_272, F_0272,"-27.2%",color.yellow);

plot FE_50 = (F_0 - F_100)*1.5 + F_100;
FE_50.setdefaultcolor(color.GREEN);
FE_50.SetStyle(Curve.LONG_DASH);
FE_50.setlineweight(2);
def FE_050 =(if isNaN(FE_50[1]) then FE_50 else Double.NaN);
addchartBubble(LabelsOn and FE_50, FE_050,"-50%",color.GREEN);

# Alerts
Alert(AlertsOn and UpArrow, " ", Alert.Bar, Sound.ding);

Short Position Code:
Code:
#Automatic Fibonacci Short Levels
#based on Mobius's Fractal Pivot Strategy
#developed by Chewie76 on 8/27/2021

# User Inputs
input n = 20;
input FractalEnergyLength = 8;
input FractalEnergyThreshold = .68;
input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
input LabelsOn = yes;
input AlertsOn = yes;

# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def TS = TickSize();
def nan = double.nan;
def ATR = Round((MovingAverage(AvgType, TrueRange(h, c, l), nATR)) / TS, 0) * TS;
def risk = if Between(c, 0, 1500)
           then ATR
           else if Between(c, 1500, 3500)
           then 2
           else if Between(c, 3500, 5500)
                then 4
           else 6;
def FE = Log(Sum((Max(h, c[1]) - Min(l, c[1])), FractalEnergyLength) /
        (Highest(h, FractalEnergyLength) - Lowest(l, FractalEnergyLength)))
            / Log(FractalEnergyLength);
# Parent Aggregation Pivot High
# Pivot High Variables
def p_hh = fold i = 1 to n + 1
           with p = 1
           while p
           do h > GetValue(h, -1);
def p_PivotH = if (bar > n and
                   h == Highest(h, n) and
                   p_hh)
               then h
               else NaN;
def p_PHValue = if !IsNaN(p_PivotH)
                then p_PivotH
                else p_PHValue[1];
def p_PHBar = if !IsNaN(p_PivotH)
                    then bar
                    else nan;
# Pivot High and Pivot High Exit Variables
# Pivot High Variables
def hh = fold ii = 1 to n + 1
         with pp = 1
         while pp
         do h > GetValue(h, -1);
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 PHBar = if !IsNaN(PivotH)
                  then bar
                  else nan;
# Pivot High Exit Variables
def PHExit = if (bar > n and
              h == Highest(h, n) and
              hh)
             then if l[1] < l
                  then l[1]
                  else fold r = 0 to 20
                       with a = NaN
                       while IsNaN(a)
                       do if GetValue(l[1], r) < l
                          then GetValue(l[1], r)
                          else NaN
            else Double.NaN;
def PHExitValue = if !IsNaN(PHExit)
                  then PHExit
                  else PHExitValue[1];
def PHExitBar = if (bar > n and
                    h == Highest(h, n) and
                    hh)
                then if l[1] < l
                then bar - 1
                else fold d = 0 to 20
                     with y = NaN
                     while IsNaN(y)
                     do if GetValue(l[1], d) < l
                        then GetValue(bar - 1, d)
                        else NaN
                else NaN;
# Pivot Low and Pivot Low Entry Variables
# Parent Pivot Low Variables
def p_ll = fold j = 1 to n + 1
           with q = 1
           while q
           do l < GetValue(l, -1);
def p_PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 p_ll)
             then l
             else NaN;
def p_PLValue = if !IsNaN(p_PivotL)
              then p_PivotL
              else p_PLValue[1];
def p_PLBar = if !IsNaN(p_PivotL)
              then bar
              else nan;
# Pivot Low Variables
def ll = fold jj = 1 to n + 1
         with qq = 1
         while qq
         do l < GetValue(l, -1);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBar = if !IsNaN(PivotL)
            then bar
            else nan;
# Pivot Low Entry Variables
def PLEntry = if (bar > n and
                  l == Lowest(l, n) and
                  ll)
              then if h[1] > h
              then h[1]
              else fold t = 0 to 20
                   with w = NaN
                   while IsNaN(w)
                   do if GetValue(h[1], t) > h
                      then GetValue(h[1], t)
                      else NaN
              else NaN;
def PLEntryValue = if !IsNaN(PLEntry)
                   then PLEntry
                   else PLEntryValue[1];
def PLEntryBar =  if (bar > n and
                  l == Lowest(l, n) and
                  ll)
                  then if h[1] > h
                       then bar - 1
                       else fold u = 0 to 20
                            with z = NaN
                            while IsNaN(z)
                            do if GetValue(h[1], u) > h
                               then GetValue(bar - 1, u)
                               else NaN
              else NaN;
# Plots

plot S_100 = if bar >= HighestAll(PHBar) then HighestAll(if isNaN(close[-1]) then PHValue  else nan) else nan;
S_100.SetDefaultColor(Color.dark_red);
S_100.SetLineWeight(1);
def S_0100 =(if isNaN(S_100[1]) then S_100 else Double.NaN);
addchartBubble(LabelsOn and S_100, S_0100,"100%",color.dark_red);

plot S_786 = if bar >= HighestAll(PHexitBar) then HighestAll(if isNaN(close[-1]) then PHExitValue  else nan)  else nan;
S_786.SetDefaultColor(Color.red);
def S_0786 =(if isNaN(S_786[1]) then S_786 else Double.NaN);
addchartBubble(LabelsOn and S_786, S_0786,"78.6%",color.light_red);

plot priceLine = HighestAll(if IsNaN(c[-1])
                            then c
                            else Double.NaN);
priceLine.SetStyle(Curve.SHORT_DASH);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.CYAN);

plot DnArrow = if c crosses below S_786 and ((FE > .618)
                or (FE < .382))
               then h
               else Double.NaN;
DnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow.SetLineWeight(5);
DnArrow.SetDefaultColor(Color.RED);

#Fib levels

plot S_0 = (S_100-S_786) * -4.673 + S_100;
S_0.setdefaultcolor(color.red);
S_0.setlineweight(1);
def S_00 =(if isNaN(S_0[1]) then S_0 else Double.NaN);
addchartBubble(LabelsOn and S_0, S_00,"0%: " + asDollars(S_00),color.red);

plot S_124 = (S_100-S_0)/-1.14155 + S_100;
S_124.setdefaultcolor(color.dark_orange);
S_124.setlineweight(1);
def S_0124 =(if isNaN(S_124[1]) then S_124 else Double.NaN);
addchartBubble(LabelsOn and S_124, S_0124,"12.4%",color.dark_orange);

plot S_236 = (S_100-S_0)/-1.3089+ S_100;
S_236.setdefaultcolor(color.plum);
S_236.setlineweight(1);
def S_0236 = (if isNaN(S_236[1]) then S_236 else Double.NaN);
addchartBubble(LabelsOn and S_236, S_0236,"23.6%",color.plum);

plot S_382 = (S_100-S_0)/-1.618+ S_100;
S_382.setdefaultcolor(color.cyan);
S_382.setlineweight(1);
def S_0382 =(if isNaN(S_382[1]) then S_382 else Double.NaN);
addchartBubble(LabelsOn and S_382, S_0382,"38.2%",color.cyan);

plot S_50 = (S_100-S_0)/-2 + S_100;
S_50.setdefaultcolor(color.green);
S_50.setlineweight(1);
def S_050 =(if isNaN(S_50[1]) then S_50 else Double.NaN);
addchartBubble(LabelsOn and S_50, S_050,"50%",color.green);

plot S_618 = (S_100-S_0)/-2.618+ S_100;
S_618.setdefaultcolor(color.yellow);
S_618.setlineweight(1);
def S_0618 =(if isNaN(S_618[1]) then S_618 else Double.NaN);
addchartBubble(LabelsOn and S_618, S_0618,"61.8%",color.yellow);

plot S_886 = (S_100-S_786)/-1.88+ S_100;
S_886.setdefaultcolor(color.violet);
S_886.setlineweight(1);
def S_0886 =(if isNaN(S_886[1]) then S_886 else Double.NaN);
addchartBubble(LabelsOn and S_886, S_0886,"88.6%",color.violet);

plot SE_272 = (S_100-S_0)*-1.272 + S_100;
SE_272.setdefaultcolor(color.yellow);
SE_272.SetStyle(Curve.LONG_DASH);
SE_272.setlineweight(2);
def S_0272 =(if isNaN(SE_272[1]) then SE_272 else Double.NaN);
addchartBubble(LabelsOn and SE_272, S_0272,"-27.2%",color.yellow);

plot SE_50 = (S_100-S_0)*-1.5 + S_100;
SE_50.setdefaultcolor(color.GREEN);
SE_50.SetStyle(Curve.LONG_DASH);
SE_50.setlineweight(1);
def SE_050 =(if isNaN(SE_50[1]) then SE_50 else Double.NaN);
addchartBubble(LabelsOn and SE_50, SE_050,"-50%",color.GREEN);

# Alerts
Alert(AlertsOn and DnArrow, " ", Alert.Bar, Sound.ding);
 
Last edited:

tem2005

Member
VIP
Can we merge the long and short in one code or at least add them in same chart?

also would it be issue if this indicator added to the other Automatic Quadrant Lines? or they are the same. forgive me cuz im noob.
 

chewie76

Well-known member
VIP Enthusiast
Can we merge the long and short in one code or at least add them in same chart?

also would it be issue if this indicator added to the other Automatic Quadrant Lines? or they are the same. forgive me cuz im noob.
Add both to your chart. You can also add Quadrant lines too, they are different.
 

D_Tramp

New member
VIP
All other Automatic Fib indicators look at the past, measure a previous high and previous low and give you levels. This indicator looks into the future and gives you possible high and low price levels. The Automatic Fibonacci Levels are two indicators, one for long positions and one for short positions. Up and Down arrows show entry signals when price crosses the 78.6% level. Entry signal alerts are included. This can be used for both swing trading and day trading based on the timeframe of your chart.

If price continues and breaks the 100% level, a new set of lines will be automatically drawn. Note, no guarantee price will hit the target.

Here are some examples.

Long position in /RTY 4 hr.
TIpDhV0.jpg


Long position in WMT daily.
gEGf6ry.jpg


Long position in JNJ daily.
mjvPGHG.jpg


Short position in /SI daily.
bIETPeg.jpg


Short position in HRZN 4 hr.
pGZPUTM.jpg


Shareable Link:
Long position:

http://tos.mx/AyuB4pQ

Short position:
http://tos.mx/1IJ4a4L

Long Position Code:
Code:
#Automatic Fibonacci Long Levels
#based on Mobius's Fractal Pivot Strategy
#developed by Chewie76 on 8/27/2021

# User Inputs
input n = 20;
input FractalEnergyLength = 8;
input FractalEnergyThreshold = .68;
input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
input LabelsOn = yes;
input AlertsOn = yes;

# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def TS = TickSize();
def nan = double.nan;
def ATR = Round((MovingAverage(AvgType, TrueRange(h, c, l), nATR)) / TS, 0) * TS;
def risk = if Between(c, 0, 1500)
           then ATR
           else if Between(c, 1500, 3500)
           then 2
           else if Between(c, 3500, 5500)
                then 4
           else 6;
def FE = Log(Sum((Max(h, c[1]) - Min(l, c[1])), FractalEnergyLength) /
        (Highest(h, FractalEnergyLength) - Lowest(l, FractalEnergyLength)))
            / Log(FractalEnergyLength);
# Parent Aggregation Pivot High
# Pivot High Variables
def p_hh = fold i = 1 to n + 1
           with p = 1
           while p
           do h > GetValue(h, -1);
def p_PivotH = if (bar > n and
                   h == Highest(h, n) and
                   p_hh)
               then h
               else NaN;
def p_PHValue = if !IsNaN(p_PivotH)
                then p_PivotH
                else p_PHValue[1];
def p_PHBar = if !IsNaN(p_PivotH)
                    then bar
                    else nan;
# Pivot High and Pivot High Exit Variables
# Pivot High Variables
def hh = fold ii = 1 to n + 1
         with pp = 1
         while pp
         do h > GetValue(h, -1);
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 PHBar = if !IsNaN(PivotH)
                  then bar
                  else nan;
# Pivot High Exit Variables
def PHExit = if (bar > n and
              h == Highest(h, n) and
              hh)
             then if l[1] < l
                  then l[1]
                  else fold r = 0 to 20
                       with a = NaN
                       while IsNaN(a)
                       do if GetValue(l[1], r) < l
                          then GetValue(l[1], r)
                          else NaN
            else Double.NaN;
def PHExitValue = if !IsNaN(PHExit)
                  then PHExit
                  else PHExitValue[1];
def PHExitBar = if (bar > n and
                    h == Highest(h, n) and
                    hh)
                then if l[1] < l
                then bar - 1
                else fold d = 0 to 20
                     with y = NaN
                     while IsNaN(y)
                     do if GetValue(l[1], d) < l
                        then GetValue(bar - 1, d)
                        else NaN
                else NaN;
# Pivot Low and Pivot Low Entry Variables
# Parent Pivot Low Variables
def p_ll = fold j = 1 to n + 1
           with q = 1
           while q
           do l < GetValue(l, -1);
def p_PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 p_ll)
             then l
             else NaN;
def p_PLValue = if !IsNaN(p_PivotL)
              then p_PivotL
              else p_PLValue[1];
def p_PLBar = if !IsNaN(p_PivotL)
              then bar
              else nan;
# Pivot Low Variables
def ll = fold jj = 1 to n + 1
         with qq = 1
         while qq
         do l < GetValue(l, -1);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBar = if !IsNaN(PivotL)
            then bar
            else nan;
# Pivot Low Entry Variables
def PLEntry = if (bar > n and
                  l == Lowest(l, n) and
                  ll)
              then if h[1] > h
              then h[1]
              else fold t = 0 to 20
                   with w = NaN
                   while IsNaN(w)
                   do if GetValue(h[1], t) > h
                      then GetValue(h[1], t)
                      else NaN
              else NaN;
def PLEntryValue = if !IsNaN(PLEntry)
                   then PLEntry
                   else PLEntryValue[1];
def PLEntryBar =  if (bar > n and
                  l == Lowest(l, n) and
                  ll)
                  then if h[1] > h
                       then bar - 1
                       else fold u = 0 to 20
                            with z = NaN
                            while IsNaN(z)
                            do if GetValue(h[1], u) > h
                               then GetValue(bar - 1, u)
                               else NaN
              else NaN;
# Plots

plot F_100 =  if bar >= HighestAll(PLBar)
          then HighestAll(if isNaN(c[-1])
                          then PLValue
                          else nan)
          else nan;
F_100.SetDefaultColor(Color.dark_red);
F_100.SetLineWeight(2);
def F_0100 =(if isNaN(F_100[1]) then F_100 else Double.NaN);
addchartBubble(LabelsOn and F_100, F_0100,"100%",color.dark_red);

plot F_786 = if bar >= HighestAll(PLEntryBar)
                    then HighestAll(if isNaN(c[-1])
                                    then PLEntryValue
                                    else nan)
                    else nan;
F_786.SetDefaultColor(Color.light_red);
F_786.SetLineWeight(2);
def F_0786 =(if isNaN(F_786[1]) then F_786 else Double.NaN);
addchartBubble(LabelsOn and F_786, F_0786,"78.6%",color.light_red);

plot priceLine = HighestAll(if IsNaN(c[-1])
                            then c
                            else Double.NaN);
priceLine.SetStyle(Curve.SHORT_DASH);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.CYAN);
plot UpArrow = if c crosses above F_786 and FE > .5
               then l
               else Double.NaN;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow.SetLineWeight(5);
UpArrow.SetDefaultColor(Color.GREEN);

#Fib levels

plot F_0 = (F_786-F_100) * 4.673 + F_100;
F_0.setdefaultcolor(color.red);
F_0.setlineweight(2);
def F_00 =(if isNaN(F_0[1]) then F_0 else Double.NaN);
addchartBubble(LabelsOn and F_0, F_00,"0%: " + asDollars(F_00),color.red);

plot F_124 = (F_0 - F_100)/1.14155+ F_100;
F_124.setdefaultcolor(color.dark_orange);
F_124.setlineweight(2);
def F_0124 =(if isNaN(F_124[1]) then F_124 else Double.NaN);
addchartBubble(LabelsOn and F_124, F_0124,"12.4%",color.dark_orange);

plot F_236 = (F_0 - F_100)/1.3089+ F_100;
F_236.setdefaultcolor(color.plum);
F_236.setlineweight(2);
def F_0236 = (if isNaN(F_236[1]) then F_236 else Double.NaN);
addchartBubble(LabelsOn and F_236, F_0236,"23.6%",color.plum);

plot F_382 = (F_0 - F_100)/1.618+ F_100;
F_382.setdefaultcolor(color.cyan);
F_382.setlineweight(2);
def F_0382 =(if isNaN(F_382[1]) then F_382 else Double.NaN);
addchartBubble(LabelsOn and F_382, F_0382,"38.2%",color.cyan);

plot F_50 = (F_0 - F_100)/2 + F_100;
F_50.setdefaultcolor(color.green);
F_50.setlineweight(2);
def F_050 =(if isNaN(F_50[1]) then F_50 else Double.NaN);
addchartBubble(LabelsOn and F_50, F_050,"50%",color.green);

plot F_618 = (F_0 - F_100)/2.618+ F_100;
F_618.setdefaultcolor(color.yellow);
F_618.setlineweight(2);
def F_0618 =(if isNaN(F_618[1]) then F_618 else Double.NaN);
addchartBubble(LabelsOn and F_618, F_0618,"61.8%",color.yellow);

plot F_886 = (F_786 - F_100)/1.88+ F_100;
F_886.setdefaultcolor(color.violet);
F_886.setlineweight(2);
def F_0886 =(if isNaN(F_886[1]) then F_886 else Double.NaN);
addchartBubble(LabelsOn and F_886, F_0886,"88.6%",color.violet);

plot FE_272 = (F_0 - F_100)*1.272 + F_100;
FE_272.setdefaultcolor(color.yellow);
FE_272.SetStyle(Curve.LONG_DASH);
FE_272.setlineweight(2);
def F_0272 =(if isNaN(FE_272[1]) then FE_272 else Double.NaN);
addchartBubble(LabelsOn and FE_272, F_0272,"-27.2%",color.yellow);

plot FE_50 = (F_0 - F_100)*1.5 + F_100;
FE_50.setdefaultcolor(color.GREEN);
FE_50.SetStyle(Curve.LONG_DASH);
FE_50.setlineweight(2);
def FE_050 =(if isNaN(FE_50[1]) then FE_50 else Double.NaN);
addchartBubble(LabelsOn and FE_50, FE_050,"-50%",color.GREEN);

# Alerts
Alert(AlertsOn and UpArrow, " ", Alert.Bar, Sound.ding);

Short Position Code:
Code:
#Automatic Fibonacci Short Levels
#based on Mobius's Fractal Pivot Strategy
#developed by Chewie76 on 8/27/2021

# User Inputs
input n = 20;
input FractalEnergyLength = 8;
input FractalEnergyThreshold = .68;
input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
input LabelsOn = yes;
input AlertsOn = yes;

# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def TS = TickSize();
def nan = double.nan;
def ATR = Round((MovingAverage(AvgType, TrueRange(h, c, l), nATR)) / TS, 0) * TS;
def risk = if Between(c, 0, 1500)
           then ATR
           else if Between(c, 1500, 3500)
           then 2
           else if Between(c, 3500, 5500)
                then 4
           else 6;
def FE = Log(Sum((Max(h, c[1]) - Min(l, c[1])), FractalEnergyLength) /
        (Highest(h, FractalEnergyLength) - Lowest(l, FractalEnergyLength)))
            / Log(FractalEnergyLength);
# Parent Aggregation Pivot High
# Pivot High Variables
def p_hh = fold i = 1 to n + 1
           with p = 1
           while p
           do h > GetValue(h, -1);
def p_PivotH = if (bar > n and
                   h == Highest(h, n) and
                   p_hh)
               then h
               else NaN;
def p_PHValue = if !IsNaN(p_PivotH)
                then p_PivotH
                else p_PHValue[1];
def p_PHBar = if !IsNaN(p_PivotH)
                    then bar
                    else nan;
# Pivot High and Pivot High Exit Variables
# Pivot High Variables
def hh = fold ii = 1 to n + 1
         with pp = 1
         while pp
         do h > GetValue(h, -1);
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 PHBar = if !IsNaN(PivotH)
                  then bar
                  else nan;
# Pivot High Exit Variables
def PHExit = if (bar > n and
              h == Highest(h, n) and
              hh)
             then if l[1] < l
                  then l[1]
                  else fold r = 0 to 20
                       with a = NaN
                       while IsNaN(a)
                       do if GetValue(l[1], r) < l
                          then GetValue(l[1], r)
                          else NaN
            else Double.NaN;
def PHExitValue = if !IsNaN(PHExit)
                  then PHExit
                  else PHExitValue[1];
def PHExitBar = if (bar > n and
                    h == Highest(h, n) and
                    hh)
                then if l[1] < l
                then bar - 1
                else fold d = 0 to 20
                     with y = NaN
                     while IsNaN(y)
                     do if GetValue(l[1], d) < l
                        then GetValue(bar - 1, d)
                        else NaN
                else NaN;
# Pivot Low and Pivot Low Entry Variables
# Parent Pivot Low Variables
def p_ll = fold j = 1 to n + 1
           with q = 1
           while q
           do l < GetValue(l, -1);
def p_PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 p_ll)
             then l
             else NaN;
def p_PLValue = if !IsNaN(p_PivotL)
              then p_PivotL
              else p_PLValue[1];
def p_PLBar = if !IsNaN(p_PivotL)
              then bar
              else nan;
# Pivot Low Variables
def ll = fold jj = 1 to n + 1
         with qq = 1
         while qq
         do l < GetValue(l, -1);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else NaN;
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
def PLBar = if !IsNaN(PivotL)
            then bar
            else nan;
# Pivot Low Entry Variables
def PLEntry = if (bar > n and
                  l == Lowest(l, n) and
                  ll)
              then if h[1] > h
              then h[1]
              else fold t = 0 to 20
                   with w = NaN
                   while IsNaN(w)
                   do if GetValue(h[1], t) > h
                      then GetValue(h[1], t)
                      else NaN
              else NaN;
def PLEntryValue = if !IsNaN(PLEntry)
                   then PLEntry
                   else PLEntryValue[1];
def PLEntryBar =  if (bar > n and
                  l == Lowest(l, n) and
                  ll)
                  then if h[1] > h
                       then bar - 1
                       else fold u = 0 to 20
                            with z = NaN
                            while IsNaN(z)
                            do if GetValue(h[1], u) > h
                               then GetValue(bar - 1, u)
                               else NaN
              else NaN;
# Plots

plot S_100 = if bar >= HighestAll(PHBar) then HighestAll(if isNaN(close[-1]) then PHValue  else nan) else nan;
S_100.SetDefaultColor(Color.dark_red);
S_100.SetLineWeight(1);
def S_0100 =(if isNaN(S_100[1]) then S_100 else Double.NaN);
addchartBubble(LabelsOn and S_100, S_0100,"100%",color.dark_red);

plot S_786 = if bar >= HighestAll(PHexitBar) then HighestAll(if isNaN(close[-1]) then PHExitValue  else nan)  else nan;
S_786.SetDefaultColor(Color.red);
def S_0786 =(if isNaN(S_786[1]) then S_786 else Double.NaN);
addchartBubble(LabelsOn and S_786, S_0786,"78.6%",color.light_red);

plot priceLine = HighestAll(if IsNaN(c[-1])
                            then c
                            else Double.NaN);
priceLine.SetStyle(Curve.SHORT_DASH);
priceLine.SetLineWeight(1);
priceLine.SetDefaultColor(Color.CYAN);

plot DnArrow = if c crosses below S_786 and ((FE > .618)
                or (FE < .382))
               then h
               else Double.NaN;
DnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow.SetLineWeight(5);
DnArrow.SetDefaultColor(Color.RED);

#Fib levels

plot S_0 = (S_100-S_786) * -4.673 + S_100;
S_0.setdefaultcolor(color.red);
S_0.setlineweight(1);
def S_00 =(if isNaN(S_0[1]) then S_0 else Double.NaN);
addchartBubble(LabelsOn and S_0, S_00,"0%: " + asDollars(S_00),color.red);

plot S_124 = (S_100-S_0)/-1.14155 + S_100;
S_124.setdefaultcolor(color.dark_orange);
S_124.setlineweight(1);
def S_0124 =(if isNaN(S_124[1]) then S_124 else Double.NaN);
addchartBubble(LabelsOn and S_124, S_0124,"12.4%",color.dark_orange);

plot S_236 = (S_100-S_0)/-1.3089+ S_100;
S_236.setdefaultcolor(color.plum);
S_236.setlineweight(1);
def S_0236 = (if isNaN(S_236[1]) then S_236 else Double.NaN);
addchartBubble(LabelsOn and S_236, S_0236,"23.6%",color.plum);

plot S_382 = (S_100-S_0)/-1.618+ S_100;
S_382.setdefaultcolor(color.cyan);
S_382.setlineweight(1);
def S_0382 =(if isNaN(S_382[1]) then S_382 else Double.NaN);
addchartBubble(LabelsOn and S_382, S_0382,"38.2%",color.cyan);

plot S_50 = (S_100-S_0)/-2 + S_100;
S_50.setdefaultcolor(color.green);
S_50.setlineweight(1);
def S_050 =(if isNaN(S_50[1]) then S_50 else Double.NaN);
addchartBubble(LabelsOn and S_50, S_050,"50%",color.green);

plot S_618 = (S_100-S_0)/-2.618+ S_100;
S_618.setdefaultcolor(color.yellow);
S_618.setlineweight(1);
def S_0618 =(if isNaN(S_618[1]) then S_618 else Double.NaN);
addchartBubble(LabelsOn and S_618, S_0618,"61.8%",color.yellow);

plot S_886 = (S_100-S_786)/-1.88+ S_100;
S_886.setdefaultcolor(color.violet);
S_886.setlineweight(1);
def S_0886 =(if isNaN(S_886[1]) then S_886 else Double.NaN);
addchartBubble(LabelsOn and S_886, S_0886,"88.6%",color.violet);

plot SE_272 = (S_100-S_0)*-1.272 + S_100;
SE_272.setdefaultcolor(color.yellow);
SE_272.SetStyle(Curve.LONG_DASH);
SE_272.setlineweight(2);
def S_0272 =(if isNaN(SE_272[1]) then SE_272 else Double.NaN);
addchartBubble(LabelsOn and SE_272, S_0272,"-27.2%",color.yellow);

plot SE_50 = (S_100-S_0)*-1.5 + S_100;
SE_50.setdefaultcolor(color.GREEN);
SE_50.SetStyle(Curve.LONG_DASH);
SE_50.setlineweight(1);
def SE_050 =(if isNaN(SE_50[1]) then SE_50 else Double.NaN);
addchartBubble(LabelsOn and SE_50, SE_050,"-50%",color.GREEN);

# Alerts
Alert(AlertsOn and DnArrow, " ", Alert.Bar, Sound.ding);
Hi! It's amazing!!!
Is there tradingview version?
 

sunnybabu

Member
VIP
@chewie76 thank you for this code. Is it possible to build a strategy out of it and see the p/l please? Also is quadrant yielding better results or this one? Many Thanks
 

chewie76

Well-known member
VIP Enthusiast
@chewie76 thank you for this code. Is it possible to build a strategy out of it and see the p/l please? Also is quadrant yielding better results or this one? Many Thanks
There is no guarantee price will hit the targets, so it's hard to say which is better. I'm sure a strategy can be built from this. I'm have not made any strategies.
 

MatthewA

Active member
@chewie76
Wow! Just Wow!
As usual you create something similar to my thoughts!

I have some ideas to combine this and you Quad lines into an intelligent Ewave, Wolf Wave, Harmonic that can be confirmed or challenged by order flow...
I will contact you on discord
 
Last edited:

slashgw

New member
There is no guarantee price will hit the targets, so it's hard to say which is better. I'm sure a strategy can be built from this. I'm have not made any strategies.
Adding the strategy portion is pretty straight-forward. Define conditions that will trigger the buy or sell, as well as the price & quantity and call the AddOrder() statement with those parameters.

The version we created is setup to resemble the "Measured Move" philosophy. Part of the hurdle we initially had was not being able to keep the current target in that same location once a new high was was made (using a long setup example). We approached that by defining a variable that was set when once we determined that the setup was actually trading. (Within 2-3 ticks of the level that you choose to use) I have mine as an input that I can choose either the 38.2% or the 50% hwb. Use TickSize() to determine what price would be at that 2-3 tick distance. Once it's determined that a setup is trading, then we "lock" the levels until either the 61.8% level breaks, or the -23.6% target is hit.

We also defined multiple "modes" that define which session we are optimizing for. Globex, RTH, Euro, Custom (manually define the start & end time).

With regard to having a long & a short version, if you read the BarNumber() of the high & the low, then just use simple math to determine which occurred first. If the high occurred first, and the low was more recent, then calculate if close would still be inside the 61.8% level. If true, then setup in a "Short" configuration. And of course the opposite for a "Long" configuration.

Having said that, it's also easier & cleaner to create an internal "script" function that will handle all of the fib levels as you pass the fib% to the function via inputs similar to when using the "reference" command.

I hope this will provide some assistance or ideas for "think"scripting outside the box!

Best wishes,
slash

Example Chart
 

sindhu19

New member
2019 Donor
VIP
Adding the strategy portion is pretty straight-forward. Define conditions that will trigger the buy or sell, as well as the price & quantity and call the AddOrder() statement with those parameters.

The version we created is setup to resemble the "Measured Move" philosophy. Part of the hurdle we initially had was not being able to keep the current target in that same location once a new high was was made (using a long setup example). We approached that by defining a variable that was set when once we determined that the setup was actually trading. (Within 2-3 ticks of the level that you choose to use) I have mine as an input that I can choose either the 38.2% or the 50% hwb. Use TickSize() to determine what price would be at that 2-3 tick distance. Once it's determined that a setup is trading, then we "lock" the levels until either the 61.8% level breaks, or the -23.6% target is hit.

We also defined multiple "modes" that define which session we are optimizing for. Globex, RTH, Euro, Custom (manually define the start & end time).

With regard to having a long & a short version, if you read the BarNumber() of the high & the low, then just use simple math to determine which occurred first. If the high occurred first, and the low was more recent, then calculate if close would still be inside the 61.8% level. If true, then setup in a "Short" configuration. And of course the opposite for a "Long" configuration.

Having said that, it's also easier & cleaner to create an internal "script" function that will handle all of the fib levels as you pass the fib% to the function via inputs similar to when using the "reference" command.

I hope this will provide some assistance or ideas for "think"scripting outside the box!

Best wishes,
slash

Example Chart
very clean chart, mind sharing your indicator?
 

FreefallJM03

Member
VIP
Are you checking charts in a particular list or are you using some other criteria to determine which charts you would check? It looks great when you see the signal hit. I've checked several charts in a watchlist I have that have shown great entry and exit points based off the script you wrote. I'm just wondering how you're going about using it. Is there a way to make a watchlist that will let you know when a stock in it triggers or do you have to check charts every so often to find one with an entry?
 

chewie76

Well-known member
VIP Enthusiast
Are you checking charts in a particular list or are you using some other criteria to determine which charts you would check? It looks great when you see the signal hit. I've checked several charts in a watchlist I have that have shown great entry and exit points based off the script you wrote. I'm just wondering how you're going about using it. Is there a way to make a watchlist that will let you know when a stock in it triggers or do you have to check charts every so often to find one with an entry?
You can create a scan and have the arrow as true on any timeframe. I like the 4hr and 30 min chart. Then create a watchlist from the scan.
 

Similar threads

Top