Moving Average Macro Trend Filter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jordanfray

//@version=5
indicator(title="Moving Average Macro Trend Filter", overlay=true)

// Indenting Classs
indent_1 = " "
indent_2 = "  "
indent_3 = "   "
indent_4 = "    "
indent_5 = "     "
indent_6 = "      "
indent_7 = "       "
indent_8 = "        "
indent_9 = "         "
indent_10 = "          "

// Group Titles
group_one_title = "Fast EMA Settings"
group_two_title = "Slow EMA Settings"
group_three_title = "Background Colors"

// Input Tips
tooltipDoNotRepaint = "To prevent the moving averages from repainting, you can have the last bar's value be used intead of the current bar's value."
tooltipBullBackground = "The background on the chart when the fast moving average is above the slow moving average."
tooltipBearBackground = "The background on the chart when the slow moving average is above the fast moving average."
tooltipSidewaysBackground = "The background on the chart when the fast moving average and slow moving average don't agree on trend direction."
tooltipBackgroundSmoothing = "How many bars to lookback when comparing the slope of the moving average. The higher the number, the less often short term changes in line slope will cause changes to the background."

oceanBlue = color.new(#0C6090,0)
skyBlue = color.new(#00A5FF,0)
green = color.new(#2DBD85,0)
red = color.new(#E02A4A,0)
lightGreen = color.new(#2DBD85,85)
lightRed = color.new(#E02A4A,85)
lightYellow = color.new(#FFF900,85)

// Fast EMA
fastEMAtimeframe = input.timeframe(defval="", title="Timeframe", group=group_one_title)
fastEMAlength = input.int(defval=50, minval=1, title="Length", group=group_one_title)
fastEMAtype = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title="Type", group=group_one_title)
fastEMAsource = input.source(defval=close, title="Source", group=group_one_title)
fastEMA = switch fastEMAtype
    "EMA" => ta.ema(fastEMAsource, fastEMAlength)
    "SMA" => ta.sma(fastEMAsource, fastEMAlength)
    "RMA" => ta.rma(fastEMAsource, fastEMAlength)
    "WMA" => ta.wma(fastEMAsource, fastEMAlength)
    => na

// Slow EMA
slowEMAtimeframe = input.timeframe(defval="", title="Timeframe", group=group_two_title)
slowEMAlength = input.int(defval=200, minval=1, title="Length", group=group_two_title)
slowEMAtype = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title="Type", group=group_two_title)
slowEMAsource = input.source(defval=close, title="Source", group=group_two_title)
slowEMA = switch slowEMAtype
    "EMA" => ta.ema(slowEMAsource, slowEMAlength)
    "SMA" => ta.sma(slowEMAsource, slowEMAlength)
    "RMA" => ta.rma(slowEMAsource, slowEMAlength)
    "WMA" => ta.wma(slowEMAsource, slowEMAlength)
    => na

// Plot Background
bullColor = input.color(title="Bull Market", defval=lightGreen, tooltip=tooltipBullBackground, group=group_three_title)
bearColor = input(title="Bear Market",  defval=lightRed, tooltip=tooltipBearBackground, group=group_three_title)
sidewaysColor = input(title="Sideways",  defval=lightYellow, tooltip=tooltipSidewaysBackground, group=group_three_title)
lookbackPeriod = input.int(title="Lookback Smoothing", defval=3, minval=1, tooltip=tooltipBackgroundSmoothing, group=group_three_title)
noRepaint = input.bool(defval=false, title="Don't repaint on higher timeframes", tooltip=tooltipDoNotRepaint, group=group_three_title)

fastEMA_ = fastEMAtimeframe == timeframe.period ? fastEMA : noRepaint ? request.security(syminfo.ticker, fastEMAtimeframe, fastEMA[1], lookahead = barmerge.lookahead_on) : request.security(syminfo.ticker, fastEMAtimeframe, fastEMA)
slowEMA_ = slowEMAtimeframe == timeframe.period ? slowEMA : noRepaint ? request.security(syminfo.ticker, slowEMAtimeframe, slowEMA[1], lookahead = barmerge.lookahead_on) : request.security(syminfo.ticker, slowEMAtimeframe, slowEMA)
color = fastEMA_ > slowEMA_ and fastEMA_ > fastEMA_[lookbackPeriod] ? bullColor : slowEMA_ > fastEMA_ and fastEMA_[lookbackPeriod] > fastEMA_ ? bearColor : sidewaysColor
    
plot(fastEMA_, title="Fast EMA", linewidth=2, color=skyBlue, editable=true)
plot(slowEMA_, title="Slow EMA", linewidth=2, color=oceanBlue, editable=true)
bgcolor(color=color)