Auto Fib (Fibonacci) Levels Indicator for ThinkorSwim

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
I am sorry, newbie question. Is there any way the fib label can be moved to the right side? Thank you.
 
@ChadAnt here is the Study for AutoFib, But I guess you may have this already.

https://usethinkscript.com/threads/auto-fib-fibonacci-levels-indicator-for-thinkorswim.14/

Here is the 5 line code you are looking for as an addition to this study, all you have to do is scan for that gpArrow from this script on Weekly charts.

Ruby:
def gpH = value4 + (value4 * .05);
def gPL = value4 + (value4 * .01);
def gp = if price > gpL and price < gPH then 1 else Double.NaN;

plot gpArrow = if gp then low else Double.NaN;
gpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Hi if i wanted a custom scan to find stocks that are in the zone (between 50% - 62%) levels how can i create this? can i have help please

Hi @SuryaKiranC im a little confused on how to put this in my custom field for the scan. Do i only need those 5 lines of code? or does the whole script with the 5 lines of code need to be included. All i am trying to do is scan for these stocks
 
Last edited by a moderator:
Hi if i wanted a custom scan to find stocks that are in the zone (between 50% - 62%) levels how can i create this? can i have help please

Hi @SuryaKiranC im a little confused on how to put this in my custom field for the scan. Do i only need those 5 lines of code? or does the whole script with the 5 lines of code need to be included. All i am trying to do is scan for these stocks
add the 5 lines to your study. Scan for gpArrow is true
 
See if this helps. Picture is set to showtodayonly == yes. You can move the bubbles left/right at the bubblemover input.
Hi all, new to the forum, I was looking for a way to "ToS-ify" one of my MT4 indicators which led me here.

@SleepyZ - thanks for your Auto_FibPrevDay code, it was exactly what I needed for my project, which is a recreation of a "Bank Trader Strategy" indicator that I've had for years in Metatrader. It looks fancy, but ultimately it's just a different way of using Fib retracements and extensions for buy/sell decisions and profit targets. It was intended for Forex but it seems to work fairly well on Futures as well.

es_bts_20211222.png


Ruby:
#Bank Trader Strategy Study
#Based on Auto_FibPrevDay by SleepyZ on usethinkscript.com - plots high/low from previous day and fib levels based thereon

input ShowTodayOnly = {"No", default "Yes"};
input showbubbles   = yes;
input showBuySellZone = yes;
input bubblemover   = 2; #Hint n: Number of Bars to shift bubble to the right
def bubblemover2   = bubblemover + 3;
def period        = AggregationPeriod.DAY;
def periodWk      = AggregationPeriod.WEEK;
input displace      = 1;

#ADR
def length = 1;

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, length);

plot RH = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else open(period = AggregationPeriod.DAY) + (AvgRange/2);
plot RL = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else open(period = AggregationPeriod.DAY) - (AvgRange/2);

RH.SetDefaultColor(Color.CYAN);
RH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RH.SetLineWeight(1);

RL.SetDefaultColor(Color.DARK_RED);
RL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RL.SetLineWeight(1);

#Yesterday's high/low
plot YH = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else high(period = period)[displace];
plot YL = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else  low(period = period)[displace];
plot OOD = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else open(period = period);

YH.SetDefaultColor(Color.VIOLET);
YH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YH.SetLineWeight(1);

YL.SetDefaultColor(Color.RED);
YL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YL.SetLineWeight(1);

#Open of the day
OOD.SetDefaultColor(Color.WHITE);
OOD.SetPaintingStrategy(PaintingStrategy.LINE);
OOD.SetLineWeight(3);

#Open of the week
plot OOW = open(period = periodWk);

OOW.SetDefaultColor(CreateColor(228,188,0));
OOW.SetPaintingStrategy(PaintingStrategy.POINTS);
OOW.SetLineWeight(3);

#Previous week high/low
def prevWkHigh = high(period = periodWk)[displace];
def prevWkLow =  low(period = periodWk)[displace];

plot PWH = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else prevWkHigh;
plot PWL = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else prevWkLow;

