Tide Finder (TiFi)
// 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 = "Tide Finder", shorttitle = "TiFi", overlay = true, precision = 2)


/// Settings ///
Magnetic_Waves_On = input.bool(title = "Magnetic Waves", defval = true)
Water_Level_On = input.bool(title = "Water Level", defval = true)
Lines_On = input.bool(title = "Lines", defval = false)
Compass_On = input.bool(title = "Compass", defval = false)
Compass_Record_On = input.bool(title = "Compass Record", defval = false)
Cycle_On = input.bool(title = "Day & Night Cycle", defval = false)
Ship_Path_On = input.bool(title = "Ship Path", defval = false)


    /// Calculations ///


/// Day & Night Cycle ///
AM = ta.ema(close, 55)
PM = ta.ema(close, 200)

Day = ta.crossunder(PM, AM)
Night = ta.crossover(PM, AM)


/// Compass (Magnetic Polarity) ///
North_Polarity_Source = ta.ema(hlc3, 8)
North_Polarity = (North_Polarity_Source[1] * (7) + hlc3) / 8

South_Polarity_Source = ta.ema(hlc3, 24)
South_Polarity = (South_Polarity_Source[1] * (23) + hlc3) / 24


/// Water Level ///
Water_Level = ta.rsi(close, 14)

High_Water_Level = (Water_Level >= 68.5)
Low_Water_Level = (Water_Level <= 31.5)


/// Lines ///
// Slow Middle Line Calculations
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)
//

Line_Deviation = ta.stdev(close, 20)
Line_Split = ta.sma(close, 20)

Top_Line = Line_Split + (Line_Deviation * 3)
High_Line = Line_Split + (Line_Deviation * 2)

Slow_Middle_Line = ta.ema(HAshi_Close, 20) //  Slow Middle Line (Kaufman Adaptive Moving Average)
Fast_Middle_Line = ta.ema(low, 14)[1] + (hlc3 - ta.ema(low, 14)[1]) / (14 * math.pow(hlc3 / ta.ema(close, 14)[1], 4)) // Fast Middle Line (Adaptive Rebound Line)

Low_Line = Line_Split - (Line_Deviation * 2)
Bottom_Line = Line_Split - (Line_Deviation * 3)

Dip_Line_Calculation = low - (ta.atr(20) * 5.0)
Dip_Line = ta.sma(Dip_Line_Calculation, 75)


/// Magnetic Waves ///
Wave_Length = 75
Magnetic_Source = hlc3
Wave_Current = ta.ema(Magnetic_Source, Wave_Length)

North_Pole = ((Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length) * (104 * 0.01) // Alt: (105 * 0.01)
Northern_Hemisphere = ((Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length) * (102 * 0.01) // Alt: (102.5 * 0.01)

Equator = ((Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length) * (100 * 0.01)

Southern_Hemisphere = ((Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length) * (98 * 0.01) // Alt: (97.5 * 0.01)
South_Pole = ((Wave_Current[1] * (Wave_Length - 1) + Magnetic_Source) / Wave_Length) * (96 * 0.01) // Alt: (95 * 0.01)


    /// Plots /// "↓" "↑" "•"


/// Day & Time Cycle ///
plot(Cycle_On ? PM : na, title = "Night Time", color = color.blue, linewidth = 4)
plot(Cycle_On ? AM : na, title = "Day Time", color = color.yellow, linewidth = 4)

plotshape(Cycle_On ? Night : na, title = "Moon", style = shape.circle, location = location.top, color = color.blue, size = size.normal)
plotshape(Cycle_On ? Day : na, title = "Sun", style = shape.circle, location = location.top, color = color.yellow, size = size.normal)


/// Magnetic Waves ///
plot(Magnetic_Waves_On ? North_Pole : na, title = "North Pole", color = color.rgb(129, 199, 132, 50), style = plot.style_circles) // North Pole
plot(Magnetic_Waves_On ? Northern_Hemisphere : na, title = "Northern Hemisphere", color = color.rgb(129, 199, 132, 75), style = plot.style_circles) // Northern Hemisphere

plot(Magnetic_Waves_On ? Equator : na, title = "Equator", color = color.rgb(144, 191, 249, 50), style = plot.style_circles) // Equator

plot(Magnetic_Waves_On ? Southern_Hemisphere : na, title = "Southern Hemisphere", color = color.rgb(247, 124, 128, 75), style = plot.style_circles) // Southern Hemisphere
plot(Magnetic_Waves_On ? South_Pole : na, title = "South Pole", color = color.rgb(247, 124, 128, 50), style = plot.style_circles) // South Pole


/// Ship Path ///
plot(Ship_Path_On ? ta.sar(0.02, 0.02, 0.2) : na, title = "Wave Shape", color = color.orange, style = plot.style_stepline_diamond)
plot(Ship_Path_On ? high : na, title = "High Point", color = color.rgb(0, 230, 118, 50), style = plot.style_stepline)
plot(Ship_Path_On ? close : na, title = "Close Point", color = color.white, linewidth = 2)
plot(Ship_Path_On ? low : na, title = "Low Point", color = color.rgb(255, 82, 82, 50), style = plot.style_stepline)


/// Water Level ///
plotshape(Water_Level_On ? High_Water_Level : na, title = "High Water Level", style = shape.circle, location = location.abovebar, color = color.red, size = size.tiny)
plotshape(Water_Level_On ? Low_Water_Level : na, title = "Low Water Level", style = shape.circle, location = location.belowbar, color = color.lime, size = size.tiny)


/// Lines ///
plot(Lines_On ? Top_Line : na, title = "Top Line", color = color.rgb(165, 214, 167, 50))
plot(Lines_On ? High_Line : na, title = "High Line", color = color.rgb(165, 214, 167))

plot(Lines_On ? Slow_Middle_Line : na, title = "Slow Middle Line (KAMA)", color = color.rgb(144, 191, 249))
plot(Lines_On ? Fast_Middle_Line : na, title = "Fast Middle Line (ARL)", color = color.rgb(144, 191, 249))

plot(Lines_On ? Low_Line : na, title = "Low Line", color = color.rgb(250, 161, 164))
plot(Lines_On ? Bottom_Line : na, title = "Bottom Line", color = color.rgb(250, 161, 164, 50))

plot(Lines_On ? Dip_Line : na, title = "Dip Line", color = color.yellow)


/// Compass ///
plot(Compass_On ? South_Polarity : na, title = "South Magnetic Polarity", color = color.red, linewidth = 3, offset = 8, show_last = Compass_Record_On ? na : 25) // South Polarity
plot(Compass_On ? North_Polarity : na, title = "North Magnetic Polarity", color = color.rgb(0, 188, 212), linewidth = 3, offset = 3, show_last = Compass_Record_On ? na : 20) // North Polarity
