How to get live stock prices of companies listed on the NSE using Python.

Shounak Lohokare
2 min readNov 23, 2020

--

There are multiple ways to get the live stock price of a company listed on the National Stock Exchange.

One of the easiest way to get it it is using the yahoo_fin package. The yahoo_fin package is a Python 3 package that scrapes stock price data from Yahoo! Finance website.

Installing

The yahoo_fin package can be installed using the pip command:-

pip install yahoo_fin

yahoo_fin requires following packages to be installed:-

ftplib
io
json
pandas
requests
requests_html

excluding requests_html these prerequisites come pre-installed in the Anaconda environment.

requests_html can be installed with the pip command:-

pip install requests_html

Extracting live stock price

There are two modules in the yahoo_fin package, stock_info and options. We are going to use the stock_info module to get the live stock price.

Step 1:- Import stock_info module from yahoo_fin package.

Step 2:- Insert the ticker symbol of a stock followed by a suffix “.NS” as a parameter in the function get_live_price().

In the above example, the ticker symbol of Larsen and Toubro Limited followed by suffix “.NS” is given as a parameter to the function get_live_price()

To iterate, the parameter of get_live_price() function must be:-

valid_stock_ticker_symbol.NS

The “.NS” suffix represents the National Stock Exchange.

Some other examples of getting live stock price using the get_live_price() function.

We can also get the live stock prices of companies which are listed on NASDAQ using the get_live_price() function.

Note how there’s no suffix after the stock ticker symbol.

While getting the prices of companies listed on NASDAQ a suffix is not needed.

Additionally, We can also get balance sheets, cash flow, earnings etc. of a company and other related information like prices of commodities using the yahoo_fin package.

--

--

Shounak Lohokare
Shounak Lohokare

Written by Shounak Lohokare

pretending to teach is one of the best way to learn.

No responses yet