PWH.SetDefaultColor(Color.DARK_ORANGE);
PWH.SetPaintingStrategy(PaintingStrategy.LINE);
PWH.SetLineWeight(3);

PWL.SetDefaultColor(Color.YELLOW);
PWL.SetPaintingStrategy(PaintingStrategy.LINE);
PWL.SetLineWeight(3);

#Fib levels
input F1 = -1.00;
input F2 = -0.618;
input F3 = -0.414;
input F4 = -0.272;
input F5 = 0.236;
input F6 = 0.382;
input F7 = 0.500;
input F8 = 0.618;
input F9 = 0.764;
input F10 = 1.00;
input F11 = 1.272;
input F12 = 1.414;
input F13 = 1.618;
input F14 = 2.00;

#Fib calculations
def rHi =  YH;
def rLo =  YL;

def range  = rHi - rLo;
plot LT4   = rLo + (range * F1);
plot LT3   = rLo + (range * F2);
plot LT2   = rLo + (range * F3);
plot LT1   = rLo + (range * F4);
def BSZ   = rLo + (range * F5);
plot IRL   = rLo + (range * F6);
plot DM   = rLo + (range * F7);
plot IRH   = rLo + (range * F8);
def TBZ   = rLo + (range * F9);
def ZERO  = rLo + (range * F10);
plot HT1  = rLo + (range * F11);
plot HT2  = rLo + (range * F12);
plot HT3  = rLo + (range * F13);
plot HT4  = rLo + (range * F14);

def rangeWk = prevWkHigh - prevWkLow;
plot wLT2 = prevWkLow + (rangeWk * F3);
plot wLT1 = prevWkLow + (rangeWk * F4);
plot wHT1 = prevWkLow + (rangeWk * F11);
plot wTH2 = prevWkLow + (rangeWk * F12);

#Draw the lines
LT4.SetDefaultColor(Color.MAGENTA);
LT4.SetPaintingStrategy(PaintingStrategy.DASHES);
LT4.SetLineWeight(1);

LT3.SetDefaultColor(Color.MAGENTA);
LT3.SetPaintingStrategy(PaintingStrategy.DASHES);
LT3.SetLineWeight(1);

LT2.SetDefaultColor(Color.MAGENTA);
LT2.SetPaintingStrategy(PaintingStrategy.DASHES);
LT2.SetLineWeight(1);

LT1.SetDefaultColor(Color.MAGENTA);
LT1.SetPaintingStrategy(PaintingStrategy.DASHES);
LT1.SetLineWeight(1);

#BSZ.SetDefaultColor(Color.RED);
#BSZ.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#BSZ.SetLineWeight(1);

IRL.SetDefaultColor(Color.PLUM);
IRL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
IRL.SetLineWeight(1);

DM.SetDefaultColor(Color.GRAY);
DM.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DM.SetLineWeight(1);

IRH.SetDefaultColor(Color.DARK_GREEN);
IRH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
IRH.SetLineWeight(1);

#TBZ.SetDefaultColor(Color.BLUE);
#TBZ.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#TBZ.SetLineWeight(1);

#ZERO.SetDefaultColor(Color.GREEN);
#ZERO.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#ZERO.SetLineWeight(1);

HT1.SetDefaultColor(Color.GREEN);
HT1.SetPaintingStrategy(PaintingStrategy.DASHES);
HT1.SetLineWeight(1);

HT2.SetDefaultColor(Color.GREEN);
HT2.SetPaintingStrategy(PaintingStrategy.DASHES);
HT2.SetLineWeight(1);

HT3.SetDefaultColor(Color.GREEN);
HT3.SetPaintingStrategy(PaintingStrategy.DASHES);
HT3.SetLineWeight(1);

HT4.SetDefaultColor(Color.GREEN);
HT4.SetPaintingStrategy(PaintingStrategy.DASHES);
HT4.SetLineWeight(1);

wLT2.SetDefaultColor(Color.MAGENTA);
wLT2.SetPaintingStrategy(PaintingStrategy.POINTS);
wLT2.SetLineWeight(1);

