# Auto Fib V1.3
# tomsk
# 11.19.2019
#German_Burrito
# 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
#
# V1.4 - 1.22.2021 - AspaTrader. - Modified to show fib levels for HA candles
#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 period = 50;
input candleSmoothing = {default Valcu, Vervoort};
input slope_adjust_multiplier = 1.00;
input movingAverageType = {default TEMA, Exponential, Hull };
def slope_adj = slope_adjust_multiplier;
def openMA;
def closeMA;
def highMA;
def lowMA;
switch (movingAverageType) {
case Exponential:
openMA = CompoundValue(1, ExpAverage(open, period), open);
closeMA = CompoundValue(1, ExpAverage(close, period), close);
highMA = CompoundValue(1, ExpAverage(high, period), high);
lowMA = CompoundValue(1, ExpAverage(low, period), low);
case Hull:
openMA = CompoundValue(1, HullMovingAvg(open, period), open);
closeMA = CompoundValue(1, HullMovingAvg(close, period), close);
highMA = CompoundValue(1, HullMovingAvg(high, period), high);
lowMA = CompoundValue(1, HullMovingAvg(low, period), low);
case TEMA:
openMA = CompoundValue(1, TEMA(open, period), open);
closeMA = CompoundValue(1, TEMA(close, period), close);
highMA = CompoundValue(1, TEMA(high, period), high);
lowMA = CompoundValue(1, TEMA(low, period), low);
}
def haOpen;
def haClose;
switch (candleSmoothing){
case Valcu:
haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0), open);
haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);
case Vervoort:
haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);
}
plot o = haOpen;
o.Hide();
def haLow = Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);
def price = haClose;
def high = haHigh;
def low = haLow;
input coefficient_0 = 0.000;
input Coefficient_1 = .236;
input Coefficient_2 = .382;
input Coefficient_3 = .5;
input Coefficient_4 = .786;
input Coefficient_5 = .618;
input Coefficient_6 = 1.00;
input LabelColor = {default "MAGENTA", "CYAN", "PINK", "LIGHT_GRAY", "ORANGE", "RED", "GREEN", "GRAY", "WHITE"};
input n = 3;
def n1 = n + 1;
def a = HighestAll(haHigh);
def b = LowestAll(haLow);
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;
def xx = if condition1 then (barnumber - highnumberall) else (barnumber - lownumberall);
def mm = if condition1 then HighestAll((ohlc4 - Retracement6) / xx) else LowestAll((ohlc4 - Retracement6) / xx);
#PLOT FIBS with slope
# Y = MX + B
#y = end value, m = slope = x is time b = beg value
plot r7 = mm * slope_adj * xx + Retracement6;
plot r6 = mm * slope_adj * xx + Retracement5;
plot r5 = mm * slope_adj * xx + Retracement4;
plot r4 = mm * slope_adj * xx + Retracement3;
plot r3 = mm * slope_adj * xx + Retracement3;
plot r2 = mm * slope_adj * xx + Retracement2;
plot r1 = mm * slope_adj * xx + Retracement1;
plot r0 = mm * slope_adj * xx + Retracement0;
Retracement0.AssignValueColor(CreateColor(255, 255, 255));
Retracement0.SetLineWeight(4);
Retracement0.HideBubble();
#AddChartBubble((downward and haClose[n1]) and IsNaN(haClose[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);
#AddChartBubble((upward and haClose[n1]) and IsNaN(haClose[n]), retracement0, concat( (coefficient_0 * 100), "%"), GetColor(LabelColor), yes);
Retracement2.AssignValueColor(CreateColor(0, 197, 49));
Retracement2.SetLineWeight(2);
Retracement2.HideBubble();
#AddChartBubble((downward and haClose[n1]) and IsNaN(haClose[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);
#AddChartBubble((upward and haClose[n1]) and IsNaN(haClose[n]), retracement2, concat( (coefficient_2 * 100), "%"), GetColor(LabelColor), yes);
Retracement4.AssignValueColor(CreateColor(255, 215, 0));
Retracement4.SetLineWeight(5);
Retracement4.HideBubble();
#AddChartBubble((downward and haClose[n1]) and IsNaN(haClose[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);
#AddChartBubble((upward and haClose[n1]) and IsNaN(haClose[n]), retracement4, concat( (coefficient_4 * 100), "%"), GetColor(LabelColor), yes);
Retracement6.AssignValueColor(CreateColor(255, 255, 255));
Retracement6.SetLineWeight(4);
Retracement6.HideBubble();
#AddChartBubble((downward and haClose[n1]) and IsNaN(haClose[n]), retracement6, concat( (coefficient_6 * 100), "%"), GetColor(LabelColor), yes);
#AddChartBubble((upward and haClose[n1]) and IsNaN(haClose[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 Retracement2) , "Price crosses below Retracement Line 2");
Alert((price crosses above Retracement2) , "Price crosses above Retracement Line 2");
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 Retracement6) , "Price crosses below Retracement Line 6");
Alert((price crosses above Retracement6) , "Price crosses above Retracement Line 6");
# End Auto Fib v1.3
AddChartBubble((barnumber == istodaybarnumber), Retracement0, Concat( "$", Round(Retracement0, 2)), Color.RED, yes);
AddChartBubble((barnumber == istodaybarnumber), Retracement2, Concat( "$", Round(Retracement2, 2)), Color.RED, yes);
AddChartBubble((barnumber == istodaybarnumber), Retracement4, Concat( "$", Round(Retracement4, 2)), Color.RED, yes);
AddChartBubble((barnumber == istodaybarnumber), Retracement6, Concat( "$", Round(Retracement6, 2)), Color.RED, yes);
r0.setlineWeight(2);
r1.setlineWeight(2);
r2.setlineWeight(2);
r3.setlineWeight(2);
r4.setlineWeight(2);
r5.setlineWeight(2);
r6.setlineWeight(2);
r7.setlineWeight(2);
R0.AssignValueColor(CreateColor(255, 255, 255));
R1.AssignValueColor(CreateColor(255, 255, 25));
R2.AssignValueColor(CreateColor(25, 255, 255));
R3.AssignValueColor(CreateColor(255, 255, 255));
R4.AssignValueColor(CreateColor(5, 215, 25));
R5.AssignValueColor(CreateColor(115, 155, 215));
R7.AssignValueColor(CreateColor(255, 255, 255));
retracement0.Hide();
retracement1.Hide();
retracement2.Hide();
retracement3.Hide();
retracement4.Hide();
retracement5.Hide();
retracement6.Hide();