On Balance Volume Heikin-Ashi Transformed
// 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("On Balance Volume Heikin-Ashi", "[Ʌ] - 𝘖𝘉𝘝 𝘏-𝘈", true, scale = scale.none)

type bar
    float o = open
    float h = high
    float l = low
    float c = close
    float v = volume

method src(bar b, simple string src) =>
    float x = switch src
        'oc2'   => math.avg(b.o, b.c          )
        'hl2'   => math.avg(b.h, b.l          )
        'hlc3'  => math.avg(b.h, b.l, b.c     )
        'ohlc4' => math.avg(b.o, b.h, b.l, b.c)
        'hlcc4' => math.avg(b.h, b.l, b.c, b.c)

    x

method ha(bar b, simple bool p = true) =>
    var bar x = bar.new(       )
    x.c      := b  .src('ohlc4')
    x        := bar.new(
         na(x.o[1]) ? 
             b.src('oc2') : nz(x.src('oc2')[1]),
         math.max(b.h, math.max(x.o, x.c))     ,
         math.min(b.l, math.min(x.o, x.c))     ,
         x.c                                   )

    p ? x : b

f_obv(float src) =>
    bar   b   = bar .new (         )
    float dir = math.sign(b.c - b.o)

    ta.cum(dir ==  1   ? 
           + b.v * src : 
           dir == -1   ? 
           - b.v * src : 
           0           )


var string go = "OBV", var string ge = "EMAs"
norm = input.bool(false, "Normalize  |", inline = '3', group = go)
len  = input.int (21   , "Period"      , inline = '3', group = go)
ma1  = input.bool(true , "EMA  |"      , inline = '1', group = ge)
len1 = input.int (20   , "Length"      , inline = '1', group = ge)
ma2  = input.bool(false, "EMA  |"      , inline = '2', group = ge)
len2 = input.int (50   , "Length"      , inline = '2', group = ge)


float calc = norm ?
     f_obv(ohlc4) - ta.ema(f_obv(ohlc4), len) :
     f_obv(ohlc4)

bar   obv  = bar.new(
     calc[1]                ,
     math.max(calc, calc[1]),
     math.min(calc, calc[1]),
     calc                   ).ha()


var color colup   = #fff2cc
var color coldn   = #6fa8dc
var color colema1 = #FFD6E8
var color colema2 = #9a9adf

color haColor = switch
    obv.c > obv.o => colup
    obv.c < obv.o => coldn

plotcandle(obv.o, obv.h, obv.l, obv.c, 
     " 𝘖𝘉𝘝", haColor, haColor, bordercolor = haColor)

hline(norm ? 0 : na                 , "Zero Line", #ffffff80, hline.style_solid)
plot (ma1 ? ta.ema(obv.c, len1) : na, " 𝘌𝘔𝘈 1"  , colema1)
plot (ma2 ? ta.ema(obv.c, len2) : na, " 𝘌𝘔𝘈 2"  , colema2)

//Source Construction For Indicator\Strategy Exports
plot(obv.o           , "open" , editable = false, display = display.none)
plot(obv.h           , "high" , editable = false, display = display.none)
plot(obv.l           , "low"  , editable = false, display = display.none)
plot(obv.c           , "close", editable = false, display = display.none)
plot(obv.src('hl2'  ), "hl2"  , editable = false, display = display.none)
plot(obv.src('hlc3' ), "hlc3" , editable = false, display = display.none)
plot(obv.src('ohlc4'), "ohlc4", editable = false, display = display.none)
plot(obv.src('hlcc4'), "hlcc4", editable = false, display = display.none)