While AMFI ( Association of Mutual Funds in India) provides the foundational NAV data, this guide focuses on the engineering required to build high-performance retrieval systems.
Each Mutual Fund scheme is identified by a unique, standardized scheme code; by programmatically targeting this identifier, we can retrieve real-time data points including the latest NAV (Net Asset Value), the most recent valuation date, and the official scheme nomenclature.import requests
def get_latest_nav(scheme_code):
url = f"https://api.mfapi.in/mf/{scheme_code}/latest"
response = requests.get(url)
data = response.json()
latest_nav = data['data'][0]['nav']
date = data['data'][0]['date']
fund_name = data['meta']['scheme_name']
return fund_name, latest_nav, date
# Example: Quant Small Cap Fund (Direct Plan)
name, nav, date = get_latest_nav("120847")
print(f"Fund: {name} | Current NAV: {nav} | Date: {date}")Fund: quant ELSS Tax Saver Fund - Growth Option - Direct Plan | Current NAV: 414.31430 | Date: 09-01-2026
/latest — Appended to the URL to fetch only the most recent data point, reducing payload size.
data['data'][0]['date'] — Specifically extracts the timestamp of the last updated NAV from the API response.
return fund_name, latest_nav, date — Returns a tuple containing all three critical data points for further processing.
print(f"... Date: {date}") — Uses Python f-strings to display the formatted output in the console.
# Input scheme list
input_schemes = [
{"schemeCode": 120251, "schemeName": "ICICI Prudential Equity & Debt Fund Direct Growth"},
{"schemeCode": 118955, "schemeName": "HDFC Flexi Cap Direct Plan Growth"},
{"schemeCode": 120334, "schemeName": "ICICI Prudential Multi Asset Fund Direct Growth"}
]
For production environments, this input list can be dynamically retrieved from databases such as SQLite or MySQL. Furthermore, the processed outputs can be archived back into storage for historical analysis and reporting.
Below is the complete, integrated implementation.
import requests
import json
from datetime import datetime
def fetch_latest_navs(scheme_list):
"""
Retrieves the most recent NAV and corresponding date for a list of funds.
"""
base_url = "https://api.mfapi.in/mf/"
final_report = []
print(f"{'SCHEME NAME':<65} | {'NAV':<10} | {'DATE':<12}")
print("=" * 92)
for scheme in scheme_list:
code = scheme['schemeCode']
name = scheme['schemeName']
try:
# Request data from MFAPI
response = requests.get(f"{base_url}{code}/latest", timeout=10)
response.raise_for_status()
data = response.json()
if "data" in data and len(data["data"]) > 0:
# API data is usually in reverse chronological order.
latest = data["data"][0]
nav = latest["nav"]
date = latest["date"]
final_report.append({
"name": name,
"code": code,
"nav": nav,
"date": date
})
# Truncated name for table alignment
short_name = (name[:62] + '..') if len(name) > 65 else name
print(f"{short_name:<65} | {nav:<10} | {date:<12}")
else:
print(f"Data missing for: {name}")
except Exception as e:
print(f"Could not retrieve {code}: {e}")
return final_report
if __name__ == "__main__":
# Input scheme list
input_schemes = [
{"schemeCode": 120251, "schemeName": "ICICI Prudential Equity & Debt Fund Direct Growth"},
{"schemeCode": 118955, "schemeName": "HDFC Flexi Cap Direct Plan Growth"},
{"schemeCode": 120334, "schemeName": "ICICI Prudential Multi Asset Fund Direct Growth"}
]
print(f"Querying MFAPI for latest records...\n")
results = fetch_latest_navs(input_schemes)
Querying MFAPI for latest records...
SCHEME NAME | NAV | DATE
=================================================================================
ICICI Prudential Equity & Debt Fund Direct Growth | 453.09000 | 09-01-2026
HDFC Flexi Cap Direct Plan Growth | 2264.30200 | 09-01-2026
ICICI Prudential Multi Asset Fund Direct Growth | 898.67790 | 09-01-2026
import requests
import matplotlib.pyplot as plt
from datetime import datetime
def fetch_and_plot_nav(scheme_code):
"""
Fetches historical NAV data for a given scheme code from mfapi.in
and plots the variation over time.
"""
api_url = f"https://api.mfapi.in/mf/{scheme_code}"
print(f"Fetching data for Scheme Code: {scheme_code}...")
try:
response = requests.get(api_url, timeout=10)
response.raise_for_status()
data = response.json()
if data.get("status") != "SUCCESS":
print("Error: Could not find data for this scheme code.")
return
scheme_name = data['meta']['scheme_name']
nav_data = data['data']
# Extract dates and NAV values
# We take the last 60 entries for a clear 2-month view
subset = nav_data[:60]
# Parse dates and convert NAVs to floats
dates = [datetime.strptime(item['date'], '%d-%m-%Y') for item in subset]
nav_values = [float(item['nav']) for item in subset]
# Sort by date
plot_data = sorted(zip(dates, nav_values))
dates, nav_values = zip(*plot_data)
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(dates, nav_values, marker='o', linestyle='-',
color='b', markersize=4)
plt.title(f"NAV Variation: {scheme_name}", fontsize=12)
plt.xlabel("Date")
plt.ylabel("Net Asset Value (NAV)")
plt.grid(True, linestyle='--', alpha=0.7)
plt.xticks(rotation=45)
plt.tight_layout()
print("Displaying graph...")
plt.show()
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
print("--- Mutual Fund NAV Tracker ---")
user_code = input("Enter the Mutual Fund Scheme Code: ").strip()
if user_code.isdigit():
fetch_and_plot_nav(user_code)
else:
print("Please enter a valid numeric scheme code.")
Once the current Net Asset Value (NAV) is retrieved for each scheme, you can determine your total investment value by multiplying the NAV by the number of units held. This provides a real-time snapshot of your portfolio's worth:
Integrating this logic allows for automated tracking of gains, losses, and overall asset allocation.
Access raw master data for all mutual fund schemes including scheme codes, types, and classification categories via the official AMFI portal.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.