EMA Slope Histogram
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chasm9a

//@version=5
indicator("EMA Slope Histogram", overlay = false)

emaPeriod = input.int(defval = 50, title="EMA Period" ,minval = 0, maxval = 999, step = 1)
changePeriod = input.int(defval = 5, title="Change Period" ,minval = 0, maxval = 999, step = 1)
changeinDirection = input.bool(defval = true, title="Show Change in Direction")

ema = ta.ema(close ,emaPeriod)

histo = (ema[0] - ema[changePeriod]) / changePeriod
histoColor = histo[0] < 0 ? color.red : color.green
histoAlpha = histo[0] > histo[1] ? 50 : 0
histoBeta = histo[0] < histo[1] ? 50 : 0
emaMinus = (histo[0] > 0 and histo[1] <0) ? 1 : na
emaPlus = (histo[0] < 0 and histo[1] >0)? 1 : na

plotchar(changeinDirection == true ? emaPlus : na, char = "*", size= size.tiny, location = location.top, color= color.red)
plotchar(changeinDirection == true ? emaMinus : na, char = "*", size= size.tiny, location = location.top, color=color.green)

plot(histo, style=plot.style_columns, color= color.new(histoColor, histoColor == color.red?histoAlpha:histoBeta))