MACDV
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Options360
// 10/7/23 update: fine tune default input lengths, add a total line option, add a PVT or AD option, add a bullish or bearish tiny dot option.
// 10/14/23 update: add 6 volume source options: Accumulation Distribution, Price Volume Trend, Positive Volume Index, On Balance Volume, Money Flow Index, Average of all 5.
//@version=5
indicator("MACDV")
//
len2 = input.int(7, 'K length', minval=1, tooltip='Stochastic accumulation / distribution length')
len = input.int(3, 'D smoothing', minval=1, tooltip='Smoothing stochastic accumulation / distribution volume weighted moving average')
fa = input.int(6, 'MACDV fast', minval=1, tooltip='MACDV fast length line')
sl = input.int(13, 'MACDV slow', minval=1, tooltip='MACDV slow length line')
sig = input.int(4, 'MACDV signal', minval=1, tooltip='MACDV histogram length')
mult = input.int(2, 'stretch', minval=1, tooltip='Output multiplier for MACDV visual expansion')
total = input.int(1, 'total', minval=1, tooltip='Moving average of all 3 : MACDV fast + MACDV slow + MACDV histogram / 3')
//
normalize(src, min, max) =>
    var historicMin = 10e10
    var historicMax = -10e10
    historicMin := math.min(nz(src, historicMin), historicMin)
    historicMax := math.max(nz(src, historicMax), historicMax)
    min + (max - min) * (src - historicMin) / math.max(historicMax - historicMin, 10e-10)
//
var cumVol = 0.
cumVol += nz(volume)
ad = ta.cum(close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume)
ad2 = ta.sma(ta.stoch(ad, ad, ad, len2), len)
pvt = ta.pvt
pvt2 = ta.sma(ta.stoch(pvt, pvt, pvt, len2), len)
pvi = ta.pvi
pvi2 = ta.sma(ta.stoch(pvi, pvi, pvi, len2), len)
obv = ta.obv
obv2 = ta.sma(ta.stoch(obv, obv, obv, len2), len)
mfi = ta.mfi(ohlc4, len)
mfi2 = ta.sma(ta.stoch(mfi, mfi, mfi, len2), len)
all = (ad2 + pvt2 + pvi2 + obv2 + mfi2)/5
//
string v01 = "AD"
string v02 = "PVT"
string v03 = "PVI"
string v04 = "OBV"
string v05 = "MFI"
string v06 = "ALL"

string  GRP = "Settings"
string  vType = input.string(v02, "Volume Type", group = GRP, options = [v01, v02, v03, v04, v05, v06], tooltip='Accumulation Distribution, Price Volume Trend, Positive Volume Index, On Balance Volume, Money Flow Index, Average of all 5')

v(simple string type) => 
    float result = switch type
        v01 => ad2
        v02 => pvt2
        v03 => pvi2
        v04 => obv2
        v05 => mfi2
        v06 => all     
        => na
//
A = v(vType)
B = normalize(A, -100, 100)
C = B * mult
//
up = #3cfe12
up2 = #4e8642
down2 = #8c2121
down = #ff0202
//
[macdLine, signalLine, histLine] = ta.macd(C, fa, sl, sig)
plot(histLine * 2, 'Histogram', style=plot.style_area, color=(histLine>=0 ? (histLine[1] < histLine ? up : up2) : (histLine[1] < histLine ? down2 : down)))
plot(macdLine, 'MACD fast', linewidth=2, color=color.new(#008eff, 0))
plot(signalLine, 'MACD slow', linewidth=2, color=color.new(#ffffff, 0))
t = ta.sma((macdLine + signalLine + histLine)/3, total)
plot(t, 'total', linewidth=2, color=(t[1] < t ? up : down), display=display.none)
//
long = ta.crossover(macdLine, signalLine)
plotshape(long, title='long', style=shape.circle, location=location.bottom, color=(#3cfe12), size=size.tiny)
bull = ta.rising(histLine, 1)
plotchar(bull, title='bullish', char=".", location=location.bottom, color=(#3cfe12), size=size.tiny)
short = ta.crossunder(macdLine, signalLine)
plotshape(short, title='short', style=shape.circle, location=location.top, color=(#ff0202), size=size.tiny)
bear = ta.falling(histLine, 1)
plotchar(bear, title='bearish', char=".", location=location.top, color=(#ff0202), size=size.tiny)
//
h0 = hline(100, 'top', color=#989595, linestyle=hline.style_dotted)
h1 = hline(75, '75', color=#989595, linestyle=hline.style_dotted, display=display.none)
h2 = hline(50, '50', color=#989595, linestyle=hline.style_dotted)
h3 = hline(25, '25', color=#989595, linestyle=hline.style_dotted, display=display.none)
h4 = hline(0, '0', color=#989595, linestyle=hline.style_dashed)
h5 = hline(-25, '-25', color=#989595, linestyle=hline.style_dotted, display=display.none)
h6 = hline(-50, '-50', color=#989595, linestyle=hline.style_dotted)
h7 = hline(-75, '-75', color=#989595, linestyle=hline.style_dotted, display=display.none)
h8 = hline(-100, 'bottom', color=#989595, linestyle=hline.style_dotted)
