input slope_adjust_multiplier = 0.00;
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 slope_adj = slope_adjust_multiplier;
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 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 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 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 r6 = mm*slope_adj*xx + retracement6;
plot r5 = mm*slope_adj*xx + retracement5;
plot r4 = mm*slope_adj*xx + retracement4;
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;
#VISUALS AND ALERTS
Retracement0.assignvaluecolor(CreateColor(255,255,255));
Retracement0.setLineWeight(4);
retracement0.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber +10), retracement0, concat( "$", round(retracement0, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement0, concat( (coefficient_0 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement0, concat( (coefficient_0 * 100), "%"), color.red, yes);
Retracement1.assignvaluecolor(CreateColor(173,216,230));
Retracement1.setLineWeight(2);
retracement1.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement1, concat( "$", round(retracement1, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement1, concat( (coefficient_1 * 100), "%"), color.red, yes);
Retracement2.assignvaluecolor(CreateColor(0,197,49));
Retracement2.setLineWeight(2);
retracement2.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement2, concat( "$", round(retracement2, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement2, concat( (coefficient_2 * 100), "%"), color.red, yes);
Retracement3.assignvaluecolor(CreateColor(255,64,64));
Retracement3.setLineWeight(3);
retracement3.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement3, concat( "$", round(retracement3, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement3, concat( (coefficient_3 * 100), "%"), color.red, yes);
Retracement4.assignvaluecolor(CreateColor(255,215,0));
Retracement4.setLineWeight(5);
retracement4.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement4, concat( "$", round(retracement4, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement4, concat( (coefficient_4 * 100), "%"), color.red, yes);
Retracement5.assignvaluecolor(CreateColor(0,255,255));
Retracement5.setLineWeight(2);
retracement5.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement5, concat( "$", round(retracement5, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement5, concat( (coefficient_5 * 100), "%"), color.red, yes);
Retracement6.assignvaluecolor(CreateColor(255,255,255));
Retracement6.setLineWeight(4);
Retracement6.hidebubble();
#AddChartBubble((barnumber == istodaybarnumber+10), retracement6, concat( "$", round(retracement6, 2)), color.red, yes);
AddChartBubble((downward and barnumber == highnumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
AddChartBubble((upward and barnumber == lownumberall), retracement6, concat( (coefficient_6 * 100), "%"), color.red, yes);
r0.setlineWeight(2);
r1.setlineWeight(2);
r2.setlineWeight(2);
r3.setlineWeight(2);
r4.setlineWeight(2);
r5.setlineWeight(2);
r6.setlineWeight(2);
retracement0.Hide();
retracement1.Hide();
retracement2.Hide();
retracement3.Hide();
retracement4.Hide();
retracement5.Hide();
retracement6.Hide();
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");