Limited MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Limited MACD", timeframe = "", timeframe_gaps = false, max_bars_back = 500)
src=input(close, title="source")
len=math.min(bar_index+1,200)//input.int(200, title="sensitivity lenght")
lenshortema=input.int(12,title="short ema lenght")
lenlongema=input.int(26, title="long ema lenght")
lensignal=input.int(9, title="signal lenght")
macd=ta.ema(src,lenshortema)-ta.ema(src,lenlongema)
macd:= 100*ta.ema(src,lenshortema)/ta.ema(src,lenlongema)-100
signal = ta.ema(macd,lensignal)
y = math.abs(macd)
n = bar_index
m = n<lenlongema? 1 : math.min(n-lenlongema+1,500)

t = ta.wma(y,m)//ta.cum(y)/n 
f = 100-200/(math.pow(1+1/(t*1),macd)+1)
plot(f, color= #2962ff)
y2 = math.abs(signal)
t2 = ta.wma(y,m)//ta.cum(y)/n 
f2 = 100-200/(math.pow(1+1/(t*1),signal)+1)
plot(f2, color=color.orange)

buy=ta.crossover(f,-50) or ta.crossover(f,50)
sell=ta.crossunder(f,50) or ta.crossunder(f,-50)
hline(50)
hline(-50)
bgcolor(color= buy? color.green : sell? color.red : na)
z=(f2/2+50)*2.55
barcolor(color=color.rgb(math.min(255+255-z*2,255),z, 0), display = display.none)
alertcondition(buy or sell)

col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")

hline(0, "Zero Line", color=color.new(#787B86, 50))
hist = f-f2
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))