Auto Fib (Fibonacci) Levels Indicator for ThinkorSwim

Hi SleepyZ, can you please show how to color hilo line. I tried as below but it did not assign color:
hilo.assignvaluecolor(if hilo>hilo[1] then color.magenta else color.cyan);

Other than the last day's developing hilo between the last day and the current day, the following code should help with the coloring.

Screenshot-2022-12-09-080253.png

Ruby:
input display_rthrs_only = yes;

input ShowTodayOnly      = { default "No", "Yes"};
def s                    = ShowTodayOnly;
input period             = {default "Day", "Week", "Month"};
def closeByPeriod        = close(period = period)[-1];
def rthrs = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) > 0;


input Fpos236 = .236;
input Fpos382 = .382;
input Fpos50  = .5;
input Fpos618 = .618;
input Fpos786 = .786;

def rhi =  if display_rthrs_only then if !rthrs then Double.NaN else high(period = period) else high(period = period);
def rlo =  if display_rthrs_only then if !rthrs then Double.NaN else low(period = period) else low(period = period);

def range  = rhi - rlo;
def F236   = rhi - (range * Fpos236);
def F382   = rhi - (range * Fpos382);
def F50    = rhi - (range * Fpos50);
def F618   = rhi - (range * Fpos618);
def F786   = rhi - (range * Fpos786);

plot ORH;
plot ORL;
plot Fib50;
plot Fib236;
plot Fib382;
plot Fib618;
plot Fib786;
if ShowTodayOnly and !IsNaN(closeByPeriod)
then {
    ORH      = Double.NaN;
    ORL      = Double.NaN;
    Fib50    = Double.NaN;
    Fib236   = Double.NaN;
    Fib382   = Double.NaN;
    Fib618   = Double.NaN;
    Fib786   = Double.NaN;

} else {
    ORH      = rhi;
    ORL      = rlo;
    Fib50    = F50;
    Fib236   = F236;
    Fib382   = F382;
    Fib618   = F618;
    Fib786   = F786;

}
;


ORH.SetDefaultColor(Color.GREEN);
ORH.SetStyle(Curve.LONG_DASH);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.RED);
ORL.SetStyle(Curve.LONG_DASH);
ORL.SetLineWeight(2);

Fib50.SetDefaultColor(Color.WHITE);
Fib50.SetStyle(Curve.LONG_DASH);
Fib50.SetLineWeight(1);

Fib236.SetDefaultColor(Color.CYAN);
Fib236.SetStyle(Curve.LONG_DASH);
Fib236.SetLineWeight(1);

Fib382.SetDefaultColor(Color.MAGENTA);
Fib382.SetStyle(Curve.LONG_DASH);
Fib382.SetLineWeight(1);

Fib618.SetDefaultColor(Color.YELLOW);
Fib618.SetStyle(Curve.LONG_DASH);
Fib618.SetLineWeight(1);

Fib786.SetDefaultColor(Color.DARK_RED);
Fib786.SetStyle(Curve.LONG_DASH);
Fib786.SetLineWeight(2);

input showbubbles = yes;

input bubblemover = 5;
def m = bubblemover;
def m1 = m + 1;
AddChartBubble(showbubbles and IsNaN(ORH[m]) and !IsNaN(ORH[m1]), ORH[m1], "H", ORH.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(ORL[m]) and !IsNaN(ORL[m1]), ORL[m1], "L", ORL.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(Fib50[m]) and !IsNaN(Fib50[m1]), Fib50[m1], "50", Fib50.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(Fib236[m]) and !IsNaN(Fib236[m1]), Fib236[m1], "236", Fib236.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(Fib382[m]) and !IsNaN(Fib382[m1]), Fib382[m1], "382", Fib382.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(Fib618[m]) and !IsNaN(Fib618[m1]), Fib618[m1], "618", Fib618.TakeValueColor());
AddChartBubble(showbubbles and IsNaN(Fib786[m]) and !IsNaN(Fib786[m1]), Fib786[m1], "786", Fib786.TakeValueColor());


plot lo = if low == rlo then 1 else 0;
plot hi = if high == rhi then 1 else 0;

plot hilo =  if hi then rhi else if lo then low else Double.NaN;
hilo.EnableApproximation();
hilo.setlineweight(5);
def hl0 = if !isnan(hilo) then hilo else hl0[1];
def hl1 = if hl0!=hl0[1] then hl0[1] else hl1[1];
addlabel(1,hl0+" "+hl1);
hilo.assignvaluecolor(if (hl0-hl1)>0 then color.magenta else color.cyan);
 

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

