Home » Technical Analysis Functions » Understanding the ta.mfi() Function in Pine Script

Understanding the ta.mfi() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the ta.mfi() function in Pine Script, exploring its syntax, arguments, and how to implement it through an illustrative example.

Syntax Overview

ta.mfi(series, length) → series float


  • series (series int/float): This represents the series of values to be processed, typically price data.
  • length (series int): Specifies the number of bars, or time periods, over which the MFI calculation is to be performed.

Detailed Example

In Pine Script, implementing the MFI is straightforward. Below is a basic script example:

indicator("Money Flow Index")

plot(ta.mfi(hlc3, 14), color=color.yellow)

This script plots the Money Flow Index using a 14-period length on the hlc3 price series, which is the average of the high, low, and close prices. The MFI values are displayed in yellow.

Code Walkthrough

  1. Pine Script Version Declaration:
    • //@version=5: This line specifies that the script uses version 5 of Pine Script. Each version has its own set of features and syntax rules, with version 5 being the latest and offering advanced functionalities.
  2. Indicator Declaration:
    • indicator("Money Flow Index"): This function declares the script as a custom indicator on TradingView and names it “Money Flow Index”. The name provided in the quotes appears in the indicator window when the script is added to a chart.
  3. Plotting the Money Flow Index:
    • plot(ta.mfi(hlc3, 14), color=color.yellow): This line of code is responsible for calculating and plotting the Money Flow Index (MFI) on the chart. Let’s dissect it further:
      • ta.mfi(hlc3, 14): This function call calculates the Money Flow Index. ta is a namespace containing technical analysis functions, mfi is the function for calculating the Money Flow Index, hlc3 is the price series used (average of the high, low, and close prices of each bar), and 14 is the length or period over which the MFI is calculated. The MFI uses both price and volume to identify overbought or oversold conditions.
      • color=color.yellow: This argument specifies the color of the MFI plot line on the chart. Here, color.yellow sets the line color to yellow, making the MFI visually distinct and easy to identify among other indicators.

Custom MFI Calculation

To deepen understanding, let’s dissect a custom MFI calculation:

pine_mfi(source, duration) =>
    float positive_flow = math.sum(volume * (ta.change(source) <= 0.0 ? 0.0 : source), duration)
    float negative_flow = math.sum(volume * (ta.change(source) >= 0.0 ? 0.0 : source), duration)
    custom_mfi = 100.0 - (100.0 / (1.0 + positive_flow / negative_flow))

plot(pine_mfi(hlc3, 14))
Custom MFI Calculation

Code Walkthrough

  • positive_flow and negative_flow Calculation: These variables accumulate the “money flow” over the specified duration. positive_flow sums up volumes multiplied by the source when its change is positive, indicating buying pressure. Conversely, negative_flow sums volumes during periods of selling pressure (negative change in the source).
  • custom_mfi Calculation: The MFI is derived by comparing the positive and negative flows, translating into a value between 0 and 100. A high MFI indicates overbought conditions, while a low MFI suggests oversold conditions.

Key Features and Takeaways

  • Function Useability and Syntax: The ta.mfi() function is user-friendly, requiring just the data series and period length to calculate the Money Flow Index.
  • Custom Implementation Insight: The breakdown of the custom MFI function illustrates the versatility of Pine Script in financial analysis.
  • Application: MFI is invaluable for traders and analysts for identifying potential market reversals based on volume-weighted price momentum.

Leave a Comment