NSE Tools In Python
About NSE
NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was organized as the first dematerialized electronic exchange in the country.
The nsetools is defined as the library that is used for getting real-time data from the National Stock Exchange of India. It is used in different kinds of projects which need to fetches live quotes for a specific stock or indexes and developing large data sets for performing data analytics.
Command-line interface applications can be also built for providing the live market details at a very fast speed and it can be much faster than any browser. The data accuracy is perfect as provided on http://www.nseindia.com.
We can easily install nsetools by using the following command
pip install nsetools
Example of creating a Nse object
In this example, we are creating a Nse object which returns the driver class for National Stock Exchange.
Example –
# import nse from nse tools
from nsetools import Nse
# Now, creating a Nse object
nse = Nse()
# printing the Nse object
print(nse)
Output:
Driver Class for National Stock Exchange (NSE)
Example of getting information of a company
Here, we are going to retrieve the information of a specific company and display the buying price of company shares.
# Code for getting information
# of company shares
# import nse from nse tools
from nsetools import Nse
# creating a Nse object
nse = Nse()
# getting quote of the HDFC
quote = nse.get_quote('hdfc')
# printing the name of company
print(quote['companyName'])
# printing the buying price of company shares
print("Buy Price : " + str(quote['buyPrice1']))
Output:
HDFC
Buy Price: 2818.10
Example of getting average trade price
We can also get the average price of any company. In this example we are trying to get the average share price of a company.
Example –
# Code for getting information
# of company shares
# import nse from nse tools
from nsetools import Nse
# creating a Nse object
nse = Nse()
# getting quote of the HDFC
quote = nse.get_quote('hdfc')
# printing company name
print(quote['companyName'])
# printing the average price of company shares
print("Average Price : " + str(quote['averagePrice']))
Output:
HDFC
Average Price: 2833.94
Conclusion
In the above article, we have studied the nsetools module in Python. Also, we have seen how to get the information of a company shares.