This script is awesome, been using for a while and love it. Thanks!

Quick question wondering if its possible to use an IF-THAN statement for this? For example:

  1. Plot fibs on BACKSIDE from HOD to low AFTER high is made
    • (Open 20.00 High 22.00, then drops back down and makes a higher low at 21.00, fib would be from 22 to 21 with the 50% fib at 21.50)
  2. 2. Plot if HOD Range is > 20%

Thanks!
 
Is it possible to add the code necessary for additional Fibonacci retracements between each key ratio on the big retracement?

For instance, the smaller Fib retracements between the 100% area to the 61.8% area, another retracement from 61.8% to 50%, etc.

Ideally, if any lines are overlapping from the multiple retracements then those lines should be indicated by a different plot color so it's easier to identify them since they'll likely serve as key support/resistance areas.
 
Last edited by a moderator:
Hello :) Is there a way to modify the script to set up the Auto Fib from 1:30PM to 10:30PM?
[Edited to provide a basic trendline option and expansion of the def bars to provide more plots.]

1.This allows an innput of the timeframe desired (1330 to 2230 in your case). The start can be greater than the end time input, for example, to do the entire aftermarket for stocks input from 1600 to 0929,
2.The current developing fibs will be displayed. To do this, the volumeprofiles profilehigh/profilelow was used set at the bar timeframe. This has a limitation on the number of bars it will then display on your chart. The lower the chart aggregation the fewer the fibs will plot.
3. The fibs will flip from 0 and 100 to 100 and 0 depending on whether the bar of the high is before the bar of the low.
4. Various options are available to extend the lines (default) yes, showtodayonly (default no).

Screenshot-2023-01-18-113454.png

Ruby:
#Auto_Fibs_TimeRange_Flip_0_100

input start = 0400;
input end   = 0929;
input showtodayonly  = no;
input bubblemover    = 0;
input showbubbles    = yes;
input extendlines    = yes;
input show_trendline = yes;

def sec1    = SecondsFromTime(start);
def sec2    = SecondsFromTime(end);
def isTime1 = (sec1 >= 0 and sec1[1] <= 0) or
              (sec1 < sec1[1] and sec1 > 0);
def isTime2 = (sec2 > 0 and sec2[1] <= 0) or
              (sec2 < sec2[1] and sec2 > 0) or
              (if TickValue() <= .01 and (sec2 <= 0)
              then GetYYYYMMDD() != GetYYYYMMDD()[-1]
              else 0);
def aftermarket = CompoundValue(1,
                  if isTime1[1] == 0 and isTime1 == 1
                  then 1
                  else if isTime2
                  then 0
                  else aftermarket[1], 0);

def bars   = 100000;
def bn     = BarNumber();
def na     = Double.NaN;

def period = bn - 1;
def count  = CompoundValue(1,
             if aftermarket and period != period[1]
             then (count[1] + period - period[1]) % bars
             else count[1], 0);
def cond   = count < count[1] + period - period[1];

profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = bars, "pricePerRow" = PricePerRow.TICKSIZE , "value area percent" = 0);

def hProfile = if aftermarket and IsNaN(vol.GetHighest())
               then hProfile[1]
               else if period != period[1]
               then vol.GetHighest()
               else hProfile[1];
def lProfile = if aftermarket and IsNaN(vol.GetLowest())
               then lProfile[1]
               else if period != period[1]
               then vol.GetLowest()
               else lProfile[1];

def ProfileHigh = if extendlines and !aftermarket then ProfileHigh[1] else if aftermarket then hProfile else na;
def ProfileLow  = if extendlines and !aftermarket then ProfileLow[1] else if aftermarket then lProfile else na;

def hrange = ProfileHigh;
def lrange = ProfileLow;


input showverticalline = yes;
AddVerticalLine(showverticalline and hrange != hrange[1], "", Color.BLUE, stroke = Curve.FIRM);


input showtimerange_label = yes;
AddLabel(showtimerange_label, "HL Time Range: " + AsPrice(start) + " to " + AsPrice(end), Color.YELLOW);

input Fpos236 = .236;
input Fpos382 = .382;
input Fpos50  = .5;
input Fpos618 = .618;
input Fpos764 = .764;
input Fpos1618 = 1.618;
input Fneg618  = -0.618;

