Pine has ta.cmf built in, and the from-scratch version is short enough to be worth
showing — it makes the money flow multiplier explicit. See
the explainer for the concept.
What you’ll need
- A TradingView account (free tier is fine)
- The Pine Editor
The source
//@version=5
indicator("AlgoGen CMF", shorttitle="AG CMF", overlay=false)
length = input.int(20, "Length", minval=1)
cmfVal = ta.cmf(length)
col = cmfVal >= 0 ? #0ca30c : #d03b3b
plot(cmfVal, "CMF", color=col, style=plot.style_columns)
hline(0, "Zero", color=color.gray)
hline(0.05, "Accum", color=color.new(color.gray, 60))
hline(-0.05, "Distrib", color=color.new(color.gray, 60))
ta.cmf(length) computes the standard Chaikin Money Flow; colouring the histogram
by sign (green above zero, red below) makes accumulation vs distribution obvious —
matching the output chart. The ±0.05 lines mark the usual “meaningful pressure”
thresholds.
From scratch
//@version=5
indicator("AlgoGen CMF (manual)", overlay=false)
length = input.int(20, "Length")
mfm = (high == low) ? 0.0 : ((close - low) - (high - close)) / (high - low)
mfv = mfm * volume
cmfManual = math.sum(mfv, length) / math.sum(volume, length)
plot(cmfManual, color=#2a78d6, linewidth=2)
math.sum(x, length) is the rolling window sum; the high == low guard handles flat
bars. This reproduces ta.cmf exactly.
Using CMF as a confirmation filter
CMF shines as a filter on price signals rather than a trigger on its own. Tint the background by the money-flow regime so you only take longs while buyers dominate:
accum = cmfVal > 0.05
distrib = cmfVal < -0.05
bgcolor(accum ? color.new(#0ca30c, 92) : distrib ? color.new(#d03b3b, 92) : na)
alertcondition(ta.crossover(cmfVal, 0), "CMF turned positive", "Money flow turned to accumulation")
alertcondition(ta.crossunder(cmfVal, 0), "CMF turned negative", "Money flow turned to distribution")
Now the pane greens up during accumulation and reds during distribution — a clean visual gate for “only go long when money flow agrees.”
Gotchas
- Flat-bar guard.
high == lowwould divide by zero; return 0 for those bars. math.sum, notta.sma. CMF divides the sum of money flow volume by the sum of volume — usemath.sum, and divide after summing.- Volume required. No reliable volume, no reliable CMF; on symbols without real volume the reading is meaningless.
Same indicator elsewhere: Python, MQL5, EasyLanguage, NinjaScript. Then test a CMF filter in AlgoGen.
This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.
Sources and further reading
- Pine Script built-ins (TradingView)
