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

//@version=5

library("ChaikinMoneyFlow")

export cmf(int length = 20, bool showma = true, int smalen = 5) =>
	clv = high == low ? 0 : (close - low - (high - close)) / (high - low)
    mfv = clv * volume
    
    //adLine = cum(mfv) --Chaikin's Accum/Distrib Line
    cmfLine = math.sum(mfv, length) / math.sum(volume, length)
    sma_1 = ta.sma(cmfLine, smalen)
    smaLine = showma ? sma_1 : na
    
    hiValue = math.max(cmfLine, 0)
    loValue = math.min(cmfLine, 0)
    
    [cmfLine, smaLine, hiValue, loValue]

[calc_cmfLine, calc_smaLine, calc_hiValue, calc_loValue] = cmf()

cmfPlot = plot(calc_cmfLine, title='CMF', color=color.new(color.green, 0), linewidth=2)
smaPlot = plot(calc_smaLine, title='SMA', color=color.new(color.black, 0), linewidth=1)
midPlot = plot(0, title='Mid Line', color=color.new(color.black, 0), linewidth=1)

hiPlot = plot(series=calc_hiValue, color=na)
fill(hiPlot, midPlot, color=color.new(color.green, 50))


loPlot = plot(series=calc_loValue, color=na)
fill(loPlot, midPlot, color=color.new(color.red, 50))