def range  = AbsValue(hrange - lrange);
def F236   = hrange - (range * Fpos236);
def F382   = hrange - (range * Fpos382);
def F50    = hrange - (range * Fpos50);
def F618   = hrange - (range * Fpos618);
def F764   = hrange - (range * Fpos764);
def F1618   = hrange - (range * Fpos1618);
def F_618   = hrange - (range * Fneg618);

plot ORH;
plot ORL;
plot Fib50;
plot Fib236;
plot Fib382;
plot Fib618;
plot Fib764;
plot Fib1618;
plot Fib_618;

def dataCount = CompoundValue(1, if (aftermarket != aftermarket[1]) then dataCount[1] + 1 else dataCount[1], 0);

if showtodayonly and HighestAll(dataCount) - dataCount > 2
then {
    ORH      = Double.NaN;
    ORL      = Double.NaN;
    Fib50    = Double.NaN;
    Fib236   = Double.NaN;
    Fib382   = Double.NaN;
    Fib618   = Double.NaN;
    Fib764   = Double.NaN;
    Fib1618   = Double.NaN;
    Fib_618   = Double.NaN;

} else {
    ORH      = hrange;
    ORL      = lrange;
    Fib50    = F50;
    Fib236   = F236;
    Fib382   = F382;
    Fib618   = F618;
    Fib764   = F764;
    Fib1618  = F1618;
    Fib_618  = F_618;

}

ORH.SetDefaultColor(Color.WHITE);
ORH.SetPaintingStrategy(PaintingStrategy.DASHES);
ORH.SetLineWeight(2);

ORL.SetDefaultColor(Color.WHITE);
ORL.SetPaintingStrategy(PaintingStrategy.DASHES);
ORL.SetLineWeight(2);

Fib50.SetDefaultColor(Color.WHITE);
Fib50.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib50.SetLineWeight(1);

Fib236.SetDefaultColor(Color.CYAN);
Fib236.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib236.SetLineWeight(1);

Fib382.SetDefaultColor(Color.YELLOW);
Fib382.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib382.SetLineWeight(1);

Fib618.SetDefaultColor(Color.YELLOW);
Fib618.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib618.SetLineWeight(1);

Fib764.SetDefaultColor(Color.CYAN);
Fib764.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib764.SetLineWeight(2);

Fib1618.SetDefaultColor(Color.YELLOW);
Fib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib1618.SetLineWeight(1);

Fib_618.SetDefaultColor(Color.YELLOW);
Fib_618.SetPaintingStrategy(PaintingStrategy.DASHES);
Fib_618.SetLineWeight(1);

def mo   = bubblemover;
def mo1  = mo + 1;

def lo1 = if low  == lProfile then 1 else 0;
def hi1 = if high == hProfile then 1 else 0;
def hbar = if aftermarket and hi1 == 1 then bn else hbar[1];
def lbar = if aftermarket and lo1 == 1 then bn else lbar[1];
def last = IsNaN(close[mo + 1]) and !IsNaN(close[mo + 1]);
plot xhbar = hbar;
plot xlbar = lbar;
xhbar.SetHiding(yes);
xlbar.SetHiding(yes);

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
ORH[mo + 1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 0 else 100),
ORH.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
ORL[mo + 1], (if xhbar[mo + 1] > xlbar[mo + 1] then 100 else 0),
ORL.TakeValueColor(), no);

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib50[mo1 + 1],
"50",
Fib50.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib236[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 236 else 764),
Fib236.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib382[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 382 else 618),
Fib382.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib618[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 618 else 382),
Fib618.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib764[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 764 else 236),
Fib764.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib1618[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then 1618 else -618),
Fib1618.TakeValueColor());

AddChartBubble(
if !showbubbles
then na
else if extendlines
then ORH[mo + 1] != ORH[mo] or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1])
else IsNaN(ORH[mo]) and !IsNaN(ORH[mo + 1]) or
     IsNaN(close[mo]) and !IsNaN(close[mo + 1]),
Fib_618[mo1],
(if xhbar[mo + 1] > xlbar[mo + 1] then -618 else 1618),
Fib_618.TakeValueColor());


def hbar1 = if (aftermarket or aftermarket[1]) and hi1 == 1 then 1 else 0;
def lbar1 = if (aftermarket or aftermarket[1]) and lo1 == 1 then 1 else 0;

plot xhbar1 = hbar1;
plot xlbar1 = lbar1;
xhbar1.SetHiding(yes);
xlbar1.SetHiding(yes);

plot xline = if show_trendline then if aftermarket and !isnan(xhbar1) then hrange else if aftermarket and !isnan(xlbar1) then hrange else na else na;
xline.enableApproximation();
;
 
