OMA (One More Average) RSI Histogram [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("OMA (One More Average) RSI Histogram [Loxx]",
     shorttitle="OMARSIH [Loxx]",
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

_oma(src, len, const, adapt) =>
    e1 = nz(src[1]), e2 = nz(src[1]), e3 = nz(src[1])
    e4 = nz(src[1]), e5 = nz(src[1]), e6 = nz(src[1])
    
    averagePeriod = len
    noise = 0.00000000001
    minPeriod = averagePeriod/2.0
    maxPeriod = minPeriod*5.0
    endPeriod = math.ceil(maxPeriod)
    signal = math.abs(src - nz(src[endPeriod]))
    
    if adapt
        for k = 1 to endPeriod
            noise += math.abs(src - nz(src[k]))
        averagePeriod := math.ceil(((signal / noise) * (maxPeriod - minPeriod)) + minPeriod)
    
    //calc jurik momentum
    alpha = (2.0 + const) / (1.0 + const + averagePeriod)
    
    e1 := nz(e1[1] + alpha * (src - e1[1]), src)
    e2 := nz(e2[1] + alpha * (e1 - e2[1]), e1)
    v1 = 1.5 * e1 - 0.5 * e2
    
    e3 := nz(e3[1] + alpha * (v1 - e3[1]), v1)
    e4 := nz(e4[1] + alpha * (e3 - e4[1]), e3)
    v2 = 1.5 * e3 - 0.5 * e4
    
    e5 := nz(e5[1] + alpha * (v2 - e5[1]), v2) 
    e6 := nz(e6[1] + alpha * (e5 - e6[1]), e5) 
    v3 = 1.5 * e5 - 0.5 * e6
    v3
    
src = input.source(close, "Source", group = "Basic Settings")
const = input.float(1.5, "Speed", step = .01, group = "Basic Settings")
adapt = input.bool(true, "Make it adapotive?", group = "Basic Settings")
buylev = input.int(65, "Buy Level", group = "Basic Settings")
selllev = input.int(35, "Sell Level", group = "Basic Settings")
len = input.int(32, "Average Period", minval = 1, group = "Basic Settings")

colorbars = input.bool(false, "Color bars?", group = "UI Options")

chng = ta.change(src)
changn = _oma(chng, len, const, adapt)
changa = _oma(math.abs(chng), len, const, adapt)
rsi = (math.min(math.max(50.0 * (changn / math.max(changa, 0.0000001) + 1.0), 0), 100))

valc = 0.

valc := (rsi > buylev) ? 1 : (rsi < selllev) ? -1 : (rsi < buylev and rsi > selllev) ? 0 : nz(valc[1]) 
upH = (valc == 1) ? 1 : 0.
dnH = (valc == -1) ? 1 : 0.
nuH = (valc == 0) ? 1 : 0.

plot(nuH, "Mid", color = color.gray, style= plot.style_histogram)
plot(upH, "Up", color =  greencolor , style= plot.style_histogram)
plot(dnH, "Down", color = redcolor, style= plot.style_histogram)

colorout = upH == 1 ? greencolor : dnH == 1 ? redcolor : color.gray

barcolor(colorbars ? colorout : na)




