On-Balance Accumulation Distribution (Volume-Weighted)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("On-Balance Accumulation Distribution (Volume-Weighted)", overlay=false)

length = input.int(40, minval=1, title="Length")
volumeFactor = input(1.0, title="Volume Factor")

// Calculate OBAD
obad = math.sum(volume * close * volumeFactor * ta.change(close), length)
obadSignal = ta.sma(obad, 9)

// Visualization Enhancements
obadColor = obad >= obadSignal ? color.lime : color.fuchsia
obadbgColor = obad >= obadSignal ? color.new(color.lime, 70) : color.new(color.fuchsia, 70)
obadsigColor = obadSignal > 0 ? color.green : color.maroon
barcolor(obadColor)
bgcolor(obadbgColor)

// Plotting
plot(obad, color=color.blue, linewidth=2, style = plot.style_histogram, title="OBAD Histogram")
plot(obad, color=color.blue, linewidth=2, title='OBAD Line')
plot(obadSignal, color=obadsigColor, linewidth=4, title="OBAD Signal")

// Signal Line Crossovers
obadCrossAbove = ta.crossover(obad, obadSignal)
obadCrossBelow = ta.crossunder(obad, obadSignal)

// Other Plotting Options
hline(0, "Zero Line", color=color.gray)