Auto Fib (Fibonacci) Levels Indicator for ThinkorSwim

tomsk

Well-known member
VIP
Folks here is version 1.3 of the Auto Fib study that now displays the bubbles on the right of the chart.
Please ensure that you increase the expansion area to that the bubbles have room to be displayed
Personally I set that to 22 on my chart settings

Chart Settings > Time Axis > Expansion Area

Given several changes to this code from the first time it was posted by @BenTen in 2018, I have created a change log
It details the various modifications to the base code that was made including a really nice cleaned up version from @theelderwand

Hope this helps

Code:
# Auto Fib V1.3
# tomsk
# 11.19.2019

# 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%.

# CHANGE LOG
#
# V1.0 - 12.18.2018 - BenTen       - Initial release of Auto Fib, created by Ryan Hendricks
# V1.1 - 11.15.2019 - theelderwand - As script was difficult to read, made the following enhancements
#                                    Expands to right
#                                    Doesn't expand to left
#                                    Custom colors for Fibonacci bars (0.618 is GOLD color)
#                                    Custom line weights
#                                    Code is modularized so you can add extra plots as needed
# V1.2 - 11.15.2019 - tomsk        - Added an input selector for the colors of the label. You
#                                    can select from any of the colors listed - red, orange,
#                                    green, etc and bubbles for all the fib retracements will
#                                    utilize that color.
# V1.3 - 11.19.2019 - tomsk        - Modified the AddChartBubbles to be displayed on the right
#                                    side of the chart. Please ensure that you increase the
#                                    expansion area to that the bubbles have room to be displayed
#                                    Chart Settings > Time Axis > Expansion Area

#hint Price: Price used in the alerts on crossing retracement lines. <b>(Default is Close)</b>

#hint coefficient_0: 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: coefficient_0
#wizard text: coefficient_0:
#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 coefficient_0 = 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;

input LabelColor = {default "MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", "GRAY", "WHITE"};
input n = 3;

def n1  = n + 1;
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[1] else c, c);
def highnumberall = HighestAll(highnumber);
rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] 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;

def value0 = range * coefficient_0;
def value1 = range * coefficient_1;
def value2 = range * coefficient_2;
def value3 = range * coefficient_3;
def value4 = range * coefficient_4;
def value5 = range * coefficient_5;
def value6 = range * coefficient_6;

def condition1 = downward and barnumber >= highnumberall;
def condition2 = upward and barnumber >= lownumberall;

Plot Retracement0 = if condition1 then highestall(b + value0) else if condition2 then highestall(a - value0) else double.nan;
Plot Retracement1 = if condition1 then highestall(b + value1) else if condition2 then highestall(a - value1) else double.nan;
Plot Retracement2 = if condition1 then highestall(b + value2) else if condition2 then highestall(a - value2) else double.nan;
Plot Retracement3 = if condition1 then highestall(b + value3) else if condition2 then highestall(a - value3) else double.nan;
Plot Retracement4 = if condition1 then highestall(b + value4) else if condition2 then highestall(a - value4) else double.nan;
Plot Retracement5 = if condition1 then highestall(b + value5) else if condition2 then highestall(a - value5) else double.nan;
Plot Retracement6 = if condition1 then highestall(b + value6) else if condition2 then highestall(a - value6) else double.nan;

Retracement0.assignvaluecolor(CreateColor(255,255,255));
Retracement0.setLineWeight(4);
retracement0.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);

Retracement1.assignvaluecolor(CreateColor(173,216,230));
Retracement1.setLineWeight(2);
retracement1.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement1, concat( (coefficient_1 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement1, concat( (coefficient_1 * 100), "%"), GetColor(LabelColor), yes);

Retracement2.assignvaluecolor(CreateColor(0,197,49));
Retracement2.setLineWeight(2);
retracement2.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);

Retracement3.assignvaluecolor(CreateColor(255,64,64));
Retracement3.setLineWeight(3);
retracement3.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement3, concat( (coefficient_3 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement3, concat( (coefficient_3 * 100), "%"), GetColor(LabelColor), yes);

Retracement4.assignvaluecolor(CreateColor(255,215,0));
Retracement4.setLineWeight(5);
retracement4.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);

Retracement5.assignvaluecolor(CreateColor(0,255,255));
Retracement5.setLineWeight(2);
retracement5.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement5, concat( (coefficient_5 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement5, concat( (coefficient_5 * 100), "%"), GetColor(LabelColor), yes);

Retracement6.assignvaluecolor(CreateColor(255,255,255));
Retracement6.setLineWeight(4);
Retracement6.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement6, concat( (coefficient_6 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement6, concat( (coefficient_6 * 100), "%"), GetColor(LabelColor), 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");
# End Auto Fib v1.3
 
Last edited by a moderator:

BenTen

Administrative
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.

Z7hCOks.png


thinkScript Code

Ruby:
# Auto Fib V1.3
# tomsk
# 11.19.2019

# 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%.

# CHANGE LOG
#
# V1.0 - 12.18.2018 - BenTen       - Initial release of Auto Fib, created by Ryan Hendricks
# V1.1 - 11.15.2019 - theelderwand - As script was difficult to read, made the following enhancements
#                                    Expands to right
#                                    Doesn't expand to left
#                                    Custom colors for Fibonacci bars (0.618 is GOLD color)
#                                    Custom line weights
#                                    Code is modularized so you can add extra plots as needed
# V1.2 - 11.15.2019 - tomsk        - Added an input selector for the colors of the label. You
#                                    can select from any of the colors listed - red, orange,
#                                    green, etc and bubbles for all the fib retracements will
#                                    utilize that color.
# V1.3 - 11.19.2019 - tomsk        - Modified the AddChartBubbles to be displayed on the right
#                                    side of the chart. Please ensure that you increase the
#                                    expansion area to that the bubbles have room to be displayed
#                                    Chart Settings > Time Axis > Expansion Area

#hint Price: Price used in the alerts on crossing retracement lines. <b>(Default is Close)</b>

#hint coefficient_0: 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: coefficient_0
#wizard text: coefficient_0:
#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 coefficient_0 = 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;

input LabelColor = {default "MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", "GRAY", "WHITE"};
input n = 3;

def n1  = n + 1;
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[1] else c, c);
def highnumberall = HighestAll(highnumber);
rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] 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;

def value0 = range * coefficient_0;
def value1 = range * coefficient_1;
def value2 = range * coefficient_2;
def value3 = range * coefficient_3;
def value4 = range * coefficient_4;
def value5 = range * coefficient_5;
def value6 = range * coefficient_6;

def condition1 = downward and barnumber >= highnumberall;
def condition2 = upward and barnumber >= lownumberall;

Plot Retracement0 = if condition1 then highestall(b + value0) else if condition2 then highestall(a - value0) else double.nan;
Plot Retracement1 = if condition1 then highestall(b + value1) else if condition2 then highestall(a - value1) else double.nan;
Plot Retracement2 = if condition1 then highestall(b + value2) else if condition2 then highestall(a - value2) else double.nan;
Plot Retracement3 = if condition1 then highestall(b + value3) else if condition2 then highestall(a - value3) else double.nan;
Plot Retracement4 = if condition1 then highestall(b + value4) else if condition2 then highestall(a - value4) else double.nan;
Plot Retracement5 = if condition1 then highestall(b + value5) else if condition2 then highestall(a - value5) else double.nan;
Plot Retracement6 = if condition1 then highestall(b + value6) else if condition2 then highestall(a - value6) else double.nan;

Retracement0.assignvaluecolor(CreateColor(255,255,255));
Retracement0.setLineWeight(4);
retracement0.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);

Retracement1.assignvaluecolor(CreateColor(173,216,230));
Retracement1.setLineWeight(2);
retracement1.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement1, concat( (coefficient_1 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement1, concat( (coefficient_1 * 100), "%"), GetColor(LabelColor), yes);

Retracement2.assignvaluecolor(CreateColor(0,197,49));
Retracement2.setLineWeight(2);
retracement2.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);

Retracement3.assignvaluecolor(CreateColor(255,64,64));
Retracement3.setLineWeight(3);
retracement3.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement3, concat( (coefficient_3 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement3, concat( (coefficient_3 * 100), "%"), GetColor(LabelColor), yes);

Retracement4.assignvaluecolor(CreateColor(255,215,0));
Retracement4.setLineWeight(5);
retracement4.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);

Retracement5.assignvaluecolor(CreateColor(0,255,255));
Retracement5.setLineWeight(2);
retracement5.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement5, concat( (coefficient_5 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement5, concat( (coefficient_5 * 100), "%"), GetColor(LabelColor), yes);

Retracement6.assignvaluecolor(CreateColor(255,255,255));
Retracement6.setLineWeight(4);
Retracement6.hidebubble();
AddChartBubble((downward and close[n1]) and IsNaN(close[n]), retracement6, concat( (coefficient_6 * 100), "%"), GetColor(LabelColor), yes);
AddChartBubble((upward and close[n1]) and IsNaN(close[n]), retracement6, concat( (coefficient_6 * 100), "%"), GetColor(LabelColor), 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");
# End Auto Fib v1.3

Shareable Link

https://tos.mx/Fz8Ss0
 
Last edited by a moderator:

tomsk

Well-known member
VIP
Fibonacci Uptrend Retracement Scan
Since the market is currently in an uptrend, my assumption is that you'd like to scan for uptrend stocks that have retraced to either the 38.2%, 50%, or 61.8% levels. The way the code is written, it also has the capability to calculate retracement levels for downtrending stocks. That said, I have converted the code base you posted earlier to scan for uptrending stocks that have made a 38.2% retracement.

Code:
# Fibonacci Uptrend Retracement Scan
# tomsk
# 1.3.2020

#hint coefficient_0: 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>

input price = close;
input high = high;
input low = low;
input coefficient_0 = 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;
def highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c);
def highnumberall = HighestAll(highnumber);
def lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] 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;

def range = a - b;

def value0 = range * coefficient_0;
def value1 = range * coefficient_1;
def value2 = range * coefficient_2;
def value3 = range * coefficient_3;
def value4 = range * coefficient_4;
def value5 = range * coefficient_5;
def value6 = range * coefficient_6;

def condition1 = downward and barnumber >= highnumberall;
def condition2 = upward and barnumber >= lownumberall;

def Retracement0 = if condition1 then highestall(b + value0) else if condition2 then highestall(a - value0) else double.nan;
def Retracement1 = if condition1 then highestall(b + value1) else if condition2 then highestall(a - value1) else double.nan;
def Retracement2 = if condition1 then highestall(b + value2) else if condition2 then highestall(a - value2) else double.nan;
def Retracement3 = if condition1 then highestall(b + value3) else if condition2 then highestall(a - value3) else double.nan;
def Retracement4 = if condition1 then highestall(b + value4) else if condition2 then highestall(a - value4) else double.nan;
def Retracement5 = if condition1 then highestall(b + value5) else if condition2 then highestall(a - value5) else double.nan;
def Retracement6 = if condition1 then highestall(b + value6) else if condition2 then highestall(a - value6) else double.nan;

plot scan = close crosses above retracement2 and upward;

# End Fibonacci Uptrend Retracement Scan


In the event you'd like to scan for deeper retracement levels,, all you got to do is to revise the following line to reflect the level you'd like to scan for. The 50% retracement level is held by the variable retracement3 while that for 61.8% is help by the variable retracement4. Scanning the S&P 500 at the 38.2% retracement level, these is currently only 1 result. At the 50%, there are 2 results.

Code:
plot scan = close crosses above retracement2 and upward;
 
Last edited by a moderator:

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 tradingDays = CountTradingDays(yearstart, GetYYYYMMDD());
   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[1]
                                    else c, c);
    def highnumberall = HighestAll(highnumber);
    def lownumber = CompoundValue(1, if IsNaN(d)
                                   then lownumber[1]
                                   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[1];
   def LLbar = if low == lowestAll(low)
               then bar
               else LLbar[1];
   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[1];
   def PLBar = if UserSetSupport
               then TrendSupportEnd
               else if !IsNaN(PL)
               then bar
               else PLBar[1];
   def PHL = if !IsNaN(PH)
             then PH
             else PHL[1];
   def priorPHBar = if UserSetResistance
                    then TrendResistanceStart
                    else if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
   def PLL = if !IsNaN(PL)
             then PL
             else PLL[1];
   def priorPLBar = if UserSetSupport
                    then TrendSupportStart
                    else if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
   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[1];
   def SupportExtend = if bar == HighestAll(PLBar)
                       then 1
                       else SupportExtend[1];
  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[1]
             else HighestAll(PivotHighLine);
       A_H.SetDefaultColor(Color.Yellow);
  plot X_L = if isNaN(PivotLowLine)
             then PivotLowLine[1]
             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[10]) and !isNaN(close[11]) else double.nan,
                                A_H,
                                "Fib " + 1,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                SeventyEight,
                                "Fib " + Coeff_5,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                SixtyOne,
                                "Fib " + Coeff_4,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                Fifty,
                                "Fib " + Coeff_3,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                ThirtyEight,
                                "Fib " + Coeff_2,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                TwentyThree,
                                "Fib " + Coeff_1,
                                color.yellow,
                                yes);
AddChartBubble(if BubbleOn then isNaN(close[10]) and !isNaN(close[11]) else double.nan,
                                X_L,
                                "Fib " + 0,
                                color.yellow,
                                yes);

# End Code
 

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
 

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!
 

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
 

soary

Member
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.
 
Last edited by a moderator:

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?
 

soary

Member
@soary Make sure you do not have 2 of the Fib studies on the chart. That will give you one line but 2 bubbles.

Thanks to make sure I placed it on a new chart by itself with only underlying volume indicator and saw that it doesn't do it with all tickers. Not with RUT or IWM but yes with QQQ, SPX and SPY. That is so weird. There's a ghost buble in my charts.
 

flyer01

New member
Are there any options to define a lookback timeframe in this script. For example, when trading /ES intraday, it would be very helpful to draw the fibs from the lowest/highest points between 12:00AM the previous day and the current bar.

I can't thank you guys enough for the information on this forum!
 

AspaTrader

Member
2019 Donor
VIP
I always wonder how helpful is fib in an uptrend :) Say, for example, If you see ES_F, It is still running when we have 100% on the fib levels?? Don't you think what we are seeing is absolutely wrong? All the Autofib scripts are gonna take the high low and plot fibs for you, But it never takes Extensions into consideration :) Say 123.6% from the fib level I plotted, etc ... Do you people agree?
 

flyer01

New member
In a strong uptrend there are still a few point pullbacks. The first intraday (two day) fib level is usually makes for a good entry if you are not already in.
 

Similar threads

Top