Are you looking for stocks historical data for automated trading bot, or custom analysis and want to learn how to fetch stock’s historical data from NSE India in Python. Then you are at the right place. Just keep reading till the end and you will be able to get your desired data from NSE India yourself.
What is NSE India?
NSE India is National Stock Exchange of India Limited is one of the leading stock exchanges in India, based in Mumbai. NSE is under the ownership of various financial institutions such as banks and insurance companies. Its official website is www.nseindia.com. If you want to get any sort of stock data for your trading needs, which is being traded in NSE platform. Then it is the place for you to go. Apart from NSE, some stocks are only being traded in BSE platform.
Prerequisites
To write the script in python and execute it on your computer, you will have to have Python installed. If you want to learn how to install python you can visit on this URL “How to Install Python on Windows 10“. With python installed you should also have an active Internet connection to fetch data from NSE website.
Complete Python Code
First lets create a file on your computer, I am creating it on my Desktop with name “nse.py” and open it with your favorite IDE. Now copy the given below code and paste it in the file
Python Code Explanation
Now lets understand what is happening in the code.
- Line number 1, 2, 3, 4 is importing libraries required for our script. Library “requests” is the main requirement for this code, we will use this library to send request to NSE India and fetch the response.
- Line #6 is where we declare our class name “NSE”. Inside this class we will write our entire code to fetch historical data from NSE India. I am creating class because later we will add more functionality to this class. And we will use nse.py as an API to interact with NSE India.
- Line #7 is the definition of constructor for NSE class. In this constructor we are declaring some private variables. For example on line #8 base_url, on line #14 for timeout of http request and on line #15 to hold cookies data.
- Line #9-13 is dictionary used for header to be set in our http request to the NSE website. Most important header is “User-Agent”.
- Line #17-23 we are creating a private function to fetch cookies data from the NSE website. Cookies data is necessary to request API data from the website.
- Now finally on line #25 we are declaring function to fetch historical data from NSE India. In this function we are providing 4 parameters, first for symbol, second for segment, third for the date from when we fetch data and finally fourth for the date to fetch data till.
- Line #27, we are creating API request URL with data parameters we are providing to the function.
- Line #28-30 we are trying to submit request to the NSE website and check if getting valid response. If non 200 http response code we are retrying with renewed cookies.
- Line #32, we are converting http data into CSV using BytesIO and converting it into pandas dataframe.
- Line #33-34, now we are changing the data columns according to our requirements.
- Finally we are returning the dataframe from the function. In case any exception occur in the code, this function will return None.
- Line #40-45, these are the line just for testing purpose, where we are testing our NSE module.
Conclusion
Finally if you have reached the end of this post (how to fetch stock’s historical data from NSE India in Python), as a result you should now be able to fetch the historical data from NSE India website. Now you can use nse.py file as an API in your program and import it where you want to add. If you want to learn more about trading, then stay tune to tutlogic.com or follow my channel on youtube.com/@tutlogic.