Basic Polychromatic Momentum Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sentello88

//@version=4
study(title="Basic Polychromatic Momentum Indicator v3", shorttitle="BPMIv3", overlay=false)

// User inputs
src = input(close, title="Source")
len = input(14, title="Length", minval=1)
alertcondition = input(title="Enable Alert?", type=input.bool, defval=true)

// Calculation of Polychromatic Momentum Indicator
mom = src - src[len]
pmi = mom - (mom - mom[len]) / len

// Smoothed PMI
smoothing = input(2, title="Smoothing", minval=1)
sm_pmi = sma(pmi, smoothing)

// Alert condition
alertLevel = input(title="Alert Level", type=input.float, defval=0)
alertMessage = "PMI is crossing the alert level " + tostring(alertLevel)
if alertcondition and sma(pmi, 1)[1] < alertLevel and sma(pmi, 1) > alertLevel
    alert(alertMessage)

// Plotting
plot(sm_pmi, title="PMI", color=color.blue, linewidth=2, transp=0)
hline(0, title="Zero Line", color=color.gray, linestyle=hline.style_dotted)
bgcolor(sm_pmi > 0 ? color.green : color.red, transp=90) // Color background based on PMI direction
