Open Close Trend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262
 
//@version=5
indicator("Open Close Trend", overlay = false)

ema(src, len) => //{
    alpha = 2 / (len + 1)
    sum   = 0.0
    sum  := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])
//}
//Tripple EMA
tma(src, int len) => //{
    //src is an input.source
    //len is an input.int
    ema1 = ema(src, len)
    ema2 = ema(ema1, len)
    ema3 = ema(ema2, len)
    tma  = 3 * (ema1 - ema2) + ema3

normalize(src, float len = 200, bool ena = false) =>
    float a = 0
    float b = 1
    min = ta.lowest(src, int(len))
    max = ta.highest(src, int(len))

    float x = ((a + (src - min) * (b - a) / (max - min))- 0.5) * 2
    ena ? x : src * len

norm(src, min, max, bool pass = true) => 
    num  = src - min
    den  = max - min
    norm = num / den
    out  = pass == true ? (norm - 0.5) * 2 : src

ma_type          = input.string("EMA", "MA Select", ["SMA","EMA","TMA","WMA"])
select           = input.string("Gradient", "Feature Select", ["Gradient","Histogram","BG Color"])
type             = input.string("Normalize", "Style", ["Normalize","Moving Average"])
length           = input.int(20, "Length", 1)
signal_smoothing = input.int(1, "Signal Smoothing", 1)
colour_smoothing = input.int(2, "Divergence Convergence Smoothing", 1)
lookback         = input.float(50, "DC Normalization Lookback / Scale", 1, inline = "Back", tooltip = "When enabled the hist will be normalized. When dissabled this will double as a scaler for the hist.")
enable           = input.bool(true, "", inline = "Back")
moving_average   = input.int(200, "Moving Average Length")

grad_col_dwn   = input.color(color.new(color.red, 90), "Gradient Color", inline = "grad")
grad_col_up    = input.color(color.new(color.blue, 90), "", inline = "grad")

col_grow_below = input.color(color.new(#FFCDD2, 50), "Hist Low Color", inline = "low")
col_fall_below = input.color(color.new(#FF5252, 50), "", inline = "low")
col_fall_above = input.color(color.new(#B2DFDB, 50), "Hist High Color", inline = "high")
col_grow_above = input.color(color.new(#26A69A, 50), "", inline = "high")

bg_colour = input.color(color.new(color.blue, 90), "Background Color")

ma(src, length)=>
    switch ma_type
        "SMA" => ta.sma(src, length)
        "EMA" => ema(src, length)
        "TMA" => tma(src, length)
        "WMA" => ta.wma(src, length)

High  = ta.sma(ma(high, length) - (type == "Normalize" ? 0 : ta.sma(close, moving_average)), signal_smoothing)
Low   = ta.sma(ma(low, length) - (type == "Normalize" ? 0 : ta.sma(close, moving_average)), signal_smoothing)
Close = ta.sma(ma(close, length) - (type == "Normalize" ? 0 : ta.sma(close, moving_average)), signal_smoothing)
Open  = ta.sma(ma(open, length) - (type == "Normalize" ? 0 : ta.sma(close, moving_average)), signal_smoothing)

hist   = 
 select == "BG Color" ? na : 
 select == "Gradient" ? ta.sma(normalize(Close - Open, lookback, true), colour_smoothing) : 
 type == "Normalize" ? ta.sma(normalize(Close - Open, lookback, enable), colour_smoothing) : 
 ta.sma(Close - Open, colour_smoothing)

colour = color.from_gradient(hist, -1, 1, grad_col_dwn, grad_col_up)
hist_col = hist >= 0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)

h = plot(norm(High, Low, High, type == "Normalize" ? true : false), color = color.gray)
l = plot(norm(Low, Low, High, type == "Normalize" ? true : false), color = color.gray)

plot(select == "Histogram" ? hist : na, "Open Close Hist", hist_col, style = plot.style_columns)

fill(h, l, color = select == "Gradient" ? colour : color.new(color.blue, 90))
//bgcolor(type == "Normalize" ? na : select == "Gradient" ? colour : na)

c = plot(norm(Close, Low, High, type == "Normalize" ? true : false), color = color.green)
o = plot(norm(Open, Low, High, type == "Normalize" ? true : false), color = color.red)

plotshape(ta.crossover(Close, Open), "Green", color = color.green, location = location.top)
plotshape(ta.crossunder(Close, Open), "Red", color = color.red, location = location.bottom)

plot(0, color = color.gray)