Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Getting Stock Symbols with yfinance in Python
Next article icon

How to Use yfinance API with Python

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The yfinance API is a powerful tool for accessing financial data from Yahoo Finance. It allows users to download historical market data, retrieve financial information, and perform various financial analyses. This API is widely used in finance, investment, and trading applications for its ease of use and comprehensive data coverage. In this article, we will explore the use yfinance API in Python with detailed examples.

What is yfinance?

yfinance is a Python library designed for accessing and retrieving financial data from Yahoo Finance. It simplifies the process of fetching historical market data, financial statements, and other relevant information for stocks, indices, and securities. Widely utilized in finance, investment, and trading applications, yfinance offers comprehensive data coverage and ease of integration, making it a popular choice among developers and financial analysts alike.

Setting Up yfinance API

To use yfinance, you need to install it first. This can be done using pip:

pip install yfinance

After installation, you can import the library in your Python script:

Python
import yfinance as yf 

Fetching Financial Data

With yfinance, you can fetch historical market data and financial information about stocks, indices, and other securities. The primary method for this is the Ticker object.

Example 1: Fetching Historical Market Data of Apple Inc

In this example, we are using the yfinance library in Python to fetch comprehensive financial data for the ticker symbol "AAPL" (Apple Inc.). We begin by creating a Ticker object for "AAPL" and then use it to retrieve historical market data for the last year (period="1y"). The fetched data includes daily Open, High, Low, Close prices, and Volume. Additionally, we fetch basic financial statements (financials) and stock actions (actions) such as dividends and splits related to Apple's stock.

Python
import yfinance as yf  # Define the ticker symbol ticker_symbol = "AAPL"  # Create a Ticker object ticker = yf.Ticker(ticker_symbol)  # Fetch historical market data historical_data = ticker.history(period="1y")  # data for the last year print("Historical Data:") print(historical_data)  # Fetch basic financials financials = ticker.financials print("\nFinancials:") print(financials)  # Fetch stock actions like dividends and splits actions = ticker.actions print("\nStock Actions:") print(actions) 

Output

Screenshot-2024-07-13-224538-min

Example 2: Fetching Historical Market Data of Microsoft

In this example, we use yfinance to fetch historical market data for Microsoft Corporation (MSFT). We create a Ticker object for "MSFT" and fetch data for the last month (period="1mo"). The fetched data includes daily Open, High, Low, Close prices, and Volume. The summary output displays these key data points in a concise format, making it easier to analyze short-term trends for Microsoft's stock.

Python
import yfinance as yf  # Define the ticker symbol ticker_symbol = "MSFT"  # Create a Ticker object ticker = yf.Ticker(ticker_symbol)  # Fetch historical market data for the last 30 days historical_data = ticker.history(period="1mo")  # data for the last month  # Display a summary of the fetched data print(f"Summary of Historical Data for {ticker_symbol}:") print(historical_data[['Open', 'High', 'Low', 'Close', 'Volume']]) 

Output

OP2-min

Conclusion

In conclusion, the yfinance API proves invaluable for accessing detailed financial data from Yahoo Finance directly into Python applications. It simplifies the process of fetching historical market data, financial statements, and stock actions for various securities such as stocks and indices. By leveraging yfinance, users can efficiently perform financial analyses and gain insights crucial for finance, investment, and trading decisions.


Next Article
Getting Stock Symbols with yfinance in Python

G

gpancomputer
Improve
Article Tags :
  • Python
  • Python-API
Practice Tags :
  • python

Similar Reads

  • How to Use Mega.nz API With Python?
    In this article, we are going to see how to use mega.nz API with Python. MEGA.NZ is End-to-end encrypted and the encryption keys are owned by us. It means that mega.NZ employees won't be able to read personal data. Mega.py is a great Python module for interacting with mega.nz API. It provides easy t
    3 min read
  • How to Install yfinance with Python PIP
    The yfinance API is a powerful tool for accessing financial data from Yahoo Finance. It allows users to download historical market data, retrieve financial information, and perform various financial analyses. This API is widely used in finance, investment, and trading applications for its ease of us
    2 min read
  • How to Install Python yfinance using GitHub
    Yfinance is a popular Python library for accessing Yahoo Finance's financial data. While it is commonly installed via pip, installing it directly from GitHub allows you to access the latest development version, which may contain new features or bug fixes not yet available in the pip version. This gu
    2 min read
  • How to Check yfinance version in Python
    It is a financial data library for developers that provides a simple and convenient way to access historical market data from Yahoo Finance. It allows users to download stock price data, financial statements, and other relevant financial information for analysis and trading. With its easy-to-use int
    2 min read
  • Getting Stock Symbols with yfinance in Python
    yfinance is a Python library used for accessing financial data from Yahoo Finance. This data is useful for downloading stock data, historical market prices and various financial statistics. Using yfinance users can easily obtain stock symbols, historical price data and real-time market information.
    4 min read
  • How to Import yfinance as yf in Python
    Importing a module as an alias is important in Python to reduce the overhead of typing long module names and to enhance code readability. Using an alias allows for more concise and manageable code, especially when dealing with frequently used libraries or those with lengthy names. For example, impor
    3 min read
  • How to Make API Call Using Python
    APIs (Application Programming Interfaces) are an essential part of modern software development, allowing different applications to communicate and share data. Python provides a popular library i.e. requests library that simplifies the process of calling API in Python. In this article, we will see ho
    3 min read
  • How to Use ChatGPT API in Python?
    ChatGPT and its inevitable applications. Day by Day everything around us seems to be getting automated by several AI models using different AI and Machine learning techniques and Chatbot with Python , there are numerous uses of Chat GPT and one of its useful applications we will be discussing today.
    6 min read
  • Comparing yfinance vs yahoo_fin in Python
    In the field of financial data analysis and automation, having quality information at the right time is very vital. Data scraping activity in Python generally depends on specific libraries to fetch information from web pages such as Yahoo Finance, out of which two packages are yfinance and yahoo_fin
    9 min read
  • How to Use Google Cloud Function with Python ?
    Google Cloud Functions provides a way to run small pieces of code in response to cloud events without managing servers. If you're a developer looking to automate tasks, process data or build APIs, Python is a great language for working with Google Cloud Functions. In this article, we will look into
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences