Indicator · Pine Script v5 · Own pane

MFI Divergences Lower

by g-moe· code credited to goodgains

This indicator calculates the Money Flow Index (MFI) and detects regular and hidden divergences between price action and the MFI oscillator in a separate pane.

View on GitHub

Repaints

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code233
Settings17inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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

233 lines of Pine Script v5 from Tradingview/mfi-divergences-lower-v3.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

mfi-divergences-lower-v3.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("MFI Divergences Lower v3", overlay = false, format=format.price, precision=2, max_lines_count = 500)

// @keywords: money-flow-index, mfi-divergence, regular-divergence, hidden-divergence, overbought-oversold, pivot-length, buy-signal, sell-signal, mfi-oscillator, divergence-dots, ob-os-bands, middle-band, trade-conditions, max-bars-between, divergence-alerts, mfi-source, configurable-length, oscillator-pane, divergence-lines, mfi-overbought

bull_color1 = input.color(color.new(#1ecd25, 0), title = 'Bull 1', inline = '1')
bull_color2 = input.color(color.new(#346836, 0), title = 'Bull 2', inline = '1')

bear_color1 = input.color(color.new(#e31414, 0), title = 'Bear 1', inline = '1')
bear_color2 = input.color(color.new(#6f2d2d, 0), title = 'Bear 2', inline = '1')



// TRADE SIGNAL SETTINGS
pivot_lookback  = input.int(5, title = 'Pivot Length            ', group = 'Trade Settings', inline = 'a')

use_reg_div     = input.bool(true, title = 'Regular Div.  ', group = 'Trade Settings', inline = '1')
use_hid_div     = input.bool(false, title = 'Hidden Div.', group = 'Trade Settings', inline = '1')

use_trade_cond1 = input.bool(true, title = '1. MFI Above/Below    ', group = 'Trade Settings', inline = '2')
mfi_below       = input.int(30, title = '', group = 'Trade Settings', inline = '2')
mfi_above       = input.int(70, title = '', group = 'Trade Settings', inline = '2')

use_trade_cond2 = input.bool(true, title = '2. Max Bars between dots ', group = 'Trade Settings', inline = '3')
bn_buy          = input.int(25, title = '', group = 'Trade Settings', inline = '3')
bn_sell         = input.int(25, title = '', group = 'Trade Settings', inline = '3')



// MFI settings
mfi_length = input.int(title="MFI Length", defval=14, minval=1, maxval=2000, group = 'MFI Settings', inline = '1')
mfi_src = input.source(hlc3, title = 'MFI Source', group = 'MFI Settings', inline = '2')
mfi_ob = input.int(70, title = 'OB', group = 'MFI Settings', inline = '3')
mfi_os = input.int(30, title = 'OS', group = 'MFI Settings', inline = '3')



// MFI calculate
mfi = ta.mfi(mfi_src, mfi_length)
overbought=hline(mfi_ob, title="Overbought", color=#787B86)
oversold=hline(mfi_os, title="Oversold", color=#787B86)
hline(math.floor((mfi_ob+mfi_os)/2), "Middle Band", color=color.new(#787B86, 50))
fill(overbought, oversold, color=color.rgb(126, 87, 194, 90), title="Background")
plot(mfi, "MFI", color=#7E57C2)



// PIVOT Values calculate
var bool swing_high = na
var bool swing_low = na

var float   csh = na
var int     csh_bn = na
var float   psh = na
var int     psh_bn = na
var float   csl = na
var int     csl_bn = na
var float   psl = na
var int     psl_bn = na

var float   mfi_csh = na
var int     mfi_csh_bn = na
var float   mfi_psh = na
var int     mfi_psh_bn = na
var float   mfi_csl = na
var int     mfi_csl_bn = na
var float   mfi_psl = na
var int     mfi_psl_bn = na

var int     bars_between_lows  = na
var int     bars_between_highs = na


swing_high := ta.pivothigh(high, pivot_lookback, pivot_lookback)
swing_low  := ta.pivotlow(low, pivot_lookback, pivot_lookback)

if swing_low
    csl         := low[pivot_lookback]
    csl_bn      := bar_index[pivot_lookback]
    mfi_csl     := mfi[pivot_lookback]
    mfi_csl_bn  := bar_index[pivot_lookback]

    psl         := csl[1]
    psl_bn      := psl_bn[1]
    mfi_psl     := mfi_csl[1]
    mfi_psl_bn  := mfi_csl_bn[1]

if swing_high
    csh         := high[pivot_lookback]
    csh_bn      := bar_index[pivot_lookback]
    mfi_csh     := mfi[pivot_lookback]
    mfi_csh_bn  := bar_index[pivot_lookback]

    psh         := csh[1]
    psh_bn      := psh_bn[1]
    mfi_psh     := mfi_csh[1]
    mfi_psh_bn  := mfi_csh_bn[1]

bars_between_highs := bar_index - csh_bn
bars_between_lows  := bar_index - csl_bn



// hh = 2
// lh = -1
// ll = -2
// hl = 1
f_get_trend(current, sh, sl, csh, csl, psh, psl) =>
    var int trend = na
    var int highs_last_signal = na
    var int lows_last_signal = na

    hh = sh and csh >= psh
    lh = sh and csh < psh
    ll = sl and csl <= psl
    hl = sl and csl > psl

    trend := ((hh and high >= psh) or close > csh) ? 1 : ((ll and low <= psl) or close < csl) ? -1 : lh or hl ? 0 : trend[1]
    
    highs_last_signal := (hh and high >= psh) or current > csh ? 1 
     : lh ? 0 : highs_last_signal[1]
    lows_last_signal  := (ll and low <= psl) or current < csl ? 1 
     : hl ? 0 : lows_last_signal[1]

    [trend, highs_last_signal, lows_last_signal]




f_buy_sell_signals(price_highs_last, price_lows_last,
 osc_highs_last, osc_lows_last,
 mfi_psh, mfi_psl, bn_highs, bn_lows) =>
    var bool reg_b = na
    var bool hid_b = na
    var bool buy1 = na
    var bool buy2 = na

    var bool reg_s = na
    var bool hid_s = na
    var bool sell1 = na
    var bool sell2 = na

    var bool    buy_signal       = false
    var bool    buy_hidd_signal  = false
    var bool    sell_signal      = false
    var bool    sell_hidd_signal = false


    // buy signals (ll and hl) (hl and ll)
    reg_b := price_lows_last == 1 and osc_lows_last == 0
    hid_b := price_lows_last == 0 and osc_lows_last == 1

    buy1 := (mfi_psl <= mfi_below) or not use_trade_cond1
    buy2 := (bn_lows <= bn_buy) or not use_trade_cond2

    buy_signal := (reg_b and use_reg_div) and (buy1 and buy2)
    buy_hidd_signal := (hid_b and use_hid_div) and (buy1 and buy2)


    // sell signals (hh and lh) (lh and hh)
    reg_s := price_highs_last == 1 and osc_highs_last == 0
    hid_s := price_highs_last == 0 and osc_highs_last == 1

    sell1 := (mfi_psh >= mfi_above) or not use_trade_cond1
    sell2 := (bn_highs <= bn_sell) or not use_trade_cond2

    sell_signal := (reg_s and use_reg_div) and (sell1 and sell2)
    sell_hidd_signal := (hid_s and use_hid_div) and (sell1 and sell2)

    [buy_signal, buy_hidd_signal, sell_signal, sell_hidd_signal]



[price_trend, price_highs_last, price_lows_last] = 
 f_get_trend(close, swing_high, swing_low, csh, csl, psh, psl)
[osc_trend, osc_highs_last, osc_lows_last]       = 
 f_get_trend(mfi, swing_high, swing_low, mfi_csh, mfi_csl, mfi_psh, mfi_psl)





[f_buy_signal, f_buy_hide_signal, f_sell_signal, f_sell_hide_signal] = f_buy_sell_signals(
 price_highs_last, price_lows_last,
 osc_highs_last, osc_lows_last,
 mfi_psh, mfi_psl,
 bars_between_highs, bars_between_lows)

var bool buy_signal = na
var bool buy_hide_signal = na
var bool sell_signal = na
var bool sell_hide_signal = na

buy_signal       := f_buy_signal and not f_buy_signal[1]
buy_hide_signal  := f_buy_hide_signal and not f_buy_hide_signal[1]
sell_signal      := f_sell_signal and not f_sell_signal[1]
sell_hide_signal := f_sell_hide_signal and not f_sell_hide_signal[1]


// {TRENDLINES
f_draw_divs(x_1, x_2, y_1, y_2, color, hidden) =>
    line.new(x1=x_1, y1=y_1, x2=x_2, y2=y_2, xloc = xloc.bar_index, extend = extend.none,
     color = color, style = hidden ? line.style_dashed : line.style_solid)

if buy_signal
    f_draw_divs(mfi_psl_bn, mfi_csl_bn, mfi_psl, mfi_csl, bull_color1, false)
if buy_hide_signal
    f_draw_divs(mfi_psl_bn, mfi_csl_bn, mfi_psl, mfi_csl, bull_color1, true)


if sell_signal
    f_draw_divs(mfi_psh_bn, mfi_csh_bn, mfi_psh, mfi_csh, bear_color1, false)
if sell_hide_signal
    f_draw_divs(mfi_psh_bn, mfi_csh_bn, mfi_psh, mfi_csh, bear_color1, true)
// TRENDLINES}



plotshape(buy_signal or buy_hide_signal ? 0 : na,title='BUY Signal', style = shape.triangleup,location = location.absolute, color=bull_color1, size = size.tiny)
plotshape(sell_signal or sell_hide_signal ? 100 : na,title='SELL Signal', style = shape.triangledown,location = location.absolute, color=bear_color1, size = size.tiny)
bgcolor(buy_signal or buy_hide_signal ? color.new(bull_color1, 75) : sell_signal or sell_hide_signal ? color.new(bear_color1, 75) : na, title = 'Signal Background')


// {ALERTS
alertcondition(buy_signal or buy_hide_signal, "Buy Signal", "Buy Signal")
alertcondition(sell_signal or sell_hide_signal, "Sell Signal", "Sell Signal")


// ALERTS}

Overview

MFI Divergences Lower identifies high and low pivot points using a configurable lookback length and evaluates trade conditions, such as overbought or oversold thresholds and maximum bar distance between pivots. Users can toggle divergence types, adjust MFI length, and set signal filters. Because divergence detection relies on pivot confirmation, signals display with a lag equal to the lookback period.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Oscillators & momentum, Divergence

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
Bull 1colorcolor.new(#1ecd25, 0)bull_color1
Bull 2colorcolor.new(#346836, 0)bull_color2
Bear 1colorcolor.new(#e31414, 0)bear_color1
Bear 2colorcolor.new(#6f2d2d, 0)bear_color2
Pivot Length            int5pivot_lookback
Regular Div.  booltrueuse_reg_div
Hidden Div.boolfalseuse_hid_div
1. MFI Above/Below    booltrueuse_trade_cond1
mfi_belowint30mfi_below
mfi_aboveint70mfi_above
2. Max Bars between dots booltrueuse_trade_cond2
bn_buyint25bn_buy
Show all 17 settings
SettingTypeDefaultVariable
bn_sellint25bn_sell
MFI Lengthint14mfi_length
MFI Sourcesourcehlc3mfi_src
OBint70mfi_ob
OSint30mfi_os
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

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
330 stars

This indicator calculates the MACD oscillator from a configurable source with fast and slow lengths, then plots the histogram, MACD line, and signal line in a separate pane. It detects regular bullish divergences (price lower lows with oscillator higher lows),

Indicator v5 Oscillators & momentumDivergence
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]
218 stars

This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe RSI with Divergence

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

RSI [SystemAlpha]

by iamc1oud
56 stars

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills. It identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and be

Indicator v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of RSI [SystemAlpha]
55 stars

This indicator calculates a 14-period RSI from close and plots it in a separate pane with dotted overbought and dashed oversold lines at 70 and 30. It marks bullish divergence when a five-bar pivot low in price is lower than the price twenty bars earlier while

Indicator v6 Oscillators & momentumDivergence
Updated 6 months ago · Apache-2.0 GitHub repository of RSI Divergence Detector

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