Skip to main content

Real-Time Indian Stock Price Tracking with Python and Yahoo Finance

 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

Popular posts from this blog

SAMPLE QUESTIONS ASKED DURING INTERVIEW

Sample Questions Asked During the Interview 1. Introduce Yourself Good morning/afternoon. Thank you for the opportunity to introduce myself. I'm (Your_Name), a recent M.Sc., Applied Data Science graduate eager to kickstart my career at (Company_Name). I have a solid foundation in Python, SQL, and Machine Learning, thanks to my coursework and practical projects completed during my studies.  I am a detail-oriented and analytical thinker who thrives in collaborative settings. During my academic journey, I have developed effective communication and time management skills, which I believe are essential for success in the workplace.  I am a dedicated and enthusiastic individual, always seeking opportunities to expand my knowledge and skills.  I am confident that my strong work ethic, willingness to learn, and passion will make me a valuable asset to your team. 2. How Are You?     Thank you for asking, I'm doing well. I'm excited to be here today for this interview and...

Must-Visit Websites for Everyday Solutions

 Must-Visit Websites for Everyday Solutions 1.   COCKATOO Cockatoo supports transcription in a wide range of languages, making it easy to  convert audio to text in your preferred language. 2.  ARCSEARCH The Browser Company helps you move even faster. It brings you everything you are searching for, quickly and without distractions. 3.  SIMPLIFIED       Create anything you imagine with AI on your side. 4.   ANDISEARCH A sleek tool called Andi combines the convenience of a chatbot with the credibility of a standard search setup—and it might just be the answer we need.

PYTHON PROGRAMMING

Python is an Open source, Free, High-level, Dynamic, and Interpreted programming language. It is cleanest and easy to learn a language in the programming world. It’s easy syntax improved the readability of the code. #learnpython #pythonprogramming #TECHFRIEDRICE BYTE CODE- Machines are not able to understand the human language or high-level languages thus whenever we write code in any programing language, machine used a compiler or an interpreter to convert the human-readable code to machine code. #learnpython #pythonprogramming We have many implementations of python programming in different languages. Some are: CPython Jython PyPy IronPython Stackless Python MicroPython #learnpython #pythonprogramming #TECHFRIEDRICE Applications of PYTHON 1. Web development 2. Data science 3. ML and AI 4. Game development 5. IOT 6. DESKTOP GUI APPLICATIONS 7. Web scrapping 8. cloud computing 9. NLP 10. Image and video processing #learnpython #pythonprogramming #TECHFRIEDRICE Garbage col...