OBV-MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rewbuglag

//@version=5
indicator(title="OBV-MACD", shorttitle="OBV-MACD", timeframe="", timeframe_gaps=true, format=format.volume) // , format=format.volume
// indicator(title="OBV-MACD", shorttitle="OBV-MACD", timeframe="", timeframe_gaps=true, format=format.percent) // , format=format.volume

// OBV-MACD
i_obv_macd_timeframe = input.timeframe("", "Timeframe", group="OBV-MACD")
i_obv_mode = input.string("Default", "OBV Mode", options=["Default", "Price Chg."], group="OBV-MACD")
i_obv_macd_ma_type = input.string("EMA", "Method", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="OBV-MACD")
i_obv_macd_fast_ma_length = input.int(12, "Fast Length", minval=1, group="OBV-MACD")
i_obv_macd_slow_ma_length = input.int(26, "Slow Length", minval=1, group="OBV-MACD")
i_obv_macd_signal_ma_length = input.int(9, "Signal Length", minval=1, group="OBV-MACD")

// OBV-MACD-Donchian
i_obv_macd_high_breakout_length = input.int(20, "MACD High Breakout Length", minval=1, group="OBV-MACD Donchian")
i_obv_macd_low_breakout_length = input.int(10, "MACD Low Breakout Length", minval=1, group="OBV-MACD Donchian")

// Calculation
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

var cumVol = 0.
cumVol += math.abs(nz(volume))
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
obv = (i_obv_mode == "Default" ? ta.cum(math.sign(ta.change(close)) * math.abs(volume)) : ta.cum(ta.change(close) * math.abs(volume)))
obv_macd_fast_ma = ma(obv, i_obv_macd_fast_ma_length, i_obv_macd_ma_type)
obv_macd_slow_ma = ma(obv, i_obv_macd_slow_ma_length, i_obv_macd_ma_type)
obv_macd = obv_macd_fast_ma - obv_macd_slow_ma
obv_macd_signal_ma = ma(obv_macd, i_obv_macd_signal_ma_length, i_obv_macd_ma_type)
obv_macd_signal_hist = obv_macd - obv_macd_signal_ma

obv_macd_high = request.security(syminfo.tickerid, i_obv_macd_timeframe, ta.highest(obv_macd, i_obv_macd_high_breakout_length), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
obv_macd_low = request.security(syminfo.tickerid, i_obv_macd_timeframe, ta.lowest(obv_macd, i_obv_macd_low_breakout_length), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

var obv_macd_ath = 0.0
obv_macd_ath := obv_macd > obv_macd_ath ? obv_macd : obv_macd_ath
var obv_macd_atl = 0.0
obv_macd_atl := obv_macd < obv_macd_atl ? obv_macd : obv_macd_atl

obv_macd_breakout_high = ta.crossover(obv_macd, obv_macd_high[1])
obv_macd_breakout_low = ta.crossunder(obv_macd, obv_macd_low[1])

// Visualization
// Plot colors
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")

// plot(obv_chg, "OBV Change", obv_chg >= 0 ? color.green : color.red)

hline(0, "Zero Line", color=color.new(#787B86, 50), display=display.none)
plot(obv_macd_signal_hist, "Signal Line Histogram", (obv_macd_signal_hist>=0 ? (obv_macd_signal_hist[1] < obv_macd_signal_hist ? col_grow_above : col_fall_above) : (obv_macd_signal_hist[1] < obv_macd_signal_hist ? col_grow_below : col_fall_below)), style=plot.style_columns)
plot(obv_macd, "OBV-MACD Line", obv_macd >= 0 ? color.lime : color.red, 2)
plot(obv_macd_signal_ma, "OBV-MACD Signal Line", color.orange)
plot(obv_macd, "Zero Line Histogram", (obv_macd>=0 ? (obv_macd[1] < obv_macd ? col_grow_above : col_fall_above) : (obv_macd[1] < obv_macd ? col_grow_below : col_fall_below)), style=plot.style_columns, display=display.none)
// plot(obv, "OBV Line", obv_macd >= 0 ? color.lime : color.red, 2, display=display.none)
plot(obv, "OBV Line", (obv_macd>=0 ? (obv_macd_signal_hist >= 0 ? col_grow_above : col_fall_above) : (obv_macd_signal_hist >= 0 ? col_grow_below : col_fall_below)), 2, display=display.none)
plot(obv_macd_fast_ma, "OBV Fast MA", color.aqua, display=display.none)
plot(obv_macd_slow_ma, "OBV Slow MA", color.orange, display=display.none)
plot(obv_macd_high, title="OBV-MACD High", color=color.green, display=display.none)
plot(obv_macd_low, title="OBV-MACD Low", color=color.red, display=display.none)
plot(obv_macd_ath, title="OBV-MACD ATH", color=color.green, display=display.none)
plot(obv_macd_atl, title="OBV-MACD ATL", color=color.red, display=display.none)
plotshape(obv_macd_breakout_high or obv_macd_breakout_low, "OBV-MACD High/Low Breakout", shape.cross, location.bottom, (obv_macd_breakout_high ? color.teal : color.red), size=size.tiny)
plotshape(obv_macd_signal_hist, "Histogram Slope Up/Down", shape.square, location.top, obv_macd_signal_hist >= 0 ? (obv_macd_signal_hist > obv_macd_signal_hist[1] ? #26A69A : #B2DFDB) : (obv_macd_signal_hist < obv_macd_signal_hist[1] ? #FF5252 : #FFCDD2), display=display.none)