Last edited:
1.This allows an innput of the timeframe desired (1330 to 2230 in your case). The start can be greater than the end time input, for example, to do the entire aftermarket for stocks input from 1600 to 0929,
2.The current developing fibs will be displayed. To do this, the volumeprofiles profilehigh/profilelow was used set at the bar timeframe. This has a limitation on the number of bars it will then display on your chart. The lower the chart aggregation the fewer the fibs will plot.
3. The fibs will flip from 0 and 100 to 100 and 0 depending on whether the bar of the high is before the bar of the low.
4. Various options are available to extend the lines (default) yes, showtodayonly (default no).
Hi SleepyZ, can you please help to draw a line between the high and low of time range of 1330 to 2230?
for the intraday chart, I would like to draw a line between the high and low of the day, can you please assist ?
 
Last edited:
Hi SleepyZ, can you please help to draw a line between the high and low of time range of 1330 to 2230?
for the intraday chart, I would like to draw a line between the high and low of the day, can you please assist ?
The above script was edited to provide a basic trendline. It may or may not be useful. The default is set to Yes. Select No to hide it.
 
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
Hi @tomsk, @BenTen

I tried to modify your script to ( display Fib% limited to TODAY (Intraday chart) only ) with the following mod:


def a = Highest(high(period = AggregationPeriod.DAY), length=1);
def b = Lowest(low(period = AggregationPeriod.DAY), length=1);


my Result is comical lol
  • Fib lines are plotted across a few days
  • High of Day is previous of day?
Help?



Thanks!
 
Hi @tomsk, @BenTen

I tried to modify your script to ( display Fib% limited to TODAY (Intraday chart) only ) with the following mod:


def a = Highest(high(period = AggregationPeriod.DAY), length=1);
def b = Lowest(low(period = AggregationPeriod.DAY), length=1);


my Result is comical lol
  • Fib lines are plotted across a few days
  • High of Day is previous of day?
Help?



Thanks!

Since you used the TOS DailyHilghLow indicator, this uses more of the it than you used in your def a/b

Screenshot-2023-02-18-170244.jpg
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 aggregationPeriod = AggregationPeriod.DAY;
def displace = 0;
def length   =  1;
def showOnlyLastPeriod = yes;

def DailyHigh;
def DailyLow;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    DailyHigh = Double.NaN;
    DailyLow = Double.NaN;
} else {
    DailyHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    DailyLow  = Lowest(low(period = aggregationPeriod)[-displace], length);
}

def a = dailyhigh;
def b = dailylow;

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
 
I notice rec were used for highnumber / lownumber. Thinkscript noted rec is being replaced by def. Any particular reason in your codes?

For some of us, our script libraries go way back to the days when 'ref' was commonly used. It would be impractical for us to rewrite the thousands of scripts in our library. In fact, there is no compelling reason to do so, as 'ref' still does the job effectively."
 
For some of us, our script libraries go way back to the days when 'ref' was commonly used. It would be impractical for us to rewrite the thousands of scripts in our library. In fact, there is no compelling reason to do so, as 'ref' still does the job effectively."

Appreciate the clarification with good humor! lol
 
Other than the last day's developing hilo between the last day and the current day, the following code should help with the coloring.
Hi SleepyZ, can you kindly show how to scan for hi and lo points in the above intraday auto fib level? I always appreciate for your expertise.
.
.
def lo = if low == rlo then 1 else 0;
def hi = if high == rhi then 1 else 0;
plot scan= lo or hi;
I used the above scan and it did not produce anything. should the period be changed?
 
Last edited:
Hi SleepyZ, can you kindly show how to scan for hi and lo points in the above intraday auto fib level? I always appreciate for your expertise.
.
.
def lo = if low == rlo then 1 else 0;
def hi = if high == rhi then 1 else 0;
plot scan= lo or hi;
I used the above scan and it did not produce anything. should the period be changed?

Sorry, but this code is too complex for the scanner as written.
 
I searched this thread for a modified version of the original post study that displays the fibs only in the expansion area and with a price bubble to the right but i couldn´t find anything. Can anyone help make such a modified version?
 
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
Does anyone have a code for multi time frame fib levels? Meaning...the code will automatically plot fib levels for any time frame such as one year, or 2 years, etc on to smaller time frame.
Thank u !
 
Last edited:
I searched this thread for a modified version of the original post study that displays the fibs only in the expansion area and with a price bubble to the right but i couldn´t find anything. Can anyone help make such a modified version?

