Enhanced McClellan Summation Index
// 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/
// © EliCobra

//@version=5
indicator("Enhanced McClellan Summation Index", "[Ʌ] - 𝐄𝐧𝐡𝐚𝐧𝐜𝐞𝐝 𝐌𝐒𝐈", false)

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

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 null(bar b) =>
    
    na(b) ? bar.new(0, 0, 0, 0) : b

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_rt(float a, float d, simple int m) =>

    (a - d) / (a + d) * m

method macd(float src, simple int flen, simple int slen) =>

    ta.ema(src, flen) - ta.ema(src, slen)

method mc(bar r) =>
    var bar mc = bar.new(        )
    mc        := bar.new(
         ta.cum(r.o.macd(19, 39)),
         ta.cum(r.h.macd(19, 39)),
         ta.cum(r.l.macd(19, 39)),
         ta.cum(r.c.macd(19, 39)))

    mc


var string gm = 'McClellan Summation Index', var string gs = 'MACD'
mode  = input.string('Heikin-Ashi', "Display Mode"    , ['Heikin-Ashi', 'MACD'  , 'MACD-H'], group = gm)
idx   = input.string('NYSE'       , "Index"           , ['NYSE'       , 'NASDAQ', 'AMEX'  ], group = gm)
mlt   = input.int   (1000         , "Ratio Multiplier", [1000         , 100     , 10   , 1], group = gm)
revs  = input.bool  (true         , "Reversal Doritos",                                      group = gm)
len_f = input       (12           , "MACD  Fast"      ,                                      group = gs)
len_s = input       (26           , "MACD  Slow"      ,                                      group = gs)
len_m = input.int   (9            , "MACD  Signal"    , 1, 50                              , group = gs)


[s_adv, s_dec] = switch idx
    'NYSE'   => ['ADV' , 'DECL' ]
    'NASDAQ' => ['ADVQ', 'DECLQ']
    'AMEX'   => ['ADVA', 'DECLA']

bar adv = request.security('USI:' + s_adv, '', bar.new()).null()
bar dec = request.security('USI:' + s_dec, '', bar.new()).null()
bar mc  = bar.new(
     f_rt(adv.o, dec.o, mlt),
     f_rt(adv.h, dec.h, mlt),
     f_rt(adv.l, dec.l, mlt),
     f_rt(adv.c, dec.c, mlt)).mc().ha()

float macd = mc.c.macd(len_f, len_s)
float sig  = ta  .ema (macd , len_m)
float hist = macd - sig
float pos  = hist > 0 ? 0 : hist
float neg  = hist < 0 ? 0 : hist


var color colnt = chart.bg_color
var color colup = chart.fg_color
var color coldn = #d61717

bool dcon = ta.crossunder(mc.c, mc.o)
bool ucon = ta.crossover (mc.c, mc.o)

color hawcol = switch
    mc.c > mc.o => colup
    mc.c < mc.o => coldn

color hafcol = switch
    ucon        => colup
    dcon        => colnt
    mc.c < mc.o => coldn
    =>             colnt

color habcol = switch
    dcon        => coldn
    ucon        => colup
    mc.c > mc.o => colup
    mc.c < mc.o => coldn


plotcandle(mode == 'Heikin-Ashi' ? mc.o : na, 
           mode == 'Heikin-Ashi' ? mc.h : na, 
           mode == 'Heikin-Ashi' ? mc.l : na, 
           mode == 'Heikin-Ashi' ? mc.c : na, 
           "𝐌𝐒𝐈", hafcol, hawcol, bordercolor = habcol)

plotchar(revs and mode == 'Heikin-Ashi' ? ucon ? mc.o - mlt / 10 : na : na, "Reverse Dip", "▴", location.absolute, colup, size = size.tiny)
plotchar(revs and mode == 'Heikin-Ashi' ? dcon ? mc.c + mlt / 10 : na : na, "Reverse Top", "▾", location.absolute, coldn, size = size.tiny)

plot(mode == 'MACD-H' ? hist       : na, "MACD-H", hist > 0 ? color.new(colup, 90) : color.new(coldn, 90), 1, plot.style_columns                                          )
plot(mode == 'MACD-H' ? hist * 0.7 : na, "Filler", hist > 0 ? color.new(colup, 70) : color.new(coldn, 70), 1, plot.style_columns, display = display.pane, editable = false)
plot(mode == 'MACD-H' ? hist * 0.5 : na, "Filler", hist > 0 ? color.new(colup, 60) : color.new(coldn, 60), 1, plot.style_columns, display = display.pane, editable = false)
plot(mode == 'MACD-H' ? hist * 0.3 : na, "Filler", hist > 0 ? color.new(colup, 30) : color.new(coldn, 30), 1, plot.style_columns, display = display.pane, editable = false)
plot(mode == 'MACD-H' ? hist * 0.1 : na, "Filler", hist > 0 ? color.new(colup, 0 ) : color.new(coldn, 0 ), 1, plot.style_columns, display = display.pane, editable = false)
plot(mode == 'MACD-H' ? neg        : na, "Filler", color.new(colup, neg == 0 and neg[1] == 0 ? 100 : 0)  , 1, plot.style_line   , display = display.pane, editable = false)
plot(mode == 'MACD-H' ? pos        : na, "Filler", color.new(coldn, pos == 0 and pos[1] == 0 ? 100 : 0)  , 1, plot.style_line   , display = display.pane, editable = false)
plot(mode == 'MACD'   ? macd       : na, "𝐌𝐀𝐂𝐃" , colup                                                 , 1, plot.style_line                                             )
plot(mode == 'MACD'   ? sig        : na, "𝐒𝐢𝐠𝐧𝐚𝐥" , coldn                                                 , 1, plot.style_line                                             )

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