Improved Chaikin Money Flow
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Morogan

//@version=5
indicator(title="Improved Chaikin Money Flow", shorttitle="ICMF", format=format.price, timeframe="D", timeframe_gaps=true, precision=2)

length = input.int(21, minval=1)

var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
h = open < low[1] and close < close[1] ? close[1] : high
l = open > high[1] and close > close[1] ? close[1] : low
ad = close==h and close==l or h==l ? 0 : ((2*close-l-h)/(h-l))*volume
mf = math.sum(ad, length) / math.sum(volume, length)
cmf = 100 * mf
plot(cmf, "CMF", color=(cmf > 0 ? color.green : color.red), style=plot.style_area)
hline(0, color=color.new(color.white, 25), title="Zero", linestyle=hline.style_solid)