Filtered Momentum Indicator (FMI)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Filtered Momentum Indicator (FMI)", overlay=false)

// Calculation Inputs
momentumPeriod = input(14, "Momentum Period")
bollingerPeriod = input(20, "Bollinger Bands Period")
deviations = input(2.0, "Bollinger Bands Deviations")
signalMultiplier = input(2.0, 'Signal Comparison Multiplier', tooltip = 'To generate bull/bear signals, the F- or F+ must be greater than or equal to [this multiplier] times the value of the opposite line')
signalThreshold = input(3.0, 'Signal Value Threshold', tooltip = 'F+ or F- must be above this threshold to generate a bull/bear signal.')

// Calculate Momentum
momentum = (close / close[momentumPeriod]) * 100

// Calculate Bollinger Bands
basis = ta.sma(momentum, bollingerPeriod)
deviation = ta.stdev(momentum, bollingerPeriod)
upperBand = basis + (deviations * deviation)
lowerBand = basis - (deviations * deviation)

// Calculate Filtered Momentum
filteredPlus = upperBand - momentum
filteredMinus = momentum - lowerBand

// Colors
fmiColor = filteredMinus >= signalMultiplier*filteredPlus and filteredMinus >= signalThreshold ? color.lime : filteredPlus >= signalMultiplier*filteredMinus and filteredPlus >= signalThreshold ? color.fuchsia : color.yellow
bgcolor(color.new(fmiColor, 80))
barcolor(fmiColor)

// Plotting
plot(filteredPlus, color=color.aqua, linewidth=4, title="F+")
plot(filteredMinus, color=color.yellow, linewidth=4, title="F-")
hline(0, linewidth=2)
