//@version=3
//@author=cI8DH
study("Accumulation/Distribution Volume (ADV) [cI8DH]", "ADV [cI8DH]", overlay=false, precision=0)
// inputs
height = input(100, "Height in % (useful if overlayed on the main chart)", minval=0, maxval=100, step=5)
base_enable = input(false, "Baseline chart")
aggregation = input("smoothed MA", "Aggregation", options=["disable", "simple MA", "smoothed MA", "cumulative"])
len = input(14, "Moving average length", minval=1) // EMA27 = SMMA/RMA14 ~ lunar month
price_enable = input(false, "Show money volume (price x volume)")
// calculations
ch = change(close)
AD_ratio = nz(ch/tr(true)) // 'True Range' fixes issues caused by gaps in price
base = if base_enable
sign(AD_ratio)
else
1
vol = if price_enable
volume*hlc3
else
volume
base_price_vol = base*vol
base_AD = base_price_vol*abs(AD_ratio)
// aggregation
ma(s) =>
if aggregation!="disable"
out = if aggregation=="simple MA"
sma(s,len)
else
rma(s,len)
else
na
MA_base_price_vol = if aggregation=="cumulative"
cum(sign(AD_ratio)*vol)
else
ma(base_price_vol)
MA_AD = if aggregation=="cumulative"
cum(AD_ratio*vol)
else
ma(base_AD)
// plot
plot(aggregation=="cumulative" ? na : 0, "Zero line", #88888888)
plot(aggregation=="cumulative" ? na : base_price_vol , "Volume" , ch>0?#65D25B44:ch<0?#FE6B6944:#888888ff, style=columns)
plot(aggregation=="cumulative" ? na : base_AD , "A/D Volume", ch>0?#92df8bff:ch<0?#fe9695ff:na, style=columns)
plot(MA_AD, "A/D moving average", #4477ffff)
plot(MA_base_price_vol, "Volume moving average", #ff9900ff)
plot(height==100 or aggregation=="cumulative" ? na : (100.0/height)*base_price_vol, "Dummy", na, editable=false)