Indicator · Pine Script v5 · Own pane

MFI Divergences Lower

by g-moe· code credited to goodgains

This indicator plots the Money Flow Index in a separate pane and marks bullish and bearish divergence from pivot highs and lows.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code214
Settings18inputs
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

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

mfi-divergences-lower.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", overlay = false, format=format.price, precision=2)

// @keywords: mfi-divergences, money-flow-index, regular-divergence, hidden-divergence, pivot-dots, overbought-oversold, buy-sell-signals, mfi-plot, divergence-detection, lower-pane-oscillator, pivot-lookback, trade-conditions, horizontal-levels, dot-signals, mfi-extremes, lower-timeframe, signal-looping, bull-bear-dots, mfi-length, divergence-alerts

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
signal_looping  = input.int(2, title = 'Number of dots to look at for a signal​', group = 'Trade Settings', inline = '0',
 minval = 1, maxval = 20)

pivot_lookback  = input.int(3, 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)
plot(mfi, "MFI", color=#7E57C2)
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")



f_array_add_pop(array, new) =>
    array.pop(array)
    array.unshift(array, new)



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

_array_size = signal_looping+1

var buy_lows = array.new_float(_array_size, na)
var buy_values = array.new_float(_array_size, na)

var sell_highs = array.new_float(_array_size, na)
var sell_values = array.new_float(_array_size, na)

var buy_bns = array.new_int(_array_size, na)
var sell_bns = array.new_int(_array_size, na)


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

if swing_low
    f_array_add_pop(buy_lows, low[pivot_lookback])
    f_array_add_pop(buy_values, mfi)
    f_array_add_pop(buy_bns, bar_index[pivot_lookback])

if swing_high
    f_array_add_pop(sell_highs, high[pivot_lookback])
    f_array_add_pop(sell_values, mfi)
    f_array_add_pop(sell_bns, bar_index[pivot_lookback]) 


f_buy_sell_signals(
 buy_price_arr, buy_dot_arr, sell_price_arr, sell_dot_arr,
 buy_bn_arr, sell_bn_arr) =>

    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 int     buy_array_size   = 0
    var float   buy_price_arr0   = 0.00
    var float   buy_dot_arr0     = 0.00
    var int     buy_bn_arr0      = 0

    var bool    sell_signal      = false
    var bool    sell_hidd_signal = false
    var int     sell_array_size  = 0
    var float   sell_price_arr0  = 0.00
    var float   sell_dot_arr0    = 0.00
    var int     sell_bn_arr0     = 0

    var int     index_of_buy     = 0
    var int     index_of_sell    = 0


    // look for buys
    buy_array_size  := array.size(buy_price_arr)
    buy_price_arr0  := array.get(buy_price_arr,0)
    buy_dot_arr0    := array.get(buy_dot_arr,0)
    buy_bn_arr0     := array.get(buy_bn_arr,0)

    for i = 1 to buy_array_size - 1
        reg_b := (buy_price_arr0 <= array.get(buy_price_arr,i) and buy_dot_arr0 >= array.get(buy_dot_arr,i))
        hid_b := (buy_price_arr0 >= array.get(buy_price_arr,i) and buy_dot_arr0 <= array.get(buy_dot_arr,i))

        buy1 := (buy_dot_arr0 <= mfi_below) or not use_trade_cond1
        buy2 := (buy_bn_arr0 - array.get(buy_bn_arr, i) <= 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)

        if buy_signal or buy_hidd_signal
            index_of_buy := i
            break

    // look for sells
    sell_array_size  := array.size(sell_price_arr)
    sell_price_arr0  := array.get(sell_price_arr,0)
    sell_dot_arr0    := array.get(sell_dot_arr,0)
    sell_bn_arr0     := array.get(sell_bn_arr,0)

    for i = 1 to sell_array_size - 1
        reg_s := (sell_price_arr0 >= array.get(sell_price_arr,i) and sell_dot_arr0 <= array.get(sell_dot_arr,i))
        hid_s := (sell_price_arr0 <= array.get(sell_price_arr,i) and sell_dot_arr0 >= array.get(sell_dot_arr,i))

        sell1 := (sell_dot_arr0 >= mfi_above) or not use_trade_cond1
        sell2 := (sell_bn_arr0 - array.get(sell_bn_arr, i) <= 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)

        if sell_signal or sell_hidd_signal
            index_of_sell := i
            break

    [buy_signal, buy_hidd_signal, sell_signal, sell_hidd_signal, index_of_buy, index_of_sell]

[f_buy_signal, f_buy_hide_signal, f_sell_signal, f_sell_hide_signal, index_of_buy, index_of_sell] = f_buy_sell_signals(
 buy_lows, buy_values, sell_highs, sell_values,
 buy_bns, sell_bns)

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]

plotshape(buy_signal or buy_hide_signal ? 0 : na,title='BUY Signal', style = shape.triangleup,location = location.absolute, color=bull_color1, size = size.tiny, offset = -pivot_lookback)
plotshape(sell_signal or sell_hide_signal? 100 : na,title='SELL Signal', style = shape.triangledown,location = location.absolute, color=bear_color1, size = size.tiny, offset = -pivot_lookback)
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', offset = -pivot_lookback)


// {TRENDLINES
f_draw_divs(values_array, bn_array, array_index, color, hidden) =>

    x_1 = array.get(bn_array, array_index)
    x_2 = array.get(bn_array, 0)
    y_1 = array.get(values_array, array_index)
    y_2 = array.get(values_array, 0)
    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(buy_values, buy_bns, index_of_buy, bull_color1, false)
if buy_hide_signal
    f_draw_divs(buy_values, buy_bns, index_of_buy, bull_color1, true)

if sell_signal
    f_draw_divs(sell_values, sell_bns, index_of_sell, bear_color1, false)
if sell_hide_signal
    f_draw_divs(sell_values, sell_bns, index_of_sell, bear_color1, true)
// TRENDLINES}



// {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 stores recent pivot prices, MFI values, and bar indexes, then checks regular or hidden divergence across a chosen number of dots. Optional filters require the MFI to sit beyond overbought or oversold levels and limit how many bars may separate the dots. Users can change MFI length, source, pivot length, and level lines. It is not a strategy and has no entries, exits, or stop loss.

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

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

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
Number of dots to look at for a signal​int2signal_looping
Pivot Length            int3pivot_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
Show all 18 settings
SettingTypeDefaultVariable
bn_buyint25bn_buy
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