// © FriendOfTheTrend
//@version=5
indicator("Auto Fibonacci From Previous D/W/M/Q/Y High & Low With Percentages & Alerts", shorttitle="Auto Fibonacci by Trend Friend", overlay=true)
//Fib On/Off
fibOn = input.bool(true, title="Turn Fibonacci Levels On/Off")
//Get Timeframe And Previous High And Low
timeFrame = input.timeframe("1D", title="Timeframe", options=["1D", "1W", "1M", "3M", "12M"])
tickerHigh = request.security(syminfo.tickerid, timeFrame, high[1], barmerge.gaps_off, barmerge.lookahead_on)
tickerLow = request.security(syminfo.tickerid, timeFrame, low[1], barmerge.gaps_off, barmerge.lookahead_on)
//S&R Line Colors
supportColor = input.color(color.lime, title="Support/Lower Line Color", group="Line Style")
resistanceColor = input.color(color.red, title="Resistance/Higher Line Color", group="Line Style")
//High & Low Line Colors
highColor = input.color(color.white, title="Previous High Line Color", group="Line Style")
lowColor = input.color(color.white, title="Previous Low Line Color", group="Line Style")
//Linewidth
lw = input.int(1, title="Line Width", minval=1, maxval=4, group="Line Style")
//Table On/Off
dataTableOn = input.bool(true, title="Percentage Updates Table On/Off", group="Info Table")
//Table Colors
tableColor = input.color(color.blue, title="Percentage Table Color", group="Info Table")
previousLabelColor = input.color(color.purple, title="Previous Timeframe Being Used Label", group="Info Table")
//Table Positions
bright = position.bottom_right
bleft = position.bottom_left
bcenter = position.bottom_center
tright = position.top_right
tleft = position.top_left
tcenter = position.top_center
mright = position.middle_right
mleft = position.middle_left
mcenter = position.middle_center
tablePosition = input.string(bright, title="Table Position", options=[bright, bleft, bcenter, tright, tleft, tcenter, mright, mleft, mcenter], group="Info Table")
//Table Position Logic
tpos = position.bottom_right
if tablePosition == bleft
tpos := bleft
if tablePosition == bright
tpos := bright
if tablePosition == bcenter
tpos := bcenter
if tablePosition == tleft
tpos := tleft
if tablePosition == tright
tpos := tright
if tablePosition == tcenter
tpos := tcenter
if tablePosition == mleft
tpos := mleft
if tablePosition == mright
tpos := mright
if tablePosition == mcenter
tpos := mcenter
//Calculate Fibonacci Levels
fib(level) =>
fibGap = tickerHigh - tickerLow
fibLevel = tickerHigh - (fibGap * level)
fibext(extlevel) =>
fibExtGap = tickerLow - tickerHigh
fibExtLevel = tickerHigh - (fibExtGap * extlevel)
//Fib Retacement Variables
fib1 = if fibOn
fib(.0)
fib2 = if fibOn
fib(.117)
fib3 = if fibOn
fib(.236)
fib4 = if fibOn
fib(.382)
fib5 = if fibOn
fib(.5)
fib6 = if fibOn
fib(.618)
fib7 = if fibOn
fib(.786)
fib8 = if fibOn
fib(.883)
fib9 = if fibOn
fib(1)
fib10 = if fibOn
fib(1.272)
fib11 = if fibOn
fib(1.618)
fib12 = if fibOn
fib(2)
fib13 = if fibOn
fib(2.272)
fib14 = if fibOn
fib(2.618)
fib15 = if fibOn
fib(3)
fib16 = if fibOn
fib(3.272)
//Fib Extension Variables
fibext1 = if fibOn
fibext(.272)
fibext2 = if fibOn
fibext(.618)
fibext3 = if fibOn
fibext(1)
fibext4 = if fibOn
fibext(1.272)
fibext5 = if fibOn
fibext(1.618)
fibext6 = if fibOn
fibext(2)
fibext7 = if fibOn
fibext(2.272)
fibext8 = if fibOn
fibext(2.618)
fibext9 = if fibOn
fibext(3)
fibext10 = if fibOn
fibext(3.272)
//Fibonacci Retracements
plot(fib1, title="Previous High", color=highColor, linewidth=lw, style=plot.style_line)
plot(fib2, title=".117 Fib Ret", color=close > fib2 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib3, title=".236 Fib Ret", color=close > fib3 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib4, title=".382 Fib Ret", color=close > fib4 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib5, title=".50 Fib Ret", color=close > fib5 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib6, title=".618 Fib Ret", color=close > fib6 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib7, title=".786 Fib Ret", color=close > fib7 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib8, title=".883 Fib Ret", color=close > fib8 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib9, title="Previous Low", color=lowColor, linewidth=lw, style=plot.style_line)
plot(fib10, title="1.272 Fib Ret", color=close > fib10 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib11, title="1.618 Fib Ret", color=close > fib11 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib12, title="2 Fib Ret", color=close > fib12 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib13, title="2.272 Fib Ret", color=close > fib13 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib14, title="2.618 Fib Ret", color=close > fib14 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib15, title="3 Fib Ret", color=close > fib15 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fib16, title="3.272 Fib Ret", color=close > fib16 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
//Fibonacci Extensions
plot(fibext1, title=".272 Fib Ext", color=close > fibext1 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext2, title=".618 Fib Ext", color=close > fibext2 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext3, title="1 Fib Ext", color=close > fibext3 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext4, title="1.272 Fib Ext", color=close > fibext4 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext5, title="1.618 Fib Ext", color=close > fibext5 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext6, title="2 Fib Ext", color=close > fibext6 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext7, title="2.272 Fib Ext", color=close > fibext7 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext8, title="2.618 Fib Ext", color=close > fibext8 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext9, title="3 Fib Ext", color=close > fibext9 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
plot(fibext10, title="3.272 Fib Ext", color=close > fibext10 ? supportColor : resistanceColor, linewidth=lw, style=plot.style_line)
//Price Diiference Fib Variables
float priceDiffFibDown = 0
float priceDiffFibUp = 0
fibUpText = ""
fibDownText = ""
//Price difference Fib Extensions
if close > fib1 and close < fibext1
priceDiffFibDown := (close - fib1) / close * 100
priceDiffFibUp := (fibext1 - close) / fibext1 * 100
fibUpText := " from .272 Fib Ext"
fibDownText := " from Previous High"
else if close > fibext1 and close < fibext2
priceDiffFibDown := (close - fibext1) / close * 100
priceDiffFibUp := (fibext2 - close) / fibext2 * 100
fibUpText := " from .618 Fib Ext"
fibDownText := " from .272 Fib Ext"
else if close > fibext2 and close < fibext3
priceDiffFibDown := (close - fibext2) / close * 100
priceDiffFibUp := (fibext3 - close) / fibext3 * 100
fibUpText := " from 1 Fib Ext"
fibDownText := " from .618 Fib Ext"
else if close > fibext3 and close < fibext4
priceDiffFibDown := (close - fibext3) / close * 100
priceDiffFibUp := (fibext4 - close) / fibext4 * 100
fibUpText := " from 1.272 Fib Ext"
fibDownText := " from 1 Fib Ext"
else if close > fibext4 and close < fibext5
priceDiffFibDown := (close - fibext4) / close * 100
priceDiffFibUp := (fibext5 - close) / fibext5 * 100
fibUpText := " from 1.618 Fib Ext"
fibDownText := " from 1.272 Fib Ext"
else if close > fibext5 and close < fibext6
priceDiffFibDown := (close - fibext5) / close * 100
priceDiffFibUp := (fibext6 - close) / fibext6 * 100
fibUpText := " from 2 Fib Ext"
fibDownText := " from 1.618 Fib Ext"
else if close > fibext6 and close < fibext7
priceDiffFibDown := (close - fibext6) / close * 100
priceDiffFibUp := (fibext7 - close) / fibext7 * 100
fibUpText := " from 2.272 Fib Ext"
fibDownText := " from 2 Fib Ext"
else if close > fibext7 and close < fibext8
priceDiffFibDown := (close - fibext7) / close * 100
priceDiffFibUp := (fibext8 - close) / fibext8 * 100
fibUpText := " from 2.618 Fib Ext"
fibDownText := " from 2.272 Fib Ext"
else if close > fibext8 and close < fibext9
priceDiffFibDown := (close - fibext8) / close * 100
priceDiffFibUp := (fibext9 - close) / fibext9 * 100
fibUpText := " from 3 Fib Ext"
fibDownText := " from 2.618 Fib Ext"
else if close > fibext9 and close < fibext10
priceDiffFibDown := (close - fibext9) / close * 100
priceDiffFibUp := (fibext10 - close) / fibext10 * 100
fibUpText := " from 3.272 Fib Ext"
fibDownText := " from 3 Fib Ext"
else if close > fibext10
fibUpText := "Price Above Fib Levels"
fibDownText := "Price Above Fib Levels"
//Price difference Fib Retracements
else if close < fib1 and close > fib2
priceDiffFibUp := (close - fib1) / close * -100
priceDiffFibDown := (fib2 - close) / fib2 * -100
fibUpText := " from Previous High"
fibDownText := " from .117 Fib Ret"
else if close < fib2 and close > fib3
priceDiffFibUp := (close - fib2) / close * -100
priceDiffFibDown := (fib3 - close) / fib3 * -100
fibUpText := " from .117 Fib Ret"
fibDownText := " from .236 Fib Ret"
else if close < fib3 and close > fib4
priceDiffFibUp := (close - fib3) / close * -100
priceDiffFibDown := (fib4 - close) / fib4 * -100
fibUpText := " from .236 Fib Ret"
fibDownText := " from .382 Fib Ret"
else if close < fib4 and close > fib5
priceDiffFibUp := (close - fib4) / close * -100
priceDiffFibDown := (fib5 - close) / fib5 * -100
fibUpText := " from .382 Fib Ret"
fibDownText := " from .5 Fib Ret"
else if close < fib5 and close > fib6
priceDiffFibUp := (close - fib5) / close * -100
priceDiffFibDown := (fib6 - close) / fib6 * -100
fibUpText := " from .5 Fib Ret"
fibDownText := " from .618 Fib Ret"
else if close < fib6 and close > fib7
priceDiffFibUp := (close - fib6) / close * -100
priceDiffFibDown := (fib7 - close) / fib7 * -100
fibUpText := " from .618 Fib Ret"
fibDownText := " from .786 Fib Ret"
else if close < fib7 and close > fib8
priceDiffFibUp := (close - fib7) / close * -100
priceDiffFibDown := (fib8 - close) / fib8 * -100
fibUpText := " from .786 Fib Ret"
fibDownText := " from .883 Fib Ret"
else if close < fib8 and close > fib9
priceDiffFibUp := (close - fib8) / close * -100
priceDiffFibDown := (fib9 - close) / fib9 * -100
fibUpText := " from .883 Fib Ret"
fibDownText := " from Previous Low"
else if close < fib9 and close > fib10
priceDiffFibUp := (close - fib9) / close * -100
priceDiffFibDown := (fib10 - close) / fib10 * -100
fibUpText := " from Previous Low"
fibDownText := " from 1.272 Fib Ret"
else if close < fib10 and close > fib11
priceDiffFibUp := (close - fib10) / close * -100
priceDiffFibDown := (fib11 - close) / fib11 * -100
fibUpText := " from 1.272 Fib Ret"
fibDownText := " from 1.618 Fib Ret"
else if close < fib11 and close > fib12
priceDiffFibUp := (close - fib11) / close * -100
priceDiffFibDown := (fib12 - close) / fib12 * -100
fibUpText := " from 1.618 Fib Ret"
fibDownText := " from 2 Fib Ret"
else if close < fib12 and close > fib13
priceDiffFibUp := (close - fib12) / close * -100
priceDiffFibDown := (fib13 - close) / fib13 * -100
fibUpText := " from 2 Fib Ret"
fibDownText := " from 2.272 Fib Ret"
else if close < fib13 and close > fib14
priceDiffFibUp := (close - fib13) / close * -100
priceDiffFibDown := (fib14 - close) / fib14 * -100
fibUpText := " from 2.272 Fib Ret"
fibDownText := " from 2.618 Fib Ret"
else if close < fib14 and close > fib15
priceDiffFibUp := (close - fib14) / close * -100
priceDiffFibDown := (fib15 - close) / fib15 * -100
fibUpText := " from 2.618 Fib Ret"
fibDownText := " from 3 Fib Ret"
else if close < fib15 and close > fib16
priceDiffFibUp := (close - fib15) / close * -100
priceDiffFibDown := (fib16 - close) / fib16 * -100
fibUpText := " from 3 Fib Ret"
fibDownText := " from 3.272 Fib Ret"
else if close < fib16
fibUpText := "Price Below Fib Levels"
fibDownText := "Price Below Fib Levels"
//Alert Fib Retracements
if ta.cross(close, fib1)
alert("Price Crossing Previous High")
if ta.cross(close, fib2)
alert("Price Crossing Fibonacci .117 Level")
if ta.cross(close, fib3)
alert("Price Crossing Fibonacci .236 Level")
if ta.cross(close, fib4)
alert("Price Crossing Fibonacci .382 Level")
if ta.cross(close, fib5)
alert("Price Crossing Fibonacci .5 Level")
if ta.cross(close, fib6)
alert("Price Crossing Fibonacci .618 Level")
if ta.cross(close, fib7)
alert("Price Crossing Fibonacci .786 Level")
if ta.cross(close, fib8)
alert("Price Crossing Fibonacci .883 Level")
if ta.cross(close, fib9)
alert("Price Crossing Previous Low")
if ta.cross(close, fib10)
alert("Price Crossing Fibonacci 1.272 Retracement")
if ta.cross(close, fib11)
alert("Price Crossing Fibonacci 1.618 Retracement")
if ta.cross(close, fib12)
alert("Price Crossing Fibonacci 2 Retracement")
if ta.cross(close, fib13)
alert("Price Crossing Fibonacci 2.272 Retracement")
if ta.cross(close, fib14)
alert("Price Crossing Fibonacci 2.618 Retracement")
if ta.cross(close, fib15)
alert("Price Crossing Fibonacci 3 Retracement")
if ta.cross(close, fib16)
alert("Price Crossing Fibonacci 3.272 Retracement")
//Alert Fib Extensions
if ta.cross(close, fibext1)
alert("Price Crossing Fibonacci .272 Extension")
if ta.cross(close, fibext2)
alert("Price Crossing Fibonacci .618 Extension")
if ta.cross(close, fibext3)
alert("Price Crossing Fibonacci 1 Extension")
if ta.cross(close, fibext4)
alert("Price Crossing Fibonacci 1.272 Extension")
if ta.cross(close, fibext5)
alert("Price Crossing Fibonacci 1.618 Extension")
if ta.cross(close, fibext6)
alert("Price Crossing Fibonacci 2 Extension")
if ta.cross(close, fibext7)
alert("Price Crossing Fibonacci 2.272 Extension")
if ta.cross(close, fibext8)
alert("Price Crossing Fibonacci 2.618 Extension")
if ta.cross(close, fibext9)
alert("Price Crossing Fibonacci 3 Extension")
if ta.cross(close, fibext10)
alert("Price Crossing Fibonacci 3.272 Extension")
//Timframe Label
timeframeLabel = ""
if timeFrame == "1D"
timeframeLabel := "Using Previous Day"
if timeFrame == "1W"
timeframeLabel := "Using Previous Week"
if timeFrame == "1M"
timeframeLabel := "Using Previous Month"
if timeFrame == "3M"
timeframeLabel := "Using Previous Quarter"
if timeFrame == "12M"
timeframeLabel := "Using Previous Year"
//Create price difference table data
percentFromFibUp = str.tostring(priceDiffFibUp, format.percent) + str.tostring(fibUpText)
percentFromFibDown = str.tostring(priceDiffFibDown, format.percent) + str.tostring(fibDownText)
//Plot Price Difference Table
dataTable = table.new(tpos, columns=1, rows=3, bgcolor=tableColor)
if dataTableOn and barstate.islast
table.cell(table_id=dataTable, column=0, row=0, text=timeframeLabel, height=0, text_color=color.white, text_halign=text.align_center, text_valign= text.align_top, bgcolor=previousLabelColor)
table.cell(table_id=dataTable, column=0, row=1, text=percentFromFibUp, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_top)
table.cell(table_id=dataTable, column=0, row=2, text=percentFromFibDown, height=0, text_color=color.white, text_halign=text.align_left, text_valign= text.align_top)