This should help

Screenshot-2023-03-01-045355.png
Code:
# Auto Fib V1.3 see modification on 20230301
# tomsk
# 11.19.2019

# 20230301 Modified to show_on_expansion and bubble placement options


# 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 show_on_expansion  = yes;
input show_bubbles_left  = no;
input show_bubbles_right = yes;
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 = if show_on_expansion and !IsNaN(close) then Double.NaN else downward and barnumber >= highnumberall;
def condition2 = if show_on_expansion 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();

Retracement1.AssignValueColor(CreateColor(173, 216, 230));
Retracement1.SetLineWeight(2);
Retracement1.HideBubble();

Retracement2.AssignValueColor(CreateColor(0, 197, 49));
Retracement2.SetLineWeight(2);
Retracement2.HideBubble();

Retracement3.AssignValueColor(CreateColor(255, 64, 64));
Retracement3.SetLineWeight(3);
Retracement3.HideBubble();

Retracement4.AssignValueColor(CreateColor(255, 215, 0));
Retracement4.SetLineWeight(5);
Retracement4.HideBubble();

Retracement5.AssignValueColor(CreateColor(0, 255, 255));
Retracement5.SetLineWeight(2);
Retracement5.HideBubble();

Retracement6.AssignValueColor(CreateColor(255, 255, 255));
Retracement6.SetLineWeight(4);
Retracement6.HideBubble();

#Bubbles on Left Side
AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement0, Concat( (coefficient_0 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement0, Concat( (coefficient_0 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement1, Concat( (coefficient_1 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement1, Concat( (coefficient_1 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement2, Concat( (Coefficient_2 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement2, Concat( (Coefficient_2 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement3, Concat( (Coefficient_3 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement3, Concat( (Coefficient_3 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement4, Concat( (Coefficient_4 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement4, Concat( (Coefficient_4 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement5, Concat( (Coefficient_5 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement5, Concat( (Coefficient_5 * 100), "%"), Color.RED, yes);

AddChartBubble(show_bubbles_left and (downward and barnumber == highnumberall), Retracement6, Concat( (Coefficient_6 * 100), "%"), Color.RED, yes);
AddChartBubble(show_bubbles_left and (upward and barnumber == lownumberall), Retracement6, Concat( (Coefficient_6 * 100), "%"), Color.RED, yes);

#Bubbles on Right Side
AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement0, Concat( (coefficient_0 * 100), "%\n") + Concat( "$", Round(Retracement0, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement0, Concat( (coefficient_0 * 100), "%\n") + Concat( "$", Round(Retracement0, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement1, Concat( (coefficient_1 * 100), "%\n") + Concat( "$", Round(Retracement1, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement1, Concat( (coefficient_1 * 100), "%\n") + Concat( "$", Round(Retracement1, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement2, Concat( (Coefficient_2 * 100), "%\n") + Concat( "$", Round(Retracement2, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement2, Concat( (Coefficient_2 * 100), "%\n") + Concat( "$", Round(Retracement2, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement3, Concat( (Coefficient_3 * 100), "%\n") + Concat( "$", Round(Retracement3, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement3, Concat( (Coefficient_3 * 100), "%\n") + Concat( "$", Round(Retracement3, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement4, Concat( (Coefficient_4 * 100), "%\n") + Concat( "$", Round(Retracement4, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement4, Concat( (Coefficient_4 * 100), "%\n") + Concat( "$", Round(Retracement4, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement5, Concat( (Coefficient_5 * 100), "%\n") + Concat( "$", Round(Retracement5, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement5, Concat( (Coefficient_5 * 100), "%\n") + Concat( "$", Round(Retracement5, 2)), Color.YELLOW, yes);

AddChartBubble(show_bubbles_right and (downward and BarNumber() == HighestAll(BarNumber())), Retracement6, Concat( (Coefficient_6 * 100), "%\n") + Concat( "$", Round(Retracement6, 2)), Color.YELLOW, yes);
AddChartBubble(show_bubbles_right and (upward and BarNumber() == HighestAll(BarNumber())), Retracement6, Concat( (Coefficient_6 * 100), "%\n") + Concat( "$", Round(Retracement6, 2)), Color.YELLOW, 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
 
# Automatically draws fibonacci retracements using the highest price and lowest price
# from the current view and timeframe.

Is there a version that instead works off the aggregation period DAY/WEEK/MONTH/YEAR type thing? Having to change the chart settings for it to work is cumbersome.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
501 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