Enhanced WaveTrend Oscillator
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © QuantiLuxe

//@version=5
indicator("Enhanced WaveTrend Oscillator", "𝗘𝗻𝗵𝗮𝗻𝗰𝗲𝗱 𝗪𝗧", false)

f_ewave(src, chlen, avglen) =>
    basis = ta.ema(src, chlen)
    dev = ta.stdev(src, chlen)
    wave = (src - basis) / dev * 100

    ta.ema(wave, avglen)

src = input.source(hlc3, "Source")
chlen = input.int(10, "Channel Length")
avglen = input.int(21, "Average Length")
smooth = input.int(4, "Signal Length")
revshow = input.bool(true, "", inline = "0", group = "UI Options")
revt = input.int(100, "Reversion Threshold", options = [100, 125], inline = "0", group = "UI Options")
colbar = input.string("None", "Bar Coloring", ["None", "Midline Cross", "Extremities", "Reversions", "Slope"], group = "UI Options")
wt = f_ewave(src, chlen, avglen)
sig = ta.sma(wt, smooth)
hist = wt - sig

col_up = #5996ff
col_dn = #e864ff
h_col = hist > 0 ? (hist > hist[1] ? #d9a8ff9a : #b68bd49a) : (hist > hist[1] ? #ffc9739a : #d8aa609a)

hline(0, "Mid Line", #ffffff80, hline.style_solid)

max = hline(150, display = display.none)
hh = hline(125, display = display.none)
lh = hline(100, display = display.none)

fill(lh, hh, color = #ffc97325)
fill(hh, max, color = #ffc97336)

min = hline(-150, display = display.none)
ll = hline(-125, display = display.none)
hl = hline(-100, display = display.none)

fill(ll, hl, color = #d9a8ff21)
fill(ll, min, color = #d9a8ff34)

plot(hist, "𝗛", h_col, 1, plot.style_area)

w = plot(wt, "𝗪𝗧", wt > sig ? col_up : col_dn)
s = plot(sig, display = display.none)

fill(w, s, wt > sig ? #3179f579 : #aa47bc79)

plotchar(revshow ? wt > revt and wt < sig and not (wt[1] < sig[1]) ? wt + 40 : na : na, "OB", "⚬", location.absolute, col_dn, size = size.tiny)
plotchar(revshow ? wt < -revt and wt > sig and not (wt[1] > sig[1]) ? wt - 40 : na : na, "OS", "⚬", location.absolute, col_up, size = size.tiny)

color col = switch colbar
    "None" => na
    "Midline Cross" => wt > 0 ? col_up : col_dn
    "Extremities" => wt > revt ? col_up : wt < -revt ? col_dn : #b3b3b3c2
    "Reversions" => wt > revt and wt < wt[1] and not (wt[1] < wt[2]) ? col_dn : wt < -revt and wt > wt[1] and not (wt[1] > wt[2]) ? col_up : #b3b3b3c2
    "Slope" => wt > sig ? col_up : col_dn

barcolor(col)