Real-Time Indian Stock Price Tracking with Python and Yahoo Finance
Introduction: Keeping track of real-time stock prices in the Indian market is crucial for investors and financial enthusiasts. Python, coupled with the powerful yfinance library, provides a convenient solution for fetching and analyzing stock data. In this article, we will explore a Python program that utilizes yfinance to fetch and display the latest stock prices of Indian companies.
Code Explanation: The code starts by importing the “yfinance” library, which allows us to access stock data from Yahoo Finance. It then defines a list of Indian companies, each represented by a dictionary containing the company name and its stock symbol.
Next, the program iterates over the list of companies. For each company, it extracts the stock symbol and name. Using the yfinance library, it fetches the stock data for the given symbol. The program retrieves the closing price for the most recent trading day and stores it in the variable “current_price.”
To ensure the stock price is displayed with two decimal places, the code formats the current price using the “{:.2f}” format specifier, which rounds the number to two decimal places.
Finally, the program prints the company name along with the formatted stock price, providing users with real-time insights into Indian stock prices.
Full code in python:
The command !pip install yfinance
is used to install the yfinance
library directly in a Jupyter Notebook or Google Colab environment. It is a shorthand notation specific to these environments, where the exclamation mark (!
) indicates that the command should be run in a shell or command line within the notebook itself.
!pip install yfinance
import yfinance as yf
# Define the list of top 10 companies by market capitalization on NSE
companies = [
{"name": "Reliance Industries Ltd.", "symbol": "RELIANCE.NS"},
{"name": "Tata Consultancy Services Ltd.", "symbol": "TCS.NS"},
{"name": "HDFC Bank Ltd.", "symbol": "HDFCBANK.NS"},
{"name": "Hindustan Unilever Ltd.", "symbol": "HINDUNILVR.NS"},
{"name": "Infosys Ltd.", "symbol": "INFY.NS"},
{"name": "ICICI Bank Ltd.", "symbol": "ICICIBANK.NS"},
{"name": "Bharti Airtel Ltd.", "symbol": "BHARTIARTL.NS"},
{"name": "Kotak Mahindra Bank Ltd.", "symbol": "KOTAKBANK.NS"},
{"name": "ITC Ltd.", "symbol": "ITC.NS"},
{"name": "Housing Development Finance Corporation Ltd.", "symbol": "HDFC.NS"}
]
# Iterate over the companies and fetch the stock prices
for company in companies:
symbol = company["symbol"]
name = company["name"]
# Fetch stock data using yfinance
stock = yf.Ticker(symbol)
current_price = stock.history(period='1d')["Close"].iloc[-1]
# Format the stock price with 2 decimal places
formatted_price = "{:.2f}".format(current_price)
# Display the company name and stock price
print(f"{name}: {formatted_price}")
Conclusion: In this article, we have explored a Python program that leverages the yfinance library to track and display real-time stock prices of Indian companies. By executing this program, you can effortlessly retrieve the latest stock prices and make informed decisions in the Indian stock market. This code provides a foundation for building advanced financial analysis tools, automating trading strategies, or integrating stock price data into other applications. Python’s versatility, coupled with the yfinance library, makes it an ideal choice for anyone interested in analyzing Indian stock data and staying updated with market trends.
Comments
Post a Comment