Automatic Fibonacci Retracements
One of the most popular drawing tools in TOS is the Fibonacci Retracement. A user clicks on the highest and lowest point on the chart, and the system draws a horizontal line at those top and bottom points, as well as lines for 23.6%, 38.2%, 50%, 61.8%, and 78.6% of the high/low range. These lines can provide key support/resistance levels. The major downsides of this are that you must draw this manually on every symbol you want, and if the high or low of the chart, changes, you have to go back and update the high and low values of the drawing. Because of this, clients often ask if there is a study that will automatically draw the fibonacci for them on every chart.
We insert the following script into a blank study:
input coefficient_0 = 0.00;
input coefficient_1 = .236;
input Coefficient_2 = .382;
input Coefficient_3 = .500;
input Coefficient_4 = .618;
input Coefficient_5 = .786;
input Coefficient_6 = 1.00;
def hh = HighestAll(high);
def ll = LowestAll(low);
def barnumber = barnumber();
def bar_high = if high == hh then BarNumber else Double.NaN;
def bar_low = if low == ll then BarNumber else Double.NaN;
def highest_bar_count = HighestAll(bar_high);
def lowest_bar_count = LowestAll(bar_low);
def uptrend = highest_bar_count > lowest_bar_count;
def downtrend = lowest_bar_count > highest_bar_count;
def time_distance = AbsValue(highest_bar_count - lowest_bar_count);
def range = hh - ll;
def slope = (hh - ll)/time_distance;
def line = if uptrend then ll + (slope*(barnumber - lowest_bar_count)) else hh - (slope*(barnumber - highest_bar_count));
def current_line_uptrend = if barnumber < highest_bar_count and barnumber > lowest_bar_count then line else double.nan;
def current_line_downtrend = if barnumber < lowest_bar_count and barnumber > highest_bar_count then line else double.nan;
plot fib_diagonal = if uptrend then current_line_uptrend else current_line_downtrend;
plot retracement0 = if uptrend then hh - (range*coefficient_0) else ll + (range*coefficient_0);
plot retracement1 = if uptrend then hh - (range*coefficient_1) else ll + (range*coefficient_1);
plot retracement2 = if uptrend then hh - (range*coefficient_2) else ll + (range*coefficient_2);
plot retracement3 = if uptrend then hh - (range*coefficient_3) else ll + (range*coefficient_3);
plot retracement4 = if uptrend then hh - (range*coefficient_4) else ll + (range*coefficient_4);
plot retracement5 = if uptrend then hh - (range*coefficient_5) else ll + (range*coefficient_5);
plot retracement6 = if uptrend then hh - (range*coefficient_6) else ll + (range*coefficient_6);
DefineGlobalColor("Fibonacci Lines - Uptrend", Color.light_green);
DefineGlobalColor("Fibonacci Lines - Downtrend", color.light_red);
DefineGlobalColor("Labels - Uptrend", Color.light_green);
DefineGlobalColor("Labels - Downtrend", Color.light_red);
input bubble_right_orientation = yes;
input bubble_adjustment = 0;
#def most_recent_bar = If(IsNaN(close[-1]) and !IsNaN(close), BarNumber(), most_recent_bar[1]);
def most_recent_bar = if barnumber == highestall(barnumber) then barnumber() else double.nan;
def first_bar = if barnumber == lowestall(barnumber) then barnumber() else double.nan;
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment , "price location" = retracement0, text = " " + aspercent(coefficient_0) + " $" + round(retracement0) + " ", color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement1, text = aspercent(coefficient_1) + " $" + round(retracement1), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement2, text = aspercent(coefficient_2) + " $" + round(retracement2), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement3, text = aspercent(coefficient_3) + " $" + round(retracement3), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement4, text = aspercent(coefficient_4) + " $" + round(retracement4), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement5, text = aspercent(coefficient_5) + " $" + round(retracement5), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
AddChartBubble("time condition" = if bubble_right_orientation then barnumber == highestall(most_recent_bar) + bubble_adjustment else barnumber() == lowestall(first_bar) + bubble_adjustment, "price location" = retracement6, text = aspercent(coefficient_6) + " $" + round(retracement6), color = if uptrend then GlobalColor("Labels - Uptrend") else GlobalColor("Labels - Downtrend"));
fib_diagonal.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
fib_diagonal.setstyle(curve.long_DASH);
fib_diagonal.hidebubble();
fib_diagonal.hidetitle();
retracement0.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement1.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement2.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement3.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement4.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement5.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement6.assignvaluecolor(if uptrend then globalcolor("Fibonacci Lines - Uptrend") else globalcolor("Fibonacci Lines - Downtrend"));
retracement0.hidebubble();
retracement1.hidebubble();
retracement2.hidebubble();
retracement3.hidebubble();
retracement4.hidebubble();
retracement5.hidebubble();
retracement6.hidebubble();
retracement0.hideTitle();
retracement1.hideTitle();
retracement2.hideTitle();
retracement3.hideTitle();
retracement4.hideTitle();
retracement5.hideTitle();
retracement6.hideTitle();
Now we have a study that will automatically draw horizontal lines at the highest and lowest points, with a diagonal connecting the two. Also there are lines at each of the retracement levels in between, all at the same values at the manual version.
One major improvement on this version is the fact that it will automatically switch colors base on if the stock is in an uptrend or downtrend and switch its top to bottom or bottom to top orientation. This is something that the user would have to have done manually previously, but is done automatically in this study.