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="Polychromatic Momentum Indicator", shorttitle="PMI")

// Input variables
len = input(14, title="Length")
src = input(close, title="Source")

// Calculation of momentum
mom = src - src[len]
pdm = sum(max(mom, 0), len)
ndm = sum(max(-mom, 0), len)
pdi = 100 * pdm / (pdm + ndm)
ndi = 100 * ndm / (pdm + ndm)
pmi = pdi - ndi

// Plotting of histograms and moving average line
plot(pmi, color=pmi > pmi[1] ? color.green : pmi < pmi[1] ? color.red : color.yellow, style=plot.style_histogram, linewidth=2)
ma = sma(pmi, len/2)
plot(ma, color=color.white, linewidth=2)

// Alert conditions
buy_alert = crossover(pmi, ma)
sell_alert = crossunder(pmi, ma)

// Alert messages
alertcondition(buy_alert, title="Buy Alert", message="PMI crossed above MA")
alertcondition(sell_alert, title="Sell Alert", message="PMI crossed below MA")
