Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (2024)

Last Updated : 14 Jul, 2023

Summarize

Comments

Improve

This article will show how to get financial data from Yahoo Finance using Python. We can retrieve company financial information (e.g. financial ratios), as well as historical market data by using this.

Installation of Yahoo Finance Module in Python

Let us install them via pip commands

pip install yfinance

Once it is installed, we can import yfinance package in Python code. We need to pass as an argument of Ticker i.e. the company’s ticker.

Note: A stock symbol or a ticker is a unique series of letters assigned to a security for trading purposes. For example:

  1. For Amazon, it is “AMZN”
  2. For Meta, it is “META”
  3. For Google, it is “GOOGL”

Retrieving Financial Data from Yahoo Finance

Below are various examples that depict how to retrieve Financial Data from Yahoo Finance:

Example 1: Getting META Financial Information

Let us take the results for Meta and hence use the “META” ticker.

Python3

import yfinance as yahooFinance

# Here We are getting Facebook financial information

# We need to pass FB as argument for that

GetFacebookInformation = yahooFinance.Ticker("META")

# whole python dictionary is printed here

print(GetFacebookInformation.info)

Output:

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (1)

META Financial Information

Example 2: Getting META key metrics

We can retrieve financial key metrics like Company Sector, Price Earnings Ratio, and Company Beta from the above dictionary of items quickly.

Python3

import yfinance as yahooFinance

GetFacebookInformation = yahooFinance.Ticker("META")

# display Company Sector

print("Company Sector : ", GetFacebookInformation.info['sector'])

# display Price Earnings Ratio

print("Price Earnings Ratio : ", GetFacebookInformation.info['trailingPE'])

# display Company Beta

print(" Company Beta : ", GetFacebookInformation.info['beta'])

Output :

Company Sector : Communication Services
Price Earnings Ratio : 29.883211
Company Beta : 1.198099

Example 3: Getting META information in key-value pairs

Though we have retrieved a few financial key metrics, as it is a dictionary value, we can split that by means of key-value pair.

Python3

import yfinance as yahooFinance

GetFacebookInformation = yahooFinance.Ticker("META")

# get all key value pairs that are available

for key, value in GetFacebookInformation.info.items():

print(key, ":", value)

Output :

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (2)

META information in key-value pairs

Example 4: Getting META historic data

We can retrieve historical market prices too and display them.

Python3

import yfinance as yahooFinance

GetFacebookInformation = yahooFinance.Ticker("META")

# Let us get historical stock prices for Facebook

# covering the past few years.

# max->maximum number of daily prices available

# for Facebook.

# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y,

# 5y, 10y and ytd.

print(GetFacebookInformation.history(period="max"))

Output :

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (3)

META historic data

Example 5: Getting all the rows of META historical data

If we want to display all the rows of a ticker symbol, we will use the Python Pandas module and set the set_option() function to display maximum rows.

Python3

import yfinance as yahooFinance

import pandas as pd

GetFacebookInformation = yahooFinance.Ticker("META")

pd.set_option('display.max_rows', None)

# Let us get historical stock prices for Facebook

# covering the past few years.

# max->maximum number of daily prices available

# for Facebook.

# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y,

# 5y, 10y and ytd.

print(GetFacebookInformation.history(period="max"))

Output:

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (4)

META historic data for all rows

Example 6: Getting META information for a particular period of time

Even we can have the data for 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, and ytd. Let us check out for 6 months

Python3

import yfinance as yahooFinance

GetFacebookInformation = yahooFinance.Ticker("META")

# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y,

# 2y, 5y, 10y and ytd.

print(GetFacebookInformation.history(period="6mo"))

Output:

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (5)

META information for 6 months

Example 7: Getting META information for the provided date range

We have the flexibility to get historical market data for the provided start and end dates too.

Python3

import yfinance as yahooFinance

# in order to specify start date and

# end date we need datetime package

import datetime

# startDate , as per our convenience we can modify

startDate = datetime.datetime(2019, 5, 31)

# endDate , as per our convenience we can modify

endDate = datetime.datetime(2021, 1, 30)

GetFacebookInformation = yahooFinance.Ticker("META")

# pass the parameters as the taken dates for start and end

print(GetFacebookInformation.history(start=startDate,

end=endDate))

Output:

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (6)

META information for a date range



priyarajtt

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (8)

Improve

Next Article

How to Get Data from API in Python Flask

Please Login to comment...

Get Financial Data from Yahoo Finance with Python - GeeksforGeeks (2024)
Top Articles
How to install certificates in Google Chrome - Полезная информация о хостинге от PQ.Hosting
Top 20 HARDEST Video Game Achievements | Articles on WatchMojo.com
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5968

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.