wLT1.SetDefaultColor(Color.MAGENTA);
wLT1.SetPaintingStrategy(PaintingStrategy.POINTS);
wLT1.SetLineWeight(1);

wHT1.SetDefaultColor(Color.GREEN);
wHT1.SetPaintingStrategy(PaintingStrategy.POINTS);
wHT1.SetLineWeight(1);

wTH2.SetDefaultColor(Color.GREEN);
wTH2.SetPaintingStrategy(PaintingStrategy.POINTS);
wTH2.SetLineWeight(1);

def   b           = bubblemover;
def   b1          = b + 1;

def   b2          = bubblemover2;
def   b21          = b2 + 1;

#Add bubbles
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), LT4[b], "LT4", LT4.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), LT3[b], "LT3", LT3.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), LT2[b], "LT2", LT2.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), LT1[b], "LT1", LT1.Takevaluecolor());
#AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), BSZ[b], "BSZ", BSZ.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), IRL[b], "IRL", IRL.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), DM[b], "DM", DM.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), IRH[b], "IRH", IRH.Takevaluecolor());
#AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), TBZ[b], "TBZ", TBZ.Takevaluecolor());
#AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), ZERO[b], "ZERO", ZERO.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), HT1[b], "HT1", HT1.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), HT2[b], "HT2", HT2.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), HT3[b], "HT3", HT3.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), HT4[b], "HT4", HT4.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), YH[b], "YH", YH.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b]) and !IsNaN(close[b1]), YL[b], "YL", YL.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), RH[b2], "RH", RH.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), RL[b2], "RL", RL.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), PWH[b2], "PWH", PWH.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), PWL[b2], "PWL", PWL.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), OOD[b2], "OpD", OOD.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), OOW[b2], "OpW", OOW.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), wLT2[b2], "wLT2", wLT2.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), wLT1[b2], "wLT1", wLT1.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), wHT1[b2], "wHT1", wHT1.Takevaluecolor());
AddChartBubble(showbubbles == yes and IsNaN(close[b2]) and !IsNaN(close[b21]), wTH2[b2], "wHT2", wTH2.Takevaluecolor());

#Add shading
AddCloud(if showBuySellZone then TBZ else double.NaN, IRH, color.DARK_GREEN, color.DARK_GREEN);
AddCloud(if showBuySellZone then IRH else double.NaN, IRL, color.GRAY, color.GRAY);
AddCloud(if showBuySellZone then IRL else double.NaN, BSZ, color.DARK_RED, color.DARK_RED);

Ideally I'd like to be able to change the starting time of the fib range, so it can calculate and draw the fibs from different previous trading ranges - I.e. London open from 0300 to 0259, or New York open from 0800 to 0759. If anyone has suggestions on the best way to program that it would be appreciated.
 
Last edited:
Hey all, I see a few posts on here that convert this to a script but I've been boning up on my thinkscript skills and did my own version of this. I kept in most of the controls and added a few items. Namely:
1. can now set fibs as labels instead of bubbles
2. show/hide retracement lines as setting
3. choose your line, bubble and label color (all the same)

I also cleaned up the bubbles so it's fewer lines and inverted the 0/100 ranges so 100% is now the top (personal preference).

Anyway, hope someone finds this useful.
Code:
declare upper;
#===================================================
#          INPUTS & GLOBAL VARIABLES
#===================================================

input show_bubbles      = {default Right, Left, Labels, None};
input show_retracements = {Default All, Closest, None};
input Line_color        = {Default Magenta, Cyan, Pink, White, Gold, Red, Green, Dark_Gray, Yellow};

input _H           = high;
input _L           = low;
input Expand_Right = Yes;
input Extend_Left  = no;
input Coeffient_0  = 0.000;
input Coeffient_1  = .236;
input Coeffient_2  = .382;
input Coeffient_3  = .500;
input Coeffient_4  = .618;
input Coeffient_5  = .786;
input Coeffient_6  = 1.000;

