Pine Script Chaikin Money Flow

Master this powerful volume-based indicator in TradingView's Pine Script that quantifies the strength of accumulation and distribution to confirm trends and anticipate price movements.

Last Updated on: Expertise: Intermediate

What is Chaikin Money Flow?

Chaikin Money Flow (CMF) is a volume-based oscillator developed by Marc Chaikin, building upon his Accumulation/Distribution Line. CMF measures the amount of money flow over a specific lookback period (typically 20 or 21 periods). It essentially quantifies the degree of buying or selling pressure, relative to the volume, across a defined period. CMF ranges between +1.0 and -1.0.

The core concept behind CMF is that if a security closes in the upper half of its daily range, it's considered to be under accumulation (buying pressure). If it closes in the lower half, it's considered under distribution (selling pressure). CMF then integrates this with volume over a moving average period.

CMF is primarily used to:

In Pine Script, CMF is a versatile tool for gauging market conviction by observing volume-weighted money flow.

Components and Calculation

The calculation of Chaikin Money Flow involves these steps:

  1. Money Flow Multiplier (MFM): For each period, this value determines the extent of buying or selling pressure. It ranges from -1 to +1.
    `MFM = ((Close - Low) - (High - Close)) / (High - Low)`
    * If `High - Low` is zero (a flat bar), MFM is typically 0.
  2. Money Flow Volume (MFV): Multiply the MFM by the period's volume.
    `MFV = MFM * Volume`
  3. Chaikin Money Flow (CMF): Sum the MFV over the `length` periods and divide it by the sum of volume over the same `length` periods.
    `CMF = Sum(MFV, length) / Sum(Volume, length)`

A common `length` for CMF is 20 or 21 periods.

Basic Chaikin Money Flow (CMF) Implementation in Pine Script

Pine Script v5 provides a convenient built-in function `ta.cmf()` for calculating Chaikin Money Flow.

//@version=5
indicator("My Chaikin Money Flow (CMF)", overlay=false, format=format.percent) // overlay=false to plot in a separate pane

// Input for CMF length
length = input.int(20, title="CMF Length", minval=1)

// Calculate CMF using the built-in function
// ta.cmf() implicitly uses 'high', 'low', 'close', and 'volume' for its calculation
cmfValue = ta.cmf(length)

// Plot the CMF line
plot(cmfValue, title="CMF", color=color.new(color.blue, 0), linewidth=2)

// Plot the Zero Line as a key reference point
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted)

// Optional: Add reference levels for visual clarity, e.g., +0.20 and -0.20
// CMF values above +0.20 often indicate strong buying pressure.
// CMF values below -0.20 often indicate strong selling pressure.
hline(0.20, "Upper Ref (+0.20)", color=color.new(color.green, 0), linestyle=hline.style_dashed)
hline(-0.20, "Lower Ref (-0.20)", color=color.new(color.red, 0), linestyle=hline.style_dashed)

Money Flow Direction: CMF tells you if money is generally flowing *into* (accumulation) or *out of* (distribution) an asset over a period. Values above 0 suggest accumulation, below 0 suggest distribution.

Practical CMF Trading Strategies

1. Zero-Line Crossovers (Trend Confirmation)

Crossovers of the zero line can provide signals about the underlying trend direction and strength based on money flow. These signals are best used to confirm price trends rather than as standalone entry signals.

//@version=5
strategy("CMF Zero Line Crossover Strategy", overlay=true)

length = input.int(20, title="CMF Length", minval=1)
cmfValue = ta.cmf(length)

plot(cmfValue, "CMF", color.blue, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

// Long entry: CMF crosses above zero
longCondition = ta.crossover(cmfValue, 0)

// Short entry: CMF crosses below zero
shortCondition = ta.crossunder(cmfValue, 0)

if (longCondition)
    strategy.entry("Long CMF", strategy.long)

if (shortCondition)
    strategy.entry("Short CMF", strategy.short)

// Basic exit: opposite signal
strategy.close("Long CMF", when=shortCondition)
strategy.close("Short CMF", when=longCondition)

2. CMF Divergence (Key Reversal Signal)

Divergence between price and CMF is often considered the most powerful signal from the indicator, suggesting a weakening trend and potential reversal, as money flow is not confirming price action.

//@version=5
strategy("CMF Divergence Strategy", overlay=true)

length = input.int(20, title="CMF Length", minval=1)
cmfValue = ta.cmf(length)

plot(cmfValue, "CMF", color.blue, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)

// Simple divergence detection (conceptual, robust detection requires advanced pivot logic)
// This example looks for recent higher/lower swings in price and CMF.

// Bullish Divergence: Price lower low, CMF higher low
bullishDiv = close < close[1] and close[1] < close[2] and cmfValue > cmfValue[1] and cmfValue[1] > cmfValue[2]

// Bearish Divergence: Price higher high, CMF lower high
bearishDiv = close > close[1] and close[1] > close[2] and cmfValue < cmfValue[1] and cmfValue[1] < cmfValue[2]

// Plot shapes on the chart to indicate divergence
plotshape(bullishDiv, title="Bullish CMF Divergence", location=location.belowbar, color=color.new(color.green, 0), style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish CMF Divergence", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small)

if (bullishDiv)
    strategy.entry("Long Divergence", strategy.long)
if (bearishDiv)
    strategy.entry("Short Divergence", strategy.short)

// Basic exit after a few bars or on opposite signal
strategy.exit("Long Divergence Exit", from_entry="Long Divergence", profit=close*0.02, loss=close*0.01)
strategy.exit("Short Divergence Exit", from_entry="Short Divergence", profit=close*0.02, loss=close*0.01)

3. CMF Extreme Readings (Overbought/Oversold Volatility)

While CMF does not have traditional overbought/oversold levels like RSI, sustained readings near its extremes (+0.50 or -0.50, though these are arbitrary and depend on the asset) can indicate very strong buying or selling pressure that might be unsustainable in the long run.

//@version=5
indicator("CMF Extreme Readings", overlay=true)

length = input.int(20, title="CMF Length", minval=1)
cmfValue = ta.cmf(length)

plot(cmfValue, "CMF", color.blue, display=display.pane_only)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dotted, display=display.pane_only)
hline(0.30, "Strong Accumulation", color.new(color.green, 0), linestyle=hline.style_dashed, display=display.pane_only)
hline(-0.30, "Strong Distribution", color.new(color.red, 0), linestyle=hline.style_dashed, display=display.pane_only)

// Highlight background based on CMF strength
bgcolor(cmfValue > 0.30 ? color.new(color.lime, 90) : na, title="Accumulation Zone")
bgcolor(cmfValue < -0.30 ? color.new(color.maroon, 90) : na, title="Distribution Zone")

// Alert conditions for extreme readings
alertcondition(cmfValue > 0.30 and cmfValue[1] <= 0.30, "CMF Strong Accumulation", "CMF moved into strong accumulation zone.")
alertcondition(cmfValue < -0.30 and cmfValue[1] >= -0.30, "CMF Strong Distribution", "CMF moved into strong distribution zone.")

Optimizing Chaikin Money Flow Performance

To get the most from Chaikin Money Flow in Pine Script:

Institutional Footprint: CMF helps track where the "smart money" (large institutional orders) might be accumulating or distributing shares, providing insights into their likely future intentions.

Common CMF Pitfalls

Conclusion

Chaikin Money Flow (CMF) is a sophisticated and highly insightful volume-based technical indicator available in Pine Script for TradingView. By quantifying the balance of buying (accumulation) and selling (distribution) pressure over time, it provides traders with a deeper understanding of market conviction and potential future price movements. While its greatest strength lies in identifying powerful divergences with price, it also excels at confirming existing trends and validating shifts in market sentiment. By understanding its calculation, thoughtfully tuning its parameters, and integrating it strategically with price action and other technical analysis tools, you can leverage CMF to enhance your trading decisions and gain a clearer understanding of the underlying money flow in any market.