Trends
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FakePilot Trends v2.3
// This indicator have been tested mostly on Daily candles with BTC
// Set alarms, exit or enter with Bull, Neutral and Bear triggers

//@version=5
indicator('Trends', overlay=true)

// User Input
emaTop = input(defval=32, title='Length EMA Fast')
emaMiddle = input(defval=45, title='Length EMA Middle')
emaBottom = input(defval=57, title='Length EMA Slow')
marginATR = input(defval=15, title='ATR Bars Margin')
rangeATRmult = input.float(defval=0.60, title='Smoothing Multiplier', step=0.45)
middleToggle = input(defval=false, title='Middle line ON/OFF')

//Moving Averages
emaFast = ta.ema(close, emaTop)
emaMedium = ta.ema(close, emaMiddle)
emaSlow = ta.ema(close, emaBottom)
emaDiff = emaFast - emaSlow
emaMargin = rangeATRmult * ta.atr(marginATR)

// Case Differentiation
emaBull = emaDiff > emaMargin
emaBear = emaDiff < -emaMargin
emaNeutral = emaDiff < emaMargin and emaDiff > -emaMargin

var bool inBull = na 
var bool inBear = na
var bool inNeutral = na

if emaBull
    inBull := true
    inBear := false
    inNeutral := false

if emaBear
    inBull := false
    inBear := true
    inNeutral := false

if emaNeutral
    inBull := false
    inBear := false
    inNeutral := true

// Colors
colorBull = color.new(color(#4caf50), 0)
colorBullFill = color.new(color(#4caf50), 90)
colorBear = color.new(color(#f23645), 0)
colorBearFill = color.new(color(#f23645), 90)
colorNeutral = color.new(color(#ff9800), 0)
colorNeutralFill = color.new(color(#ff9800), 90)

// Plotting
bear = emaBear ? colorBear : colorNeutral
bearFill = emaBear ? colorBearFill : colorNeutralFill

// Draw EMA top Line
emaFastPlot = plot(emaFast, linewidth=2, title='Fast', color=emaBull ? colorBull : bear)

// Draw EMA middle Line
emaMediumPlot = plot(middleToggle and emaMedium ? emaMedium : na, title='Middle', linewidth=1, color=emaBull ? colorBull : bear)

// Draw EMA bottom Line
emaSlowPlot = plot(emaSlow, linewidth=2, title='Slow', color=emaBull ? colorBull : bear)

// Fill between EMA top and bottom lines
fill(emaFastPlot, emaSlowPlot, color=emaBull ? colorBullFill : bearFill)

// Alerts (the [1] means 1 candle backwards)
alertcondition(condition = inBull and not inBull[1], title = "Bull", message = "Bull")
alertcondition(condition = inBear and not inBear[1], title = "Bear", message = "Bear")
alertcondition(condition = inNeutral and not inNeutral[1], title ="Neutral", message = "Neutral")