def price = close;
def _nan = Double.NaN;
def a = HighestAll(high);
def b = LowestAll(low);
def _BN = BarNumber();
def c = if high == a then _BN else Double.NaN;
def d = if low == b then _BN else Double.NaN;
rec _HN = CompoundValue(1, if IsNaN(c) then _HN[1] else c, c);
def _HNA = HighestAll(_HN);
rec _LN = CompoundValue(1, if IsNaN(d) then _LN[1] else d, d);
def _LNA = LowestAll(_LN);

def x = AbsValue(_LNA - _HNA );

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

def line = b + (slope * (_BN - _LN));
def linelow = a + (slopelow * (_BN - _HN));
def currentlinelow = if _BN <= _LNA then linelow else Double.NaN;
def currentline = if _BN <= _HNA then line else Double.NaN;

#shortened variables for retracements script call
def _CFF0 = Coeffient_0;
def _CFF1 = Coeffient_1;
def _CFF2 = Coeffient_2;
def _CFF3 = Coeffient_3;
def _CFF4 = Coeffient_4;
def _CFF5 = Coeffient_5;
def _CFF6 = Coeffient_6;

def _ER  = Expand_Right;
def _EL  = Extend_Left;
def _HH  = a;
def _LL  = b;
def _DELTA =
    if _HNA > _LNA then -1 else
    if _HNA < _LNA then 1 else _nan;

#===================================================
#                 INPUT PROCESSING
#===================================================

#chart bubble and retracement line parameters
def position;
def placement;
def shade;
def HideRet;
def closest;
Switch (Line_Color){
Case Magenta:
    shade = 0;
Case Cyan:
    shade = 1;
Case Pink:
    shade = 2;
Case White:
    shade = 3;
Case Gold:
    shade = 4;
Case Red:   
    shade = 5;
Case Green:   
    shade = 6;
Case Dark_Gray:   
    shade = 7;
Case Yellow:
    shade = 8;
};
switch (show_bubbles){
case None:
    position  = _nan;
#    shade     = 9;
    placement = _nan;
case Labels:
    position  = _nan;
#    shade     = 9;
    placement = -1;
case Right:
    position  =
        if _DELTA != 0 and _BN == HighestAll(_BN)
        then Highestall(_BN) else _nan;
#    shade     = 4;
    placement = 0;
case Left:
    position  =
        if _DELTA > 0 and _BN == HighestAll(_LNA)
        then highestall(_LNA) else
        if _DELTA < 0 and _BN == HighestAll(_HNA)
        then highestall(_HNA) else _nan ;
#    shade     = 5;
    placement = 1;
};

#===================================================
#              RETRACEMENTS SCRIPT
#===================================================
script findRetracement {
    input _DELTA = 0;
    input _CFF  = 0.000;
    input _ER   = 0;
    input _EL   = 0;
    input _LL   = 0;
    input _HH   = 0;
    input _HNA  = 0;
    input _LNA  = 0;
    input _BN   = 0;

    def _nan = Double.NaN;
    def day = GetDay() == GetLastDay();
    def month = GetMonth() == GetLastMonth();
    def year = GetYear() == GetLastYear();

    def isToday = If(day and month and year, 1, 0);
    def istodaybN = HighestAll(if isToday then _BN else Double.NaN);

    def _range =  _HH - _LL;

    plot result =
         if _DELTA < 0 and !_ER and !_EL and _BN >= _HNA and _BN <= istodaybN
         then HighestAll(_LL + (_range * _CFF))
    else if _DELTA > 0 and !_EL and !_ER and _BN >= _LNA and _BN <= istodaybN
         then HighestAll(_HH - (_range * _CFF)) #
    else if _DELTA < 0 and _ER and !_EL and _BN >= _HNA
         then HighestAll(_LL + (_range * _CFF))
    else if _DELTA > 0 and _ER and !_EL and _BN >= _LNA               
         then HighestAll(_HH - (_range * _CFF))
    else if _DELTA < 0 and !_ER and _EL and _BN <= istodaybN
         then HighestAll(_LL + (_range * _CFF))
    else if _DELTA > 0 and _EL and !_ER and _BN <= istodaybN
         then HighestAll(_HH - (_range * _CFF))
    else if _DELTA < 0 and _ER and _EL
         then HighestAll(_LL + (_range * _CFF))
    else if _DELTA > 0 and _ER and _EL
         then HighestAll(_HH - (_range * _CFF))
    else _nan;
}
;

