Indicator · Pine Script v5 · Own pane

Chaikin Money Flow

by sbtnc

Calculates Chaikin Money Flow, which measures buying and selling pressure from where each close sits in its range, weighted by volume over a set length.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code125
Settings7inputs
LicenceMPL-2.0
Last update9 months ago28 Jan 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

125 lines of Pine Script v5 from Chaikin Money Flow.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Chaikin Money Flow.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sbtnc
// Created: 2019-01-05
// Last modified: 2023-04-28
// Version 2.0

// @version=5
indicator("Chaikin Money Flow", "CMF", precision=2, timeframe="", timeframe_gaps=true)


//--------------------------------------------------------------------
//                             Constants
//--------------------------------------------------------------------

DEFAULT_MF_COLOR            = color.new(#656565, 20)
DEFAULT_BG_COLOR_NEGATIVE   = #ff4e3e
DEFAULT_BG_COLOR_POSITIVE   = #3fbe53
DEFAULT_ZERO_COLOR          = color.new(#656565, 90)


//--------------------------------------------------------------------
//                               Inputs
//--------------------------------------------------------------------

// Groups

g_indicator = "Money Flow"
g_style     = "Style"


// Tooltips

t_gradientLength    = "The number of bars used for the dynamic gradient coloring." +
                         " The coloring is relative to the maximum and minimum values found over the period.\n\n" +
                         "Use a short period to emphasize the current market conditions or a long period to highlight past extreme values."


// Inputs

i_length            = input.int (20,                        "Length",       1,                                                              group=g_indicator)
i_positiveCmfColor  = input     (DEFAULT_MF_COLOR,          "Money Flow",                                           inline="Colors",        group=g_style)
i_negativeCmfColor  = input     (DEFAULT_MF_COLOR,          "",                                                     inline="Colors",        group=g_style)
i_dynamic           = input     (false,                     "Dynamic gradient",                                     inline="Dynamic",       group=g_style)
i_gradientLength    = input.int (100,                       "",             minval=1,   tooltip=t_gradientLength,   inline="Dynamic",       group=g_style)
i_positiveBgColor   = input     (DEFAULT_BG_COLOR_POSITIVE, "Background",                                           inline="Background",    group=g_style)
i_negativeBgColor   = input     (DEFAULT_BG_COLOR_NEGATIVE, "",                                                     inline="Background",    group=g_style)


//--------------------------------------------------------------------
//                              Functions 
//--------------------------------------------------------------------

// @function    Get the conditional positive/negative color
// @return      color
f_getColor(float _val, color _positive, color _negative) =>
    _val >= 0 ? _positive : _negative


// @function    Get the conditional positive/negative gradient color between a specific range
// @return      color
f_getColor(float _val, float _range, color _positive, color _negative) =>
    color.from_gradient(_val, -_range, _range, _negative, _positive)


// @function    Return the CMF line color per user config
// @return      color
g_getCmfColor(float _val, float _extremVal) =>
    switch i_dynamic
        false   => f_getColor(_val, i_positiveCmfColor, i_negativeCmfColor)
        true    => f_getColor(_val, _extremVal, i_positiveCmfColor, i_negativeCmfColor)


//--------------------------------------------------------------------
//                        Variables declarations
//--------------------------------------------------------------------

var cumulativeVolume = 0.0


//--------------------------------------------------------------------
//                      Logic (orignal built-in CMF)
//--------------------------------------------------------------------

cumulativeVolume += nz(volume)


// Notify the user when the indicator can't be used

if barstate.islast and cumulativeVolume == 0
    runtime.error("No volume is provided by the data vendor.")


// Calculate the Money Flow

ad = close == high and close == low or high == low ? 0 : ((2 * close - low - high) / (high - low)) * volume
mf = math.sum(ad, i_length) / math.sum(volume, i_length)


//--------------------------------------------------------------------
//                          Plotting & styling
//--------------------------------------------------------------------

// Base the dynamic gradient on the values found over the selected period

highest = ta.highest(mf, i_gradientLength)
lowest  = ta.lowest(mf, i_gradientLength)
extreme = math.max(highest, math.abs(lowest))
c_cmf   = g_getCmfColor(mf, extreme)


// Plot the CMF

cmf = plot(mf, "MF", c_cmf)


// Plot the zero line

hline(0, "Zero", DEFAULT_ZERO_COLOR)


// Fill with gradient backgrounds

zero = plot(0, "Zero", editable=false, display=display.none)
fill(zero, cmf, 1, -0.01, i_positiveBgColor, color.new(i_positiveBgColor, 100))
fill(zero, cmf, 0.01, -1, color.new(i_negativeBgColor, 100), i_negativeBgColor)

Overview

The default is twenty bars. It plots in a separate pane around a zero line, with colour by sign and an optional dynamic gradient relative to recent extremes. Positive and negative background colours are adjustable. It needs volume data.

Details

Published by

sbtnc on GitHub

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Volume, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pine-scriptpinescriptpinescript-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Lengthint20i_length
Money FlowinputDEFAULT_MF_COLORi_positiveCmfColor
i_negativeCmfColorinputDEFAULT_MF_COLORi_negativeCmfColor
Dynamic gradientboolfalsei_dynamic
i_gradientLengthint100i_gradientLength
BackgroundinputDEFAULT_BG_COLOR_POSITIVEi_positiveBgColor
i_negativeBgColorinputDEFAULT_BG_COLOR_NEGATIVEi_negativeBgColor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Money Flow Index

by everget
948 stars

Calculates the standard Money Flow Index (MFI) oscillator alongside an exponential moving average signal line in a separate pane. The script visually highlights trend conditions through dynamic ribbon filling, optional difference histogram bars, and overbought

Indicator v4 Oscillators & momentumVolume
Updated 11 days ago · GPL-3.0 GitHub repository of Money Flow Index

Commitment Gauge

by harryguiacorn
219 stars

This indicator measures directional momentum by evaluating candlestick body proportions and volume over a user-defined lookback window. It computes the ratio of candle body to total range, weights the cumulative value by volume and absolute body size, and dete

Indicator v4 VolumeCandlestick patterns
Updated 1 year ago · None stated GitHub repository of Commitment Gauge

Aggregate Signal

by capissimo
114 stars

This indicator builds an aggregate oscillator in a separate pane. It scales open, high, low, close, and volume into a 0 to 1 range, then averages scaled readings from MACD, a MACD histogram, RSI, stochastic, CCI, momentum, OBV, a directional index, volume-weig

Indicator v4 Oscillators & momentumVolume Repaints
Updated 1 year ago · None stated GitHub repository of Aggregate Signal

This indicator calculates the Bill Williams Market Facilitation Index as the high-low range divided by volume, or zero when volume is zero. It compares the index and volume with the previous bar and assigns one of four zones: green for both rising, fade for bo

Indicator v6 VolumeOscillators & momentum
Updated 4 weeks ago · Apache-2.0 GitHub repository of Bill Williams Market Facilitation Index

PPO ATR Bands

by cryptorife
75 stars

PPO ATR Bands plots a percentage price oscillator in a separate pane. It compares a Hull average of typical price with a volume-weighted exponential average of Parabolic SAR, then scales three true-range bands around the slow average into matching oscillator l

Indicator v4 Oscillators & momentumMoving averages
Updated 3 years ago · GPL-3.0 GitHub repository of PPO ATR Bands

Chaikin Oscillator

by be-thomas
65 stars

Calculates the Chaikin Oscillator by taking the difference between a fast exponential moving average and a slow exponential moving average of the Accumulation/Distribution line. It plots the resulting line in a separate pane alongside a dashed zero reference l

Indicator v4 Oscillators & momentumVolume
Updated 5 days ago · GPL-3.0 GitHub repository of Chaikin Oscillator

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers