Indicator · Pine Script v5 · Own pane

Adaptive Money Flow Index

by palitojendthen

The Adaptive Money Flow Index indicator dynamically adjusts its lookback length using John Ehlers dominant cycle measurement technique.

View on GitHub

GitHub stars20on the repository
Pine versionv5runs as is
Lines of code183
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago12 Jul 2025

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

183 lines of Pine Script v5 from indicator/adaptive_mfi.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

adaptive_mfi.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © palitoj_endthen

//@version=5
indicator(title = 'Adaptive Money Flow Index', shorttitle = 'adaptive_mfi', overlay = false)


// input
hp_period = input.int(defval = 48, title = 'High-Pass Period', group = 'Value', tooltip = 'Determines the High-Pass Period e.g. 48, 89, 125, etc')
src = input.source(defval = ohlc4, title = 'Source', group = 'Options', tooltip = 'Determines the source of input data use to compute dominant cycle, default to ohlc4')
color_type = input.string(defval = 'Solid', title = 'Color Scheme', group = 'Options', options = ['Solid', 'Normalized'], tooltip = 'Determines the color scheme of adaptive money flow index')


// check cumulative volume (tradingview)
vol = volume
cum_vol = 0.0
cum_vol += nz(vol)

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


// dominant cycle: determined an applied adaptive input length with dominant cycle - Ehlers
// variable
avg_length = 3
m = 0.00
n = 0.00
x = 0.00
y = 0.09
alpha1 = 0.00
hp = 0.00
a1 = 0.00
b1 = 0.00
c1 = 0.00
c2 = 0.00
c3 = 0.00
filt = 0.00
sx = 0.00
sy = 0.00
sxx = 0.00
syy = 0.00
sxy = 0.00
max_pwr = 0.00
dominant_cycle = 0.00

// array
var corr = array.new_float(50)
var cosine_part = array.new_float(50)
var sine_part = array.new_float(50)
var sq_sum = array.new_float(50)
var r1 = array.new_float(50)
var r2 = array.new_float(50)
var pwr = array.new_float(50)

// high-pass filter cyclic components whose periods are shorter than 48 bars
pi = 2*math.asin(1)
alpha1 := (math.cos(.707*360/hp_period)+math.sin(.707*360/hp_period)-1)/math.cos(.707*360/hp_period)
hp := (1-alpha1/2)*(1-alpha1/2)*(src-2*src[1]+src[2])+2*(1-alpha1)*nz(hp[1])-(1-alpha1)*(1-alpha1)*nz(hp[2])

// smoothed with super smoother filter
a1 := math.exp(-1.414*pi/10)
b1 := 2*a1*math.cos(1.414*180/10)
c2 := b1
c3 := -a1*a1
c1 := 1-c2-c3
filt := c1*(hp+hp[1])/2+c2*nz(filt[1])+c3*nz(filt[2])

// pearson correlation for each value of lag
for lag = 0 to 48
    // set the averaging length as m
    m := avg_length
    if avg_length == 0
        m := lag
    // initialize correlation sums
    sx := 0.00
    sy := 0.00
    sxx := 0.00
    syy := 0.00
    sxy := 0.00
    // advance samples of both data streams and sum pearson components
    for count = 0 to m-1
        x := filt[count]
        y := filt[lag+count]
        sx := sx+x
        sy := sy+y
        sxx := sxx+x*x
        sxy := sxy+x*y
        syy := syy+y*y
    // compute correlation for each value of lag
    if (m*sxx-sx*sx)*(m*syy-sy*sy) > 0
        array.set(corr, lag, ((m*sxy-sx*sy)/math.sqrt((m*sxx-sx*sx)*(m*syy-sy*sy))))