#===================================================
#                    PLOTS
#===================================================
plot FibFan = 
     if _DELTA < 0 then currentlinelow
else if _DELTA > 0 then currentline
else Double.NaN;

plot Retracement0 =
    findRetracement(_DELTA, _CFF0, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement1 =
    findRetracement(_DELTA, _CFF1, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement2 =
    findRetracement(_DELTA, _CFF2, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement3 =
    findRetracement(_DELTA, _CFF3, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement4 =
    findRetracement(_DELTA, _CFF4, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement5 =
    findRetracement(_DELTA, _CFF5, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement6 =
    findRetracement(_DELTA, _CFF6, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";



switch (show_retracements){
case All:
    HideRet = 0;
    closest = 0;
Case None:
    HideRet = 1;
    closest = 0;
Case Closest:
    HideRet = 0;
    closest = 1;
};



#apply hiding/showing parameters to retracement plots
FibFan.SetStyle(Curve.SHORT_DASH);
FibFan.AssignValueColor(Color.RED);
FibFan.HideBubble();

Retracement0.assignvaluecolor(GetColor(shade));
Retracement0.sethiding(HideRet);

Retracement1.assignvaluecolor(GetColor(shade));
Retracement1.sethiding(HideRet);

Retracement2.assignvaluecolor(GetColor(shade));
Retracement2.sethiding(HideRet);

Retracement3.assignvaluecolor(GetColor(shade));
Retracement3.sethiding(HideRet);

Retracement4.assignvaluecolor(GetColor(shade));
Retracement4.sethiding(HideRet);

Retracement5.assignvaluecolor(GetColor(shade));
Retracement5.sethiding(HideRet);

Retracement6.assignvaluecolor(GetColor(shade));
Retracement6.sethiding(HideRet);

#===================================================
#           CHART BUBBLES & LABELS
#===================================================

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement0,
    (_CFF0 * 100)+  "%" + "  " + retracement0,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement1,
    (_CFF1 * 100)+  "%" + "  " + retracement1,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement2,
    (_CFF2 * 100)+  "%" + "  " + retracement2,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement3,
    (_CFF3 * 100)+  "%" + "  " + retracement3,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement4,
    (_CFF4 * 100)+  "%" + "  " + retracement4,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement5,
    (_CFF5 * 100)+  "%" + "  " + retracement5,
    GetColor(shade),
    placement);

AddChartBubble(
    if position > 0 then position else _nan ,
    Retracement6,
    (_CFF6 * 100)+  "%" + "  " + retracement6,
    GetColor(shade),
    placement);

#labels
addlabel(placement < 0, "Retracements:  " +
    (_CFF6 * 100) + "%" + " = " + retracement6 + "\n   " +
    (_CFF5 * 100) + "%" + " = " + retracement5 + "\n   " +
    (_CFF4 * 100) + "%" + " = " + retracement4 + "\n   " +
    (_CFF3 * 100) + "%" + " = " + retracement3 + "\n   " +
    (_CFF2 * 100) + "%" + " = " + retracement2 + "\n   " +
    (_CFF1 * 100) + "%" + " = " + retracement1 + "\n   " +
    (_CFF0 * 100) + "%" + " = " + retracement0
    , color.yellow);
    
#individual labels instead of 1 long one
#addlabel(placement < 0, (_CFF6 * 100) + "%" + " = " + retracement6 , color.yellow);
#addlabel(placement < 0, (_CFF5 * 100) + "%" + " = " + retracement5 , color.yellow);
#addlabel(placement < 0, (_CFF4 * 100) + "%" + " = " + retracement4 , color.yellow);
#addlabel(placement < 0, (_CFF3 * 100) + "%" + " = " + retracement3 , color.yellow);
#addlabel(placement < 0, (_CFF2 * 100) + "%" + " = " + retracement2 , color.yellow);
#addlabel(placement < 0, (_CFF1 * 100) + "%" + " = " + retracement1 , color.yellow);
#addlabel(placement < 0, (_CFF0 * 100) + "%" + " = " + retracement0 , color.yellow);

#===================================================
#                    ALERTS
#===================================================

#===================================================
#                    HINTS
#===================================================

#hint show_retracements: presently only all or none is functioning.  If 'closest' is selected it will show all.

#hint show_bubbles: determines if and where the labels for retracements are placed.  Left = left side of retracement, Right = right side of retracement plot, Label = labels at the top of the chart, None = no labels of any kind.  Currently, Labels has not been enabled.
 
Thank you!!
lol, you're welcome. I noticed an issue with the Right justification of bubbles after posting this. If your right bubbles don't show up then you have extra bars shown on your chart and you'll need to set "Extend_Right" to yes. Otherwise this calculates the retracement at the extensions as NaN and won't display the bubbles.
 
Hi @MerryDay, please see below for the auto fib that i currently use. Is there a way to create a custom scan to find stocks that are within 0-3% of the levels (-27%, and 1.27%). I know this will have to be two different scans, just looking for help with creating the script.

#Auto_FibPrevDay - plots high/low from previous day and fib levels based thereon

input ShowTodayOnly = { default "No", "Yes"};
input showbubbles = yes;
input bubblemover = 4; #Hint n: Number of Bars to shift bubble to the right
input period = AggregationPeriod.DAY;
input displace = 1;

plot ORH = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else high(period = period)[displace];
plot ORL = if showtodayonly and !IsNaN(close(period = period)[-1]) then double.nan else low(period = period)[displace];

ORH.SetDefaultColor(Color.RED);
ORH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.GREEN);
ORL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ORL.SetLineWeight(2);

input F1 = -1.00;
input F2 = -0.62;
input F3 = -0.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.62;
input F8 = 0.705;
input F9 = 0.79;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rHi = ORH;
def rLo = ORL;

def range = rHi - rLo;
plot FF1 = rLo + (range * F1);
plot FF2 = rLo + (range * F2);
plot FF3 = rLo + (range * F3);
plot FF4 = rLo + (range * F4);
plot FF5 = rLo + (range * F5);
plot FF6 = rLo + (range * F6);
plot FF7 = rLo + (range * F7);
plot FF8 = rLo + (range * F8);
plot FF9 = rLo + (range * F9);
plot FF10 = rLo + (range * F10);
plot FF11 = rLo + (range * F11);
plot FF12 = rLo + (range * F12);

def h = high(period = AggregationPeriod.DAY);
def l = low(period = AggregationPeriod.DAY);



FF1.SetDefaultColor(Color.GREEN);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

FF2.SetDefaultColor(Color.GREEN);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.GREEN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.RED);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.RED);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.YELLOW);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.BLUE);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.BLUE);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF9.SetDefaultColor(Color.BLUE);
FF9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF9.SetLineWeight(1);

FF10.SetDefaultColor(Color.GREEN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.GREEN);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.GREEN);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

def b = bubblemover;
def b1 = b + 1;
@MerryDay are you able to send a video or screen shots of what you mean. I am having trouble.
 
Baller4Life - is this what you're looking for? I took the script I had from a previous post and trimmed it down to only 2 fib levels (that you input) and allows you to control the number of bars you want considered when calculating your high/low (default to 220 - 1 trading year). You can also enter the % proximity in the scan in case you want to widen it. I tested it on a few and it seems to deliver what you're looking for (if I'm understanding) and it works as a single call for either of the fibs so you won't need to set up two separate scans. Hope I understood your question....

import it and give it a try. I named it _test_scan_fibs for reference in your scan editor.

link: http://tos.mx/CZI5R9X

vgm9UXHxDI915R?width=1050&height=389&cropmode=none.png




Code:
# http://trendtradez.com

declare upper;
#===================================================
#                INPUTS & GLOBAL VARIABLES
#===================================================

input _H           = high;
input _L           = low;
input Coeffient_0  = -0.27;
input Coeffient_1  = 1.27;

input Length       = 220;
input Proximity    = .03;
input price        = close;

def _nan;           # reduce calls to double.nan function
def _HH;            # Highest High of price
def _LL;            # Lowest Low of price
def _BN;            # current bar's barnumber
def _HB;            # barnumber of highest high
def _LB;            # barnumber of lowest low
def _LNA;           #
def _HNA;           #
def _LN;            #
def _HN;            #
def _x;             #
#####################

_nan = Double.NaN;
_HH  = Highest(high, length);
_LL  = Lowest(low, length);
_BN  = BarNumber();
_HB  = if _H == _HH then _BN else _nan;
_LB  = if _L == _LL then _BN else _nan;
_HN  = CompoundValue(1, if IsNaN(_HB) then _HN[1] else _HB, _HB);
_HNA = HighestAll(_HN);
_LN  = CompoundValue(1, if IsNaN(_LB) then _LN[1] else _LB, _LB);
_LNA = LowestAll(_LN);
_x   = AbsValue(_LNA - _HNA );

#shortened variables for retracements script call
def _CFF0 = Coeffient_0;
def _CFF1 = Coeffient_1;

def _UPDN =
    if _HNA > _LNA then 1 else
    if _HNA < _LNA then -1 else _nan;

#===================================================
#              Retracements Script
#===================================================
script findRetracement {
    input _CFF  = 0.000;
    input _LL   = 0;
    input _HH   = 0;
    input _HNA  = 0;
    input _LNA  = 0;
    input _BN   = 0;

    def _nan    = Double.NaN;
    def _day    = GetDay() == GetLastDay();
    def _month  = GetMonth() == GetLastMonth();
    def _year   = GetYear() == GetLastYear();
    def _isT    = If(_day and _month and _year, 1, 0);
    def _isTBN  = HighestAll(if _isT then _BN else Double.NaN);
    def _range  = _HH - _LL;

    plot result =
    if  _BN >= _HNA
        and _BN <= _isTBN
        then HighestAll(_LL + (_range * _CFF)) 
    else if _BN <= _isTBN
        then HighestAll(_LL + (_range * _CFF))   
    else _nan;
}
;

#===================================================
#                PRIMARY PLOTS
#===================================================

def Retracement0 =
findRetracement(_CFF0, _LL, _HH, _HNA, _LNA, _BN)."result";
def Retracement1 =
findRetracement(_CFF1, _LL, _HH, _HNA, _LNA, _BN)."result";

plot alert =
    if price > Retracement0 * (1 - Proximity)
    OR price < Retracement0 * (1 + Proximity)
    OR price > Retracement1 * (1 - Proximity)
    OR price < Retracement1 * (1 + Proximity)
    then 1 else 0;
 
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
Would there be a way to only plot these in the right extention area to the right of the most recent candle?
 
Would there be a way to only plot these in the right extention area to the right of the most recent candle?
See the changes in bold to limit plot to expansion
Rich (BB 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;

input onexpansion = yes;
def condition1 = if onexpansion and !IsNaN(close) then Double.NaN else  downward and  barnumber >= highnumberall;
def condition2 = if onexpansion and !IsNaN(close) then Double.NaN else 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
 
See the changes in bold to limit plot to expansion
Rich (BB 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;

input onexpansion = yes;
def condition1 = if onexpansion and !IsNaN(close) then Double.NaN else  downward and  barnumber >= highnumberall;
def condition2 = if onexpansion and !IsNaN(close) then Double.NaN else 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
This is amazing, thank you so much!
 
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
anyway to get it to just show the .50 line and 0.618 lines with the fib high/low line(trendline), and chart bubbles that does not extend left accross the whole chart,,,, any assiatance will be greatly appreciated
 
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;
I would to know how to add three Fibonacci extension levels in both sides to this study once it breaks out the top price or bottom price.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
306 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top