Adaptive Rebound Line Bands (ARL Bands)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © More-Than-Enough

//@version=5
indicator(title = "Adaptive Rebound Line Bands", shorttitle = "ARL Bands", overlay = true, precision = 3, timeframe = "", timeframe_gaps = true)

Rsi_On = input.bool(true, "RSI Levels")
Magnetic_Waves_On     = input.bool(true, "Magnetic Waves")
Extra_Lines_On        = input.bool(false, "Extra Lines")
Extend_Extra_Lines_On = input.bool(false, "Extend Extra Lines")

Lines_Limit = input.int(defval = 31, title = "Lines Limit", minval = 5, maxval = 31)

Length   = input(14, "Length")
Source_1 = input(hlc3, "Source 1")
Source_2 = input(low , "Source 2")

Tp = input.float(defval = 2.30, title = "Top Deviation"   , minval = 0.0, step = 0.1)
Hi = input.float(defval = 1.15, title = "High Deviation"  , minval = 0.0, step = 0.1)
Lo = input.float(defval = 0.00, title = "Low Deviation"   , maxval = 0.0, step = 0.1)
Bt = input.float(defval = -1.2, title = "Bottom Deviation", maxval = 0.0, step = 0.1)

/// Magnetic Waves ///
Wave_Length     = 75
Magnetic_Source = hlc3
Wave_Current    = ta.ema(Magnetic_Source, Wave_Length)
Wave            = (Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length

North_Pole          = Wave * (104 * 0.01)
Northern_Hemisphere = Wave * (102 * 0.01)
Equator             = Wave * (100 * 0.01)
Southern_Hemisphere = Wave * (98  * 0.01)
South_Pole          = Wave * (96  * 0.01)

/// Extra Lines Calculations ///
// BB Lines
Line_Deviation = ta.stdev(close, 20)
Line_Split     = ta.sma(close, 20)

Top_Bb_Line    = Line_Split + (Line_Deviation * 3)
High_Bb_Line   = Line_Split + (Line_Deviation * 2)
Low_Bb_Line    = Line_Split - (Line_Deviation * 2)
Bottom_Bb_Line = Line_Split - (Line_Deviation * 3)

// Top Dip Line (KAMA)
Source_Sum = math.sum(math.abs(hlc3 - hlc3[1]), 5)
Change_Ratio = if Source_Sum != 0
    math.abs(hlc3 - hlc3[5]) / Source_Sum
Ratio_Smoothing = math.pow(Change_Ratio * ((2 / (2.5 + 1)) - (2 / (20 + 1))) + (2 / (20 + 1)), 2)
Previous_Ratio  = 0.0
Previous_Ratio := nz(Previous_Ratio[1]) + Ratio_Smoothing * (hlc3 - nz(Previous_Ratio[1]))
Ratio_Close = Previous_Ratio
HAshi_Close = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, Ratio_Close)

Top_Dip_Line = ta.ema(HAshi_Close, 20)

// Bottom Dip Line
Bottom_Dip_Line_Calculation = low - (ta.atr(20) * 5.0)
Bottom_Dip_Line = ta.sma(Bottom_Dip_Line_Calculation, 75)

/// ARL Bands ///
ARL = ta.ema(Source_2, Length)[1] + (Source_1 - ta.ema(Source_2, Length)[1]) / (Length * math.pow(Source_1 / ta.ema(close, Length)[1], 0))

ARL_T = ARL * (1.00 + (Tp * 0.01))
ARL_H = ARL * (1.00 + (Hi * 0.01))
ARL_L = ARL * (1.00 + (Lo * 0.01))
ARL_B = ARL * (1.00 + (Bt * 0.01))

/// RSI Level ///
Rsi_Level  = ta.rsi(close, 14)
Overbought = (Rsi_Level >= 70)
Oversold   = (Rsi_Level <= 30)

/////////////////
///// Plots /////
/////////////////

// Magnetic Waves
NP = plot(Magnetic_Waves_On ? North_Pole          : na, title = "North Pole"         , color = color.rgb(129, 199, 132, 100), linewidth = 2) // North Pole
NH = plot(Magnetic_Waves_On ? Northern_Hemisphere : na, title = "Northern Hemisphere", color = color.rgb(129, 199, 132,  80), linewidth = 2) // Northern Hemisphere
E  = plot(Magnetic_Waves_On ? Equator             : na, title = "Equator"            , color = color.rgb(144, 191, 249,  70), linewidth = 2) // Equator
SH = plot(Magnetic_Waves_On ? Southern_Hemisphere : na, title = "Southern Hemisphere", color = color.rgb(247, 124, 128,  80), linewidth = 2) // Southern Hemisphere
SP = plot(Magnetic_Waves_On ? South_Pole          : na, title = "South Pole"         , color = color.rgb(247, 124, 128, 100), linewidth = 2) // South Pole

fill(NH, NP, color.rgb(129, 199, 132, 80), "Top Zone")
fill( E, NH, color.rgb(129, 199, 132, 86), "High Zone")
fill( E, SH, color.rgb(247, 124, 128, 86), "Low Zone")
fill(SH, SP, color.rgb(247, 124, 128, 80), "Bottom Zone")

// Extra Lines
plot(Extra_Lines_On ? Top_Bb_Line    : na, title = "Top BB Line"   , color = color.new(color.white, 70), show_last = Extend_Extra_Lines_On ? na : Lines_Limit)
plot(Extra_Lines_On ? High_Bb_Line   : na, title = "High BB Line"  , color = color.new(color.white, 70), show_last = Extend_Extra_Lines_On ? na : Lines_Limit)
plot(Extra_Lines_On ? Low_Bb_Line    : na, title = "Low BB Line"   , color = color.new(color.white, 70), show_last = Extend_Extra_Lines_On ? na : Lines_Limit)
plot(Extra_Lines_On ? Bottom_Bb_Line : na, title = "Bottom BB Line", color = color.new(color.white, 70), show_last = Extend_Extra_Lines_On ? na : Lines_Limit)

plot(Extra_Lines_On ? Top_Dip_Line    : na, title = "Top Dip Line"   , color = color.new(color.yellow, 50), style = plot.style_circles, show_last = Extend_Extra_Lines_On ? na : Lines_Limit)
plot(Extra_Lines_On ? Bottom_Dip_Line : na, title = "Bottom Dip Line", color = color.new(color.yellow, 50), style = plot.style_circles, show_last = Extend_Extra_Lines_On ? na : Lines_Limit)

// ARLs
plot(ARL_T, title = "Top ARL"   , color = color.red)
plot(ARL_H, title = "High ARL"  , color = color.red)
plot(ARL_L, title = "Low ARL"   , color = color.lime)
plot(ARL_B, title = "Bottom ARL", color = color.lime)

// RSI Level
plotshape(Rsi_On ? Overbought : na, title = "Overbought", style = shape.labeldown, location = location.abovebar, color = color.red , size = size.tiny)
plotshape(Rsi_On ? Oversold   : na, title = "Oversold"  , style = shape.labelup  , location = location.belowbar, color = color.lime, size = size.tiny)