 # Auto Fib (Fibonacci) Levels Indicator for ThinkorSwim

#### BenTen

Staff member
Staff
VIP
Automatically draws fibonacci retracements using the highest price and lowest price from the current view and timeframe.

Fibonacci retracements use horizontal lines to indicate areas of support or resistance at the key Fibonacci levels before it continues in the original direction. These levels are created by drawing a trendline between two extreme points and then dividing the vertical distance by the key Fibonacci ratios of: 23.6%, 38.2%, 50%, 61.8%, 78.6%, and 100%.

Created by RyanHendricks. ### thinkScript Code

Rich (BB code):
``````#hint Price: Price used in the alerts on crossing retracement lines. <b>(Default is Close)</b>
#hint onExpansion: Determines if the retracement lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b>
#hint Extend_to_left: Determines if the retracement lines are extended to the left side of the chart. <b>(Default is No)</b>

#hint Coefficient0: Retracement Line 0: Retracement from the highest high to the lowest low.<b>(Default is 0%)</b>
#hint Coefficient_1: Retracement Line 1: Retracement from the highest high to the lowest low.<b>(Default is 23.6%)</b>
#hint Coefficient_2: Retracement Line 2: Retracement from the highest high to the lowest low.<b>(Default is 38.2%)</b>
#hint Coefficient_3: Retracement Line 3: Retracement from the highest high to the lowest low.<b>(Default is 50%)</b>
#hint Coefficient_4: Retracement Line 4: Retracement from the highest high to the lowest low.<b>(Default is 61.8%)</b>
#hint Coefficient_5: Retracement Line 5: Retracement from the highest high to the lowest low.<b>(Default is 78.6%)</b>
#hint Coefficient_6: Retracement Line 6: Retracement from the highest high to the lowest low.<b>(Default is 100%)</b>

#wizard input: Price
#wizard text: Inputs: Price:
#wizard input: onExpansion
#wizard text: onExpansion:
#wizard input: Extend_to_left
#wizard text: Extend_to_left:
#wizard input: Coefficient0
#wizard text: Coefficient0:
#wizard input: Coefficient_1
#wizard text: Coefficient_1:
#wizard input: Coefficient_2
#wizard text: Coefficient_2:
#wizard input: Coefficient_3
#wizard text: Coefficient_3:
#wizard input: Coefficient_4
#wizard text: Coefficient_4:
#wizard input: Coefficient_5
#wizard text: Coefficient_5:
#wizard input: Coefficient_6
#wizard text: Coefficient_6:

input price = close;
input high = high;
input low = low;
input onExpansion = Yes;
input Extend_to_left = no;
input Coefficient0 = 0.000;
input coefficient_1 = .236;
input Coefficient_2 = .382;
input Coefficient_3 = .500;
input Coefficient_4 = .618;
Input Coefficient_5 = .786;
input Coefficient_6 = 1.000;

def a = HighestAll(high);
def b = LowestAll(low);
def barnumber = barNumber();
def c = if high == a then barnumber else double.nan;
def d = if low == b then barnumber else double.nan;
rec highnumber = compoundValue(1, if IsNaN(c) then highnumber else c, c);
def highnumberall = HighestAll(highnumber);
rec lownumber = compoundValue(1, if IsNaN(d) then lownumber else d, d);
def lownumberall = LowestAll(lownumber);

def upward = highnumberall > lownumberall;
def downward = highnumberall < lownumberall;

def x = AbsValue(lownumberall - highnumberall );

def slope = (a - b) / x;
def slopelow = (b - a) / x;

def day = getDay();
def month = getMonth();
def year = getYear();
def lastDay = getLastDay();
def lastmonth = getLastMonth();
def lastyear = getLastYear();
def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0);
def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan);
def line = b + (slope * (barnumber - lownumber));
def linelow = a + (slopelow * (barnumber - highnumber));
def currentlinelow = if barnumber <= lownumberall then linelow else double.nan;
def currentline = if barnumber <= highnumberall then line else double.nan;

Plot FibFan =  if  downward then currentlinelow else if upward then currentline else double.nan;
FibFan.SetStyle(Curve.SHORT_DASH);
FibFan.AssignValueColor(color.red);
fibfan.hidebubble();

def range =  a - b;

Plot Retracement0 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient0))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient0)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient0))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient0)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient0))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient0)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient0))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient0)) else double.nan;
Retracement0.assignvaluecolor(color.red);
retracement0.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber +10), retracement0, concat( "\$", round(retracement0, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);

Plot Retracement1 =  if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_1))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_1)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_1))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_1)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_1))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_1)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_1))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_1)) else double.nan;
Retracement1.assignvaluecolor(color.red);
retracement1.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement1, concat( "\$", round(retracement1, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);

Plot Retracement2 =if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_2))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_2)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_2))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_2)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_2))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_2)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_2))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_2)) else double.nan;
Retracement2.assignvaluecolor(color.red);
retracement2.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement2, concat( "\$", round(retracement2, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);

Plot Retracement3 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_3))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_3)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_3))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_3)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_3))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_3)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_3))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_3)) else double.nan;
Retracement3.assignvaluecolor(color.red);
retracement3.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement3, concat( "\$", round(retracement3, 2)), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);

Plot Retracement4 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_4))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_4)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_4))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_4)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_4))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_4)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_4))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_4)) else double.nan;
Retracement4.assignvaluecolor(color.red);
retracement4.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement4, concat( "\$", round(retracement4, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);

Plot Retracement5 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_5))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_5)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_5))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_5)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_5))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_5)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_5))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_5)) else double.nan;
Retracement5.assignvaluecolor(color.red);
retracement5.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement5, concat( "\$", round(retracement5, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);

Plot Retracement6 = if downward and !onexpansion and !extend_to_left and barnumber >= highnumberall and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_6))) else if upward and !extend_to_left and !onexpansion and barnumber >= lownumberall and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_6)) else if downward and onexpansion and !extend_to_left and barnumber >= highnumberall then highestall((b + (range *  coefficient_6))) else if upward and onexpansion and barnumber >= lownumberall and !extend_to_left then highestall(a - (range * coefficient_6)) else if downward and !onexpansion and extend_to_left and barnumber <= istodaybarnumber then highestall((b + (range *  coefficient_6))) else if upward and extend_to_left and !onexpansion and barnumber <= istodaybarnumber then highestall(a - (range * coefficient_6)) else if downward and onexpansion and extend_to_left then highestall((b + (range *  coefficient_6))) else if upward and onexpansion and extend_to_left then highestall(a - (range * coefficient_6)) else double.nan;
Retracement6.assignvaluecolor(color.red);
retracement6.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement6, concat( "\$", round(retracement6, 2)), color.red, yes);

AddChartBubble((downward and barnumber == highnumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);

alert((price crosses below Retracement0) , "Price crosses below Retracement Line 0");
alert((price crosses above Retracement0) , "Price crosses above Retracement Line 0");
alert((price crosses below Retracement1) , "Price crosses below Retracement Line 1");
alert((price crosses above Retracement1) , "Price crosses above Retracement Line 1");
alert((price crosses below Retracement2) , "Price crosses below Retracement Line 2");
alert((price crosses above Retracement2) , "Price crosses above Retracement Line 2");
alert((price crosses below Retracement3) , "Price crosses below Retracement Line 3");
alert((price crosses above Retracement3) , "Price crosses above Retracement Line 3");
alert((price crosses below Retracement4) , "Price crosses below Retracement Line 4");
alert((price crosses above Retracement4) , "Price crosses above Retracement Line 4");
alert((price crosses below Retracement5) , "Price crosses below Retracement Line 5");
alert((price crosses above Retracement5) , "Price crosses above Retracement Line 5");
alert((price crosses below Retracement6) , "Price crosses below Retracement Line 6");
alert((price crosses above Retracement6) , "Price crosses above Retracement Line 6");``````

https://tos.mx/Fz8Ss0

Last edited:
• • • jerome, Kaz, RickAns and 14 others

#### PlantainPapi

##### New member
2019 Donor
Hey Ben!

This is awesome stuff, thanks for sharing so much here. I tried adding this study into TOS and it keeps running the error of "Invalid statement: < at 6:1"

Any idea how to fix this?

Code:
``````input price = close;
input high = high;
input low = low;
input showPriceBubble = yes;
input onExpansion = Yes;
input Extend_to_left = no;
input Coefficient0 = 0.000;
input coefficient_1 = .236;
input Coefficient_2 = .382;
input Coefficient_3 = .500;
input Coefficient_4 = .618;
Input Coefficient_5 = .786;
input Coefficient_6 = 1.000;

def a = HighestAll(high);
def b = LowestAll(low);
def barnumber = barNumber();
def c = if high == a
then barnumber
else double.nan;
def d = if low == b
then barnumber
else double.nan;
rec highnumber = compoundValue(1, if IsNaN(c)
then highnumber
else c, c);
def highnumberall = HighestAll(highnumber);
rec lownumber = compoundValue(1, if IsNaN(d)
then lownumber
else d, d);
def lownumberall = LowestAll(lownumber);
def upward = highnumberall > lownumberall;
def downward = highnumberall < lownumberall;
def x = AbsValue(lownumberall - highnumberall );
def slope = (a - b) / x;
def slopelow = (b - a) / x;
def day = getDay();
def month = getMonth();
def year = getYear();
def lastDay = getLastDay();
def lastmonth = getLastMonth();
def lastyear = getLastYear();
def isToday = if(day == lastDay and
month == lastmonth and
year == lastyear, 1, 0);
def istodaybarnumber = HighestAll(if isToday
then barnumber
else double.nan);
def line = b + (slope * (barnumber - lownumber));
def linelow = a + (slopelow * (barnumber - highnumber));
def currentlinelow = if barnumber <= lownumberall
then linelow
else double.nan;
def currentline = if barnumber <= highnumberall
then line
else double.nan;

Plot FibFan =  if downward
then currentlinelow
else if upward
then currentline
else double.nan;
FibFan.SetStyle(Curve.SHORT_DASH);
FibFan.AssignValueColor(color.red);
Fibfan.hidebubble();

def range =  a - b;

Plot Retracement0 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient0)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient0))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient0)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient0))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient0)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient0))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient0)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient0))
else double.nan;
Retracement0.assignvaluecolor(color.red);
Retracement0.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement0, concat( "\$", round(retracement0, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement0, concat( (coefficient0 * 100), "%"), color.red, yes);

Plot Retracement1 =  if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_1)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_1))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_1)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_1))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_1)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_1))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient_1)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient_1))
else double.nan;
Retracement1.assignvaluecolor(color.red);
Retracement1.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement1, concat( "\$", round(retracement1, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);

Plot Retracement2 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_2)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_2))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_2)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_2))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_2)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_2))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient_2)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient_2))
else double.nan;
Retracement2.assignvaluecolor(color.red);
Retracement2.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement2, concat( "\$", round(retracement2, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);

Plot Retracement3 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_3)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_3))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_3)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_3))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_3)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_3))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient_3)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient_3))
else double.nan;
Retracement3.assignvaluecolor(color.red);
Retracement3.hidebubble();
AddChartBubble(( showPriceBubble == yes and barnumber == istodaybarnumber), retracement3, concat( "\$", round(retracement3, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);

Plot Retracement4 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_4)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_4))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_4)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_4))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_4)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_4))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient_4)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient_4))
else double.nan;
Retracement4.assignvaluecolor(color.red);
Retracement4.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement4, concat( "\$", round(retracement4, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);

Plot Retracement5 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_5)))
else if upward and
!extend_to_left and
!onexpansion and
barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_5))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_5)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_5))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_5)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_5))
else if downward and
onexpansion and
extend_to_left
then highestall((b + (range *  coefficient_5)))
else if upward and
onexpansion and
extend_to_left
then highestall(a - (range * coefficient_5))
else double.nan;
Retracement5.assignvaluecolor(color.red);
Retracement5.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement5, concat( "\$", round(retracement5, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);

Plot Retracement6 = if downward and
!onexpansion and
!extend_to_left and
barnumber >= highnumberall and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_6)))
else if upward and
!extend_to_left and
!onexpansion and barnumber >= lownumberall and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_6))
else if downward and
onexpansion and
!extend_to_left and
barnumber >= highnumberall
then highestall((b + (range *  coefficient_6)))
else if upward and
onexpansion and
barnumber >= lownumberall and
!extend_to_left
then highestall(a - (range * coefficient_6))
else if downward and
!onexpansion and
extend_to_left and
barnumber <= istodaybarnumber
then highestall((b + (range *  coefficient_6)))
else if upward and
extend_to_left and
!onexpansion and
barnumber <= istodaybarnumber
then highestall(a - (range * coefficient_6))
else if downward and
onexpansion and extend_to_left
then highestall((b + (range *  coefficient_6)))
else if upward and onexpansion and
extend_to_left
then highestall(a - (range * coefficient_6))
else double.nan;
Retracement6.assignvaluecolor(color.red);
Retracement6.hidebubble();
AddChartBubble((showPriceBubble == yes and barnumber == istodaybarnumber), retracement6, concat( "\$", round(retracement6, 2)), color.red, Yes);
AddChartBubble((downward and barnumber == highnumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);

alert((price crosses below Retracement0) , "Price crosses below Retracement Line 0");
alert((price crosses above Retracement0) , "Price crosses above Retracement Line 0");
alert((price crosses below Retracement1) , "Price crosses below Retracement Line 1");
alert((price crosses above Retracement1) , "Price crosses above Retracement Line 1");
alert((price crosses below Retracement2) , "Price crosses below Retracement Line 2");
alert((price crosses above Retracement2) , "Price crosses above Retracement Line 2");
alert((price crosses below Retracement3) , "Price crosses below Retracement Line 3");
alert((price crosses above Retracement3) , "Price crosses above Retracement Line 3");
alert((price crosses below Retracement4) , "Price crosses below Retracement Line 4");
alert((price crosses above Retracement4) , "Price crosses above Retracement Line 4");
alert((price crosses below Retracement5) , "Price crosses below Retracement Line 5");
alert((price crosses above Retracement5) , "Price crosses above Retracement Line 5");
alert((price crosses below Retracement6) , "Price crosses below Retracement Line 6");
alert((price crosses above Retracement6) , "Price crosses above Retracement Line 6");

# Fibonacci Time Series
rec bar1 = if lownumber == barnumber()
then barnumber()
else bar1;
rec bars = if barnumber() == bar1
then 2
else if barnumber > bar1
then bars + 1
else 0;
def coeff = Sqrt(5);
def smallest = 5;
def n = Floor(log(bars * coeff + 0.5) / log((1 + Sqrt(5)) / 2));
def inSeries = n != n and bars >= smallest;
def Series = if inSeries
then bars
else Double.NaN;
AddVerticalLine(Series, "FTS: ( " + Series + ")", Color.Blue, Curve.Short_Dash);``````

• BACONFLIPZ

#### BenTen

Staff member
Staff
VIP
@PlantainPapi Fixed #### thinky

##### Member
This study by Mobius plots both the Whole Chart Fibonacci along with the secondary set. Typically known as First and Second Wave Fibonacci set.

Code:
``````# Fibonacci Lines From Pivots
# Mobius
# V01.02.2013
# Other technical studies can be found on MyTrade: Mobius

declare Once_per_bar;
input numBars = 55; #hint numbars: Second Wave suggested 21, 34, 55, 89, 144
input showValues = no; #hint showValues: User choice
input showBarNumbers = no; #hint showBarNumbers: User choice
input TrendResistanceStart = 0; #hint TrendResistanceStart: User input value
input TrendResistanceEnd = 0; #hint TrendResistanceEnd: User input value
input TrendSupportStart = 0; #hint TrednSupportStart: User input value
input TrendSupportEnd = 0; #hint TrendSupportEnd: User input value
input BubbleOn = yes; #hint BubbleOn: User choice
# Study Definitions
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def yearstart = GetYear() * 10000 + 101;
def Coeff_1 = .236;
def Coeff_2 = .382;
def Coeff_3 = .500;
def Coeff_4 = .618;
def Coeff_5 = .786;

# First Wave Fibonacci Retracement
script Fibs {
input C0 = 0.000;
def o = open;
def h = high;
def l = low;
# Get highest and lowest on chart
def a = HighestAll(h);
def b = LowestAll(l);
# Get the bar numbers at the highest and lowest points
def barnumber = BarNumber();
def c = if h == a
then barnumber
else Double.NaN;
def d = if l == b
then barnumber
else Double.NaN;
def highnumber = CompoundValue(1, if IsNaN(c)
then highnumber
else c, c);
def highnumberall = HighestAll(highnumber);
def lownumber = CompoundValue(1, if IsNaN(d)
then lownumber
else d, d);
def lownumberall = LowestAll(lownumber);
# Determine Slope Delta
def upward = highnumberall > lownumberall;
def downward = highnumberall < lownumberall;
# Define X
def x = AbsValue(lownumberall - highnumberall );
# Get Slope for either direction
def slope = (a - b) / x;
def slopelow = (b - a) / x;
# Get Day
def day = GetDay();
def month = GetMonth();
def year = GetYear();
def lastDay = GetLastDay();
def lastmonth = GetLastMonth();
def lastyear = GetLastYear();
def isToday = If(day == lastDay and
month == lastmonth and
year == lastyear, 1, 0);
def istodaybarnumber = HighestAll(if isToday
then barnumber
else Double.NaN);
# Calculations for line between extremes
def line = b + (slope * (barnumber - lownumber));
def linelow = a + (slopelow * (barnumber - highnumber));
def currentlinelow = if barnumber <= lownumberall
then linelow
else Double.NaN;
def currentline = if barnumber <= highnumberall
then line
else Double.NaN;
def FibFan =  if downward
then currentlinelow
else if upward
then currentline
else Double.NaN;
# Rise of line between Extremes
def range =  a - b;
plot Fib1 = fold i = 1 to 100
with p = FibFan
while (downward and
barnumber >= highnumberall and
barnumber <= istodaybarnumber)
or
(upward and
barnumber >= lownumberall and
barnumber <= istodaybarnumber)
do if downward
then HighestAll((b + (range *  C0)))
else if upward
then HighestAll(a - (range * C0))
else Double.NaN;
}
# Start Plot Sequence
input C0 = 0.000;
input C1 = .236;
input C2 = .382;
input C3 = .500;
input C4 = .618;
input C5 = .786;
input C6 = 1.000;

def TotalBars = HighestAll(bar);
def HHbar = if high == highestAll(high)
then bar
else HHbar;
def LLbar = if low == lowestAll(low)
then bar
else LLbar;
def firstBar = if HHbar > LLbar
then LLbar
else if HHbar < LLbar
then HHbar
else Double.NaN;
def BubbleLocation =  bar == HighestAll(firstbar);
plot fib1 = Round(fibs(C0 = C0), 3);
fib1.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib1
, (c0 * 100) + "%  \$" + fib1, color.gray, yes);
plot fib2 = Round(fibs(C0 = C1), 3);
fib2.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib2
, (c1 * 100) + "%  \$" + fib2, color.gray, yes);
plot fib3 = Round(fibs(C0 = C2), 3);
fib3.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib3
, concat( (c2 * 100), "%"), color.gray, yes);
plot fib4 = Round(fibs(C0 = C3), 3);
fib4.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib4
, (c3 * 100) + "%  \$" + fib4, color.gray, yes);
plot fib5 = Round(fibs(C0 = C4), 3);
fib5.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib5
, (c4 * 100) + "%  \$" + fib5 , color.gray, yes);
plot fib6 = Round(fibs(C0 = C5), 3);
fib6.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib6
, (c5 * 100) + "%  \$" + fib6, color.gray, yes);
plot fib7 = Round(fibs(C0 = C6), 3);
fib7.SetDefaultColor(Color.Red);
AddChartBubble((if BubbleOn then BubbleLocation else double.nan), fib7
, (c6 * 100) + "%  \$" + fib7, color.gray, yes);
# Second Wave Fib Series
def UserSetResistance = TrendResistanceStart > 0 and
TrendResistanceEnd > 0;
def UserSetSupport = TrendSupportStart > 0 and
TrendSupportEnd > 0;
def PH;
def PL;
def isHigherThanNextBars = fold i = 1 to numBars + 1
with p = 1
while p
do h > GetValue(h, -i);
PH = if UserSetResistance and
( bar == TrendResistanceStart or
bar == TrendResistanceEnd )
then h
else if !UserSetResistance and
(bar > numBars and
h == Highest(h, numBars) and
isHigherThanNextBars)
then h
else Double.NaN;
def isLowerThanNextBars = fold j = 1 to numBars + 1
with q = 1
while q
do l < GetValue(low, -j);
PL = if UserSetSupport and
( bar == TrendSupportStart or
bar == TrendSupportEnd )
then l
else if !UserSetSupport and
(bar > numBars and
l == Lowest(l, numBars) and
isLowerThanNextBars)
then l
else Double.NaN;
def PHBar = if UserSetResistance
then TrendResistanceEnd
else if !IsNaN(PH)
then bar
else PHBar;
def PLBar = if UserSetSupport
then TrendSupportEnd
else if !IsNaN(PL)
then bar
else PLBar;
def PHL = if !IsNaN(PH)
then PH
else PHL;
def priorPHBar = if UserSetResistance
then TrendResistanceStart
else if PHL != PHL
then PHBar
else priorPHBar;
def PLL = if !IsNaN(PL)
then PL
else PLL;
def priorPLBar = if UserSetSupport
then TrendSupportStart
else if PLL != PLL
then PLBar
else priorPLBar;
def isFinalTwoHighPivots = bar >= HighestAll(priorPHBar);
def isFinalTwoLowPivots = bar >= HighestAll(priorPLBar);
def ResistanceFinishOffset = if isFinalTwoHighPivots
then bar - PHBar
else 0;
def ResistanceStartOffset = if isFinalTwoHighPivots
then bar - priorPHBar
else 0;
def ResistanceSlope = (GetValue(PH, ResistanceFinishOffset) -
GetValue(PH, ResistanceStartOffset)) /
(PHBar - priorPHBar);
def SupportFinishOffset = if isFinalTwoLowPivots
then bar - PLBar
else 0;
def SupportStartOffset = if isFinalTwoLowPivots
then bar - priorPLBar
else 0;
def SupportSlope = (GetValue(PL, SupportFinishOffset) -
GetValue(PL, SupportStartOffset)) /
(PLBar - priorPLBar);
def ResistanceExtend = if bar == HighestAll(PHBar)
then 1
else ResistanceExtend;
def SupportExtend = if bar == HighestAll(PLBar)
then 1
else SupportExtend;
plot pivotHigh = if isFinalTwoHighPivots
then PH
else Double.NaN;
def pivotHighLine = if PHL > 0 and
isFinalTwoHighPivots
then PHL
else double.NaN;
plot ResistanceLine = pivotHigh;
plot ResistanceExtension = if ResistanceExtend
then (bar - PHBar) * ResistanceSlope + PHL
else Double.NaN;
plot pivotLow = if isFinalTwoLowPivots
then PL
else Double.NaN;
def pivotLowLine = if PLL > 0 and
isFinalTwoLowPivots
then PLL
else double.NaN;
plot SupportLine = pivotLow;
plot SupportExtension = if SupportExtend
then (bar - PLBar) * SupportSlope + PLL
else Double.NaN;
plot BN = bar;
plot A_H = if isNaN(PivotHighline)
then PivotHighLine
else HighestAll(PivotHighLine);
A_H.SetDefaultColor(Color.Yellow);
plot X_L = if isNaN(PivotLowLine)
then PivotLowLine
else LowestAll(PivotLowLine);
X_L.SetDefaultColor(Color.Yellow);
def A = A_H;
def B = X_L;
def X = ( ((c - X_L) / (A_H - X_L))) + c;
plot SeventyEight = (((a - b) * Coeff_5) + B);
plot SixtyOne    = (((a - B) * Coeff_4) + B);
plot Fifty       = (a + B) * Coeff_3;
plot ThirtyEight = ((a - B) * Coeff_2) + B;
plot TwentyThree = ((a - B) * Coeff_1) + B;
pivotHigh.SetDefaultColor(Color.Yellow);
pivotHigh.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.SetHiding(!showValues);
pivotLow.SetDefaultColor(Color.Yellow);
pivotLow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.SetHiding(!showValues);
ResistanceLine.EnableApproximation();
ResistanceLine.SetDefaultColor(GetColor(5));
ResistanceExtension.SetDefaultColor(GetColor(5));
SupportLine.EnableApproximation();
SupportLine.SetDefaultColor(GetColor(5));
SupportExtension.SetDefaultColor(GetColor(5));
BN.SetDefaultColor(GetColor(0));
BN.SetHiding(!showBarNumbers);
BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
SeventyEight.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SeventyEight.SetDefaultColor(Color.Yellow);
SixtyOne.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SixtyOne.SetDefaultColor(Color.Yellow);
Fifty.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Fifty.SetDefaultColor(Color.Yellow);
ThirtyEight.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ThirtyEight.SetDefaultColor(Color.Yellow);
TwentyThree.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TwentyThree.SetDefaultColor(Color.Yellow);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
A_H,
"Fib " + 1,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
SeventyEight,
"Fib " + Coeff_5,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
SixtyOne,
"Fib " + Coeff_4,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
Fifty,
"Fib " + Coeff_3,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
ThirtyEight,
"Fib " + Coeff_2,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
TwentyThree,
"Fib " + Coeff_1,
color.yellow,
yes);
AddChartBubble(if BubbleOn then isNaN(close) and !isNaN(close) else double.nan,
X_L,
"Fib " + 0,
color.yellow,
yes);

# End Code``````

• gregoryK and PlantainPapi

#### jaslynn

##### New member
Hi Ben, May I know if this Fibonacci generated based on the premarket activity as well? Is there anyway to generate Fibonacci based on the market open and close (9.30am - 4pm)? Thanks

Staff member
Staff
VIP

#### zeek

##### Active member
2019 Donor
Wondering if i can change the color of the Fib lines myself to something else or will that make the script non-working?
If so, can i use the color codes from this thread and simply change the "color.red" codes for each Fib line?

Love this script btw!

• markos

#### thinky

##### Member
@zeek Yes, is that simple. It would be better if code made it possible to change colors in the study's settings.

• markos

#### Billions

##### Active member
2019 Donor
VIP
@BenTen Are you releasing Magic fibs today for vip members?

Staff member
Staff
VIP

#### soary

##### Member
Hi, I added it to studies and I get lines that go across the entire chart without any labeling. I tried to edit by clicking on edit studies and checked no for both on expansion and extend to the left and it had no effect.Is there something I don't know? Thanks

#### BenTen

Staff member
Staff
VIP
@soary Please share a screenshot of your chart. If you don't know how to post a screenshot, see this post.

#### soary

##### Member
It looks like it took awhile for TOS to adjust because now it shows good, but now the problem is that zero is the high and 100 is the low and I would like it to be like in your picture with 100 as the high and zero as the low. How do I change it? Thanks

Is anyone using auto fibs to tell me how to get it like in the pic on here with 100 as the high and zero as the low. How do I change it because I copied and pasted the code as it is on here and right now it shows 100 as the low and 0 as the high. `I am not a programmer and only know a little thinkscript so you will need to write out the code for the response for me to copy and paste or give good explicit instructions for me to follow. Thanks

#### horserider

##### Well-known member
VIP
@soary Please learn how fibs work. If drawn from a high to a low as above the low is 0 and high is 100. So as price moves back up from the 0 low point it will give you the % lines as it moves up. Going all the way up to the previous high point would then be a 100% retrace.
If drawn from a low to a high it is reversed. Now the high is 0 and the low is 100. Price is expected to retrace down to 100% start.

• markos

#### Nick

##### Active member
2019 Donor
VIP
Hi, found few useful Fibonacci auto level plots here, was wonder if we can configure those plots to a scanner to scan out only those stocks with it price touches predetermined fibo level eg 61.8. Thanks

• Likos

#### soary

##### Member
@soary Please learn how fibs work. If drawn from a high to a low as above the low is 0 and high is 100. So as price moves back up from the 0 low point it will give you the % lines as it moves up. Going all the way up to the previous high point would then be a 100% retrace.
If drawn from a low to a high it is reversed. Now the high is 0 and the low is 100. Price is expected to retrace down to 100% start.

horserider, thanks for your help but you should not always assume that the problem is not knowing how to use the instrument. I've been using fibs for years but tired of drawing them and really apreciate getting the script. Explaining how they work as an automated instrument would have sufficed. I thought I would need a different script for bear and another for bull trend, but the script works better than what I thought because it sounds like what you are saying is that the fibs change automatically by themselves when the chart trend changes from bear to bull and vice-versa.

#### horserider

##### Well-known member
VIP
@soary My apologies. Sounds like you understand the study now.

• jerome

#### soary

##### Member
No problem horserider. I''m really enjoying them on my charts.
For anyone else that adding auto fib to your chart, click on the study to modify after adding it and click on show bubbles so you can see the labels of the Fib levels, otherwise you'll have the problem I mentioned above where it will show up as only horizontal lines across your entire chart without designation.

#### soary

##### Member
I need help with this fib again. I've been trading low priced stocks so the bubbles are usually overlapping and I didn't notice this before. Today, I was looking at low priced stocks and thought I was seeing two lines with the 50% bubble label, but looked at the SPY on the daily which since it's a higher price ticker the fib lines are spread out and I can see each bubble clearly and saw the problem. For the 50% line there are two bubbles one on top of the other and both bubbles say 50%. it's not an extra line it just that the 50% line has 2 bubbles and this is confusing. I'm not a coder so how do I fix this? Thanks in advance. I can't post a screen shot due to the set up here because that's another learning curve at another site, but the bubbles are one on top of the other. Is it a setting I need to turn off or something?

#### horserider

##### Well-known member
VIP
@soary Make sure you do not have 2 of the Fib studies on the chart. That will give you one line but 2 bubbles.

• jerome