00:00As an algo trader, you're often required to download live data from the NSE website.
00:04Things like bar copies, live option chain data, latest price info, historic data of indices, top gainers, losers, FIIDI activity,
00:10and much more.
00:11Although there are a few packages already available in GitHub, when I last checked them, some of them have stopped
00:15working, or some of them don't have all the functionality that we usually want.
00:19So I created a simple Python utility that does all of that, and I'm hoping that it could be of
00:23some use to you as well.
00:24In this video, I'll walk you through this Python utility and show you how to use it.
00:27You can download this code for free from our community website.
00:30So let's get started.
00:33If this is your first time here, welcome.
00:35My name is Vivek, and I'm a financially independent algo trader.
00:37This channel is all about building a community of algo traders.
00:39We discuss everything about algo trading using Python, building and back-testing trading strategies, market updates, and much more.
00:44Please do visit our community website, fabtrader.in.
00:47Also do check out my other YouTube channel, FabWealth, where I talk about my own financial independence journey and share
00:51tools, methods, and strategies that help me achieve my financial freedom.
00:56The Python code that I'm about to walk you through is available on our community website called fabtrader.in.
01:02So this is the website.
01:04And if you go into stories, you'd find the NSE utility Python blog available.
01:12And then if you browse through the entire code, this is available here.
01:15And you can just copy it and use it as you please.
01:17Now let's take a look at the code.
01:19The code has two parts.
01:21The first part is the actual utility, the Python utility, called nseutility.py.
01:26This basically has the class nseutils defined.
01:28And underneath that, you'll find the various methods, which does specific downloads from NSE websites.
01:34For example, pre-market info is all about extracting the pre-market data from the NSE website.
01:40And then the second file, which is the NSE download, is nothing but just a sample program that I wrote
01:45showing how you could actually use the NSE utils and download various data.
01:49So in order to use this utility, you would have to first install pandas, the dependency.
01:55The usual way of doing it is a pip install pandas, which I'm sure you're already aware of.
02:00And then once that is done, you can copy the code of the utility from the website, which I just
02:04showed you.
02:04I'll provide the link as well in the description.
02:07So copy that code into a file called nseutility.py.
02:10Place this file within your source root where you plan to use it.
02:13And then typically, this is how you would initiate the instance, which is basically you import the NSE utility, which
02:19is this one.
02:19And then you create an instance of the class that we built, which is the NSE utility, the file underneath
02:24it, the NSE utils class.
02:25And then NSE is the instance that you've created.
02:27Once that is done, you just invoke the methods that you use and then do various functions that you need.
02:34Here's a list of all the functions that currently this utility supports.
02:37Let's look at a few examples on which methods you could use.
02:39I don't plan to walk you through the entire list of functions here, but we'll take a few quick ones
02:44and then take you through it.
02:45The first one is called the getEquityFullList.
02:48And then when you run it, you typically get the list of all equity instruments with an NSE.
02:52And then if you don't really want the full details, but only want the list of the instruments, you can
02:58just say list only as true.
03:00And then when you run it, you typically get a list, a Python list containing just the symbols alone.
03:05So that's one way of doing it.
03:06And similarly, you can do the same for FNO symbols.
03:08If you want the pre-market info, again, if you run it and then give specific which instrument or the
03:14indices that you want, you'll download the entire table from NSE, which contains the pre-market data.
03:20Often in our strategies, we need the list of instruments contained within an index, right?
03:25For example, let's say you want all the symbols within the Nifty50, and this is one way of getting it.
03:29So get index details, and then when you run it, it provides me, again, a Python list containing all the
03:3450, you know, instruments within that.
03:37You can change it to, let's say, Nifty100, and then it will again provide the data accordingly.
03:42So the list of symbols and the indices are provided here.
03:45So you can do a quick reference if you want, anytime you want on this one.
03:50NSE holidays, typically two types of holidays, the clearing holidays and the trading holidays.
03:55So if you want the clearing holidays, you can just simply run this, and then it will give you a
03:59list of all the dates for this particular year where there are clearing holidays.
04:03And similarly, you can do it for the trading holidays as well.
04:05And then you can also check if current date, whatever date that you're currently running is also a clearing holiday
04:10or not.
04:10So if you run it for without an input parameter in it, it basically takes today's date as input and
04:15then checks and says if it's a clearing holiday.
04:17In this case, it's saying it's true.
04:18And then you can also provide a specific date, and then it will tell you whether that particular date is
04:22a holiday or not.
04:24It again gives you a return to either a true or a false.
04:26If you want all the information about a specific script, you can provide the symbol code, and it will give
04:32you an entire complete dump of all the information that you typically see on the NSE website.
04:35Write it every tab, the data contained within every tab is all collated and given to you in a JSON
04:41format.
04:43The price info function is one of very useful functions that I frequently use.
04:47This gets you the high, low, open, close, along with the upper circuit, the lower circuit details, including the web.
04:52So this is how you use very often.
04:54And then the next one is it provides you the 52 week high and low, not just the high and
04:58low value, but along also the date on which the high and low were hit, which is again, pretty useful
05:02information.
05:04And they get market depth, you get the standard five levels of market depth, and you run this.
05:10And then along with that, if you want the entire option chain downloaded for a symbol, you can use this
05:15function.
05:15Again, it's pretty useful.
05:16Or if you want just the futures symbols alone for a given symbol, you can run this and then get
05:22that data.
05:23The next set of functions are very, very useful and most frequently used by many traders, which is the bar
05:27copy download.
05:28In this, you can actually download all of those, just the delivery, bar copy, liquidity, indices, or FNO.
05:34So here in this case, let me just run it.
05:37Since it's a large file, I'm only printing just the top five rows of it.
05:41So this is how you provide a date and then it downloads the entire bar copy for that date.
05:46And in addition to that, you get some of these auxiliary items like the FIIDI activity, which is pretty useful.
05:51And then the top gainers and losers in each of these categories, for example, for Nifty, Bank Nifty, and XFD.
05:56So you can get a separate list containing the top gainers and losers.
06:00And one more useful item is getting the historic data for any indices.
06:03So I think right now it provides only the daily candles, but you can give a start in an end
06:08date and then provide the index that you want historic data for.
06:12And then it downloads the entire data onto a data frame.
06:16And with that, we've come to the end of the video.
06:18I hope you like this little utility and you make full use of it.
06:21And if there is any additional functionalities that you'd like me to add, please leave a comment and definitely include
06:25that to the code and publish it again.
06:27If you found some value in this video, please consider subscribing and liking the video.
06:30And I will see you soon in another video.
06:32Until then, take care.
06:32Bye.