for period = 10 to 48
    array.set(cosine_part, period, 0)
    array.set(sine_part, period, 0)
    for n2 = 3 to 48
        array.set(cosine_part, period, nz(array.get(cosine_part, period))+nz(array.get(corr, n2))*math.cos(360*n2/period))
        array.set(sine_part, period, nz(array.get(sine_part, period))+nz(array.get(corr, n2))*math.sin(360*n2/period))
    array.set(sq_sum, period, nz(array.get(cosine_part, period))*nz(array.get(cosine_part, period))+nz(array.get(sine_part, period))*nz(array.get(sine_part, period)))

for period2 = 10 to 48
    array.set(r2, period2, nz(array.get(r1, period2)))
    array.set(r1, period2, .2*nz(array.get(sq_sum, period2))*nz(array.get(sq_sum, period2))+.8*nz(array.get(r2, period2)))

// find maximum power level for normalization
max_pwr := .991*max_pwr
for period3 = 10 to 48
    if nz(array.get(r1, period3)) > max_pwr
        max_pwr := nz(array.get(r1, period3))
for period4 = 3 to 48
    array.set(pwr, period4, nz(array.get(r1, period4))/max_pwr)

// compute the dominant cycle using the cg of the spectrum
spx = 0.00
sp = 0.00

for period5 = 10 to 48
    if nz(array.get(pwr, period5)) >= .5
        spx := spx+period5*nz(array.get(pwr, period5))
        sp := sp+nz(array.get(pwr, period5))

if sp != 0
    dominant_cycle := spx/sp
if dominant_cycle < 10
    dominant_cycle := 10
if dominant_cycle > 48
    dominant_cycle := 48


// money flow index
// variable
typical_price = hlc3
p_rmfi = 0.00
n_rmfi = 0.00
mfr = 0.00
mfi = 0.00
smfi = 0.00

// raw money flow index
raw_mfi = typical_price*vol

// adaptive money flow index
dc = int(dominant_cycle)

for i = 0 to dc-1
    // positive money flow
    if typical_price[i] > typical_price[i+1]
        p_rmfi += raw_mfi[i]
    // negative money flow
    if typical_price[i] < typical_price[i+1]
        n_rmfi += raw_mfi[i]
    // money flow ratio
    mfr := p_rmfi/n_rmfi
    // money flow index
    mfi := 100 - (100/(1+mfr))
    // smoothed with super-smoother
    smfi := c1*(mfi+mfi[1])/2+c2*nz(smfi[1])+c3*nz(smfi[2])


// visualize
// color scheme 2 - Normalized
high_smfi = ta.highest(smfi, dc)
low_smfi = ta.lowest(smfi, dc)
norm_smfi = (smfi-low_smfi)/(high_smfi-low_smfi)

col(x) =>
    if x > .5
        color.rgb(255*(2-2*x), 255, 0)
    else if x < .5
        color.rgb(255, 2*255*x, 0)

// color scheme
color_fin = color_type == 'Solid' ? color.maroon : col(norm_smfi)

// plot
_hline = hline(50, color = color.new(color.blue, 50))
plot(smfi, color = color_fin, linewidth = 2)


// create alert
_alert = norm_smfi > .5
alertcondition((not _alert[1] and _alert), title = 'Entry', message = 'Buy/Long entry detected')
alertcondition((_alert and not _alert[1]), title = 'Close', message = 'Sell/Short entry detected')

Overview

Adaptive Money Flow Index filters the input source using a high-pass filter and a SuperSmoother filter, applying Pearson correlation and spectral analysis to estimate the active cycle period. This adaptive length is then used to compute the Money Flow Index in a separate pane. User settings include high-pass period, source selection, and color scheme. The script requires volume data and raises a runtime error if volume is missing.

Details

Published by

palitojendthen on GitHub

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Oscillators & momentum, Volume

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

indicatortechnical-analysispinescripttrading-algorithms

Settings

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

SettingTypeDefaultVariable
High-Pass Periodint48hp_period
Sourcesourceohlc4src
Color SchemestringSolidcolor_type
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 10 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 4 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