In this post we will discuss how to fetch real-time Indian Stock Market Data from the National Stock Exchange of India (NSE) using R. It includes data for equities, indices and derivatives (futures and options) of the capital market. Stock Market Data can be used for analysing movements in the market and building trading strategies accordingly.
We will use public REST APIs of NSE. These APIs are not officially released by NSE. This post is for information, private and non-commercial purposes.
I built R package called nser
for the same which includes various functions capturing stock prices, financials and events in the market.
You can install the package from github using the command below
# install.packages("remotes") remotes::install_github("deepanshu88/nser")
Once you are done with the installation, load the package.
library(nser)
Check if market is opened or not
It returns whether market is opened for trading by different market types (capital, currency, commodity and Debt). If closed, it also returns the next trading date.MarketStatus()
+----------------+--------------+-----------+-------+---------+-----------+---------------+--------------------------+
| market | marketStatus | tradeDate | index | last | variation | percentChange | marketStatusMessage |
+----------------+--------------+-----------+-------+---------+-----------+---------------+--------------------------+
| Capital Market | Closed | 5-Aug-21 |NIFTY50| 16258.8 | 128.05 | 0.79 | Normal Market has Closed |
| Currency | Closed | 5-Aug-21 | | | | | Market is Closed |
| Commodity | Open | 4-Aug-21 | | | | | Market is Open |
| Debt | Closed | 5-Aug-21 | | | | | Market is Closed |
+----------------+--------------+-----------+-------+---------+-----------+---------------+--------------------------+
Top Gainers of the Day
It gives you the list of stocks which are top gainers of the latest trading day. Stocks are ordered based on percentage change column namedpChange
. You will also find other key metrics like year high and low, percentage change in the past one month and year.
df <- TopGainers()
[1] "symbol" "identifier" "series"
[4] "open" "dayHigh" "dayLow"
[7] "lastPrice" "previousClose" "change"
[10] "pChange" "totalTradedVolume" "totalTradedValue"
[13] "lastUpdateTime" "yearHigh" "yearLow"
[16] "nearWKH" "nearWKL" "perChange365d"
[19] "date365dAgo" "chart365dPath" "date30dAgo"
[22] "perChange30d" "chart30dPath" "chartTodayPath"
[25] "meta.symbol" "meta.companyName" "meta.industry"
[28] "meta.activeSeries" "meta.debtSeries" "meta.tempSuspendedSeries"
[31] "meta.isFNOSec" "meta.isCASec" "meta.isSLBSec"
[34] "meta.isDebtSec" "meta.isSuspended" "meta.isETFSec"
[37] "meta.isDelisted" "meta.isin
Top Losers of the Day
This is very similar to the previous section. Here we are trying to find out the top losers of the latest trading day.df <- TopLosers()
Most Active Stocks
In this section we are extracting information of most active stocks. By default active stocks are identified based on volumes in the latest trading day. You can also change it to value. See example below.df <- MostActive(OrderBy = "value")
Current Price of Stock
To fetch the live stock price, you can useGetQuote('symbol')
function. Make sure to use NSE symbols only (not BSE). NSE symbols can be found from official NSE website. You will not only get current price but also low and high of the day with PE value of the symbol along with sector.
QuoteDf <- GetQuote('acc')
Historical Prices of Stock
To understand the technicals of a stock, it's important to have atleast 1 year historical data. You can do it throughHistorical(Symbol, StartDate, EndDate)
function. Make sure dates must be in DD-MM-YYYY format.
HistoricalDf <- Historical('ACC', "01-01-2020", "24-07-2021")
Get Derivatives Details
You can pull derivatives information of both stock and nifty indices. In theType
you can specify either future or options. By default it is options.
QuoteDf <- GetDerivativeQuote('banknifty') QuoteDf <- GetDerivativeQuote('banknifty', Type = "future")
Historical Data of Derivatives
You can pull historical data of derivatives viaHistoricalDerivative( )
function. Instrument Type can be either "options" or "futures". Also it is important to have dates in DD-MM-YYYY format.
HistoricalDf <- HistoricalDerivate(Symbol = 'ITC', StartDate = "01-07-2021", EndDate = "24-07-2021", InstrumentType = "options", ExpiryDate = "29-07-2021")
Get Current Price of 59 Indices
In NSE there are 59 different indices. You can get prices (currently traded, low and high) of them usingOutDf <- Indices()
function. See the complete list below.
NIFTY 50 NIFTY NEXT 50 NIFTY 100 NIFTY 200 NIFTY 500 NIFTY MIDCAP 50 NIFTY MIDCAP 100 NIFTY SMALLCAP 100 INDIA VIX NIFTY MIDCAP 150 NIFTY SMALLCAP 50 NIFTY SMALLCAP 250 NIFTY MIDSMALLCAP 400 NIFTY BANK NIFTY AUTO NIFTY FINANCIAL SERVICES NIFTY FINANCIAL SERVICES 25/50 NIFTY FMCG NIFTY IT NIFTY MEDIA NIFTY METAL NIFTY PHARMA NIFTY PSU BANK NIFTY PRIVATE BANK NIFTY REALTY NIFTY DIVIDEND OPPORTUNITIES 50 NIFTY GROWTH SECTORS 15 NIFTY100 QUALITY 30 NIFTY50 VALUE 20 NIFTY50 TR 2X LEVERAGE NIFTY50 PR 2X LEVERAGE NIFTY50 TR 1X INVERSE NIFTY50 PR 1X INVERSE NIFTY50 DIVIDEND POINTS NIFTY ALPHA 50 NIFTY50 EQUAL WEIGHT NIFTY100 EQUAL WEIGHT NIFTY100 LOW VOLATILITY 30 NIFTY200 QUALITY 30 NIFTY ALPHA LOW-VOLATILITY 30 NIFTY200 MOMENTUM 30 NIFTY COMMODITIES NIFTY INDIA CONSUMPTION NIFTY CPSE NIFTY ENERGY NIFTY INFRASTRUCTURE NIFTY100 LIQUID 15 NIFTY MIDCAP LIQUID 15 NIFTY MNC NIFTY PSE NIFTY SERVICES SECTOR NIFTY100 ESG SECTOR LEADERS NIFTY 8-13 YR G-SEC NIFTY 10 YR BENCHMARK G-SEC NIFTY 10 YR BENCHMARK G-SEC (CLEAN PRICE) NIFTY 4-8 YR G-SEC INDEX NIFTY 11-15 YR G-SEC INDEX NIFTY 15 YR AND ABOVE G-SEC INDEX NIFTY COMPOSITE G-SEC INDEX
Option Chain
Options chain records all available options contracts for a stock or nifty index. It shows calls, puts, strike price along with their expiration. You can use this functionOptionChain('symbol', Expiry = "latest")
. By default it extracts option contracts of all the future expiry. You can use Expiry
argument to select the latest expiry.
OutDf <- OptionChain('ACC') OutDf <- OptionChain('ACC', Expiry = "latest")
List of stocks in Future and Options
As of 8th August, 2021, there are 162 stocks traded in FnO. This number will change in coming months as more companies will be listed publicly.OutDf <- SecuritiesFO()
Current Price of Nifty 50 Stocks
You can use this functionOutDf <- NIFTY50()
to get stock price details of Nifty 50 companies.
IndusInd Bank Limited Adani Ports and Special Economic Zone Limited Indian Oil Corporation Limited Tech Mahindra Limited TATA CONSUMER PRODUCTS LIMITED Bharti Airtel Limited SBI Life Insurance Company Limited NTPC Limited Maruti Suzuki India Limited Tata Consultancy Services Limited Bajaj Auto Limited Coal India Limited Hindustan Unilever Limited Bharat Petroleum Corporation Limited Hero MotoCorp Limited HDFC Life Insurance Company Limited HDFC Bank Limited Kotak Mahindra Bank Limited Eicher Motors Limited Tata Motors Limited Bajaj Finserv Limited Power Grid Corporation of India Limited Divi's Laboratories Limited UPL Limited Infosys Limited Sun Pharmaceutical Industries Limited Britannia Industries Limited Oil & Natural Gas Corporation Limited Wipro Limited Dr. Reddy's Laboratories Limited ITC Limited Mahindra & Mahindra Limited ICICI Bank Limited Bajaj Finance Limited Hindalco Industries Limited Grasim Industries Limited Titan Company Limited Asian Paints Limited Axis Bank Limited Nestle India Limited JSW Steel Limited State Bank of India HCL Technologies Limited Larsen & Toubro Limited Housing Development Finance Corporation Limited TATA Steel Limited UltraTech Cement Limited SHREE CEMENT LIMITED Reliance Industries Limited Cipla Limited
Quarterly Income Statement
Prior to investing it is important to understand financials of the organisation. It helps to assess if company is profitable and growing. You can use quarterly financials to calculate quarterly year on year growth, can also compare with the previous quarter results. You need to specify symbol name in the function.df <- FinancialResults(Symbol = "ACC")
Upcoming Events
While trading it is crucial to know important dates or events, let's say when will quarterly result be announced. OR announcement of dividend. It can also be informing exchange about reasons for sudden price movements or insider trading.df <- Events()
Past Filing Information of Financials
Incase you are interested to see the past filing of financials details, you can use the functionFilingInfo(period = "quarterly")
. By default period is quarterly, you can change it to annual.
df <- FilingInfo()
Holidays Calendar
Incase you are interested to see the past filing of financials details, you can use the functionHolidays(type="trading")
. By default period is trading, you can change it to clearing. List of holidays may vary depending on the product. You can know more about the product from NSE website.
df <- Holidays()
Product tradingDate weekDay description Sr_no
1 CBM 26-Jan-2021 Tuesday Republic Day 1
2 CBM 19-Feb-2021 Friday Chatrapati Shivaji Jayanti 2
3 CBM 11-Mar-2021 Thursday Mahashivratri 3
4 CBM 29-Mar-2021 Monday Holi 4
5 CBM 01-Apr-2021 Thursday Annual Bank Closing 5
6 CBM 02-Apr-2021 Friday Good Friday 6
7 CBM 13-Apr-2021 Tuesday Gudi Padwa 7
8 CBM 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 8
9 CBM 21-Apr-2021 Wednesday Ram Navami 9
10 CBM 25-Apr-2021 Sunday Mahavir Jayanti 10
11 CBM 01-May-2021 Saturday Maharashtra Day 11
12 CBM 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 12
13 CBM 26-May-2021 Wednesday Buddha Pournima 13
14 CBM 21-Jul-2021 Wednesday Bakri Id 14
15 CBM 15-Aug-2021 Sunday Independence Day 15
16 CBM 16-Aug-2021 Monday Parsi New Year 16
17 CBM 19-Aug-2021 Thursday Moharram 17
18 CBM 10-Sep-2021 Friday Ganesh Chaturthi 18
19 CBM 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 19
20 CBM 15-Oct-2021 Friday Dussehra 20
21 CBM 19-Oct-2021 Tuesday Id-E-Milad 21
22 CBM 04-Nov-2021 Thursday Diwali-Laxmi Pujan 22
23 CBM 05-Nov-2021 Friday Diwali-Balipratipada 23
24 CBM 19-Nov-2021 Friday Gurunanak Jayanti 24
25 CBM 25-Dec-2021 Saturday Christmas 25
26 CD 26-Jan-2021 Tuesday Republic Day 1
27 CD 19-Feb-2021 Friday Chatrapati Shivaji Jayanti 2
28 CD 11-Mar-2021 Thursday Mahashivratri 3
29 CD 29-Mar-2021 Monday Holi 4
30 CD 01-Apr-2021 Thursday Annual Bank Closing 5
31 CD 02-Apr-2021 Friday Good Friday 6
32 CD 13-Apr-2021 Tuesday Gudi Padwa 7
33 CD 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 8
34 CD 21-Apr-2021 Wednesday Ram Navami 9
35 CD 25-Apr-2021 Sunday Mahavir Jayanti 10
36 CD 01-May-2021 Saturday Maharashtra Day 11
37 CD 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 12
38 CD 26-May-2021 Wednesday Buddha Pournima 13
39 CD 21-Jul-2021 Wednesday Bakri Id 14
40 CD 15-Aug-2021 Sunday Independence Day 15
41 CD 16-Aug-2021 Monday Parsi New Year 16
42 CD 19-Aug-2021 Thursday Moharram 17
43 CD 10-Sep-2021 Friday Ganesh Chaturthi 18
44 CD 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 19
45 CD 15-Oct-2021 Friday Dussehra 20
46 CD 19-Oct-2021 Tuesday Id-E-Milad 21
47 CD 05-Nov-2021 Friday Diwali-Balipratipada 22
48 CD 19-Nov-2021 Friday Gurunanak Jayanti 23
49 CD 25-Dec-2021 Saturday Christmas 24
50 CM 26-Jan-2021 Tuesday Republic Day 1
51 CM 11-Mar-2021 Thursday Mahashivratri 2
52 CM 29-Mar-2021 Monday Holi 3
53 CM 02-Apr-2021 Friday Good Friday 4
54 CM 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 5
55 CM 21-Apr-2021 Wednesday Ram Navami 6
56 CM 25-Apr-2021 Sunday Mahavir Jayanti 7
57 CM 01-May-2021 Saturday Maharashtra Day 8
58 CM 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 9
59 CM 21-Jul-2021 Wednesday Bakri Id 10
60 CM 15-Aug-2021 Sunday Independence Day 11
61 CM 19-Aug-2021 Thursday Moharram 12
62 CM 10-Sep-2021 Friday Ganesh Chaturthi 13
63 CM 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 14
64 CM 15-Oct-2021 Friday Dussehra 15
65 CM 05-Nov-2021 Friday Diwali-Balipratipada 16
66 CM 19-Nov-2021 Friday Gurunanak Jayanti 17
67 CM 25-Dec-2021 Saturday Christmas 18
68 CMOT 26-Jan-2021 Tuesday Republic Day 1
69 CMOT 11-Mar-2021 Thursday Mahashivratri 2
70 CMOT 29-Mar-2021 Monday Holi 3
71 CMOT 02-Apr-2021 Friday Good Friday 4
72 CMOT 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 5
73 CMOT 21-Apr-2021 Wednesday Ram Navami 6
74 CMOT 25-Apr-2021 Sunday Mahavir Jayanti 7
75 CMOT 01-May-2021 Saturday Maharashtra Day 8
76 CMOT 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 9
77 CMOT 21-Jul-2021 Wednesday Bakri Id 10
78 CMOT 15-Aug-2021 Sunday Independence Day 11
79 CMOT 19-Aug-2021 Thursday Moharram 12
80 CMOT 10-Sep-2021 Friday Ganesh Chaturthi 13
81 CMOT 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 14
82 CMOT 15-Oct-2021 Friday Dussehra 15
83 CMOT 05-Nov-2021 Friday Diwali-Balipratipada 16
84 CMOT 19-Nov-2021 Friday Gurunanak Jayanti 17
85 CMOT 25-Dec-2021 Saturday Christmas 18
86 COM 26-Jan-2021 Tuesday Republic Day 1
87 COM 02-Apr-2021 Friday Good Friday 2
88 COM 25-Apr-2021 Sunday Mahavir Jayanti 3
89 COM 01-May-2021 Saturday Maharashtra Day 4
90 COM 15-Aug-2021 Sunday Independence Day 5
91 COM 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 6
92 COM 25-Dec-2021 Saturday Christmas 7
93 FO 26-Jan-2021 Tuesday Republic Day 1
94 FO 11-Mar-2021 Thursday Mahashivratri 2
95 FO 29-Mar-2021 Monday Holi 3
96 FO 02-Apr-2021 Friday Good Friday 4
97 FO 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 5
98 FO 21-Apr-2021 Wednesday Ram Navami 6
99 FO 25-Apr-2021 Sunday Mahavir Jayanti 7
100 FO 01-May-2021 Saturday Maharashtra Day 8
101 FO 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 9
102 FO 21-Jul-2021 Wednesday Bakri Id 10
103 FO 15-Aug-2021 Sunday Independence Day 11
104 FO 19-Aug-2021 Thursday Moharram 12
105 FO 10-Sep-2021 Friday Ganesh Chaturthi 13
106 FO 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 14
107 FO 15-Oct-2021 Friday Dussehra 15
108 FO 05-Nov-2021 Friday Diwali-Balipratipada 16
109 FO 19-Nov-2021 Friday Gurunanak Jayanti 17
110 FO 25-Dec-2021 Saturday Christmas 18
111 IRD 26-Jan-2021 Tuesday Republic Day 1
112 IRD 19-Feb-2021 Friday Chatrapati Shivaji Jayanti 2
113 IRD 11-Mar-2021 Thursday Mahashivratri 3
114 IRD 29-Mar-2021 Monday Holi 4
115 IRD 01-Apr-2021 Thursday Annual Bank Closing 5
116 IRD 02-Apr-2021 Friday Good Friday 6
117 IRD 13-Apr-2021 Tuesday Gudi Padwa 7
118 IRD 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 8
119 IRD 21-Apr-2021 Wednesday Ram Navami 9
120 IRD 25-Apr-2021 Sunday Mahavir Jayanti 10
121 IRD 01-May-2021 Saturday Maharashtra Day 11
122 IRD 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 12
123 IRD 26-May-2021 Wednesday Buddha Pournima 13
124 IRD 21-Jul-2021 Wednesday Bakri Id 14
125 IRD 15-Aug-2021 Sunday Independence Day 15
126 IRD 16-Aug-2021 Monday Parsi New Year 16
127 IRD 19-Aug-2021 Thursday Moharram 17
128 IRD 10-Sep-2021 Friday Ganesh Chaturthi 18
129 IRD 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 19
130 IRD 15-Oct-2021 Friday Dussehra 20
131 IRD 19-Oct-2021 Tuesday Id-E-Milad 21
132 IRD 05-Nov-2021 Friday Diwali-Balipratipada 22
133 IRD 19-Nov-2021 Friday Gurunanak Jayanti 23
134 IRD 25-Dec-2021 Saturday Christmas 24
135 MF 26-Jan-2021 Tuesday Republic Day 1
136 MF 11-Mar-2021 Thursday Mahashivratri 2
137 MF 29-Mar-2021 Monday Holi 3
138 MF 02-Apr-2021 Friday Good Friday 4
139 MF 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 5
140 MF 21-Apr-2021 Wednesday Ram Navami 6
141 MF 25-Apr-2021 Sunday Mahavir Jayanti 7
142 MF 01-May-2021 Saturday Maharashtra Day 8
143 MF 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 9
144 MF 21-Jul-2021 Wednesday Bakri Id 10
145 MF 15-Aug-2021 Sunday Independence Day 11
146 MF 19-Aug-2021 Thursday Moharram 12
147 MF 10-Sep-2021 Friday Ganesh Chaturthi 13
148 MF 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 14
149 MF 15-Oct-2021 Friday Dussehra 15
150 MF 04-Nov-2021 Thursday Diwali-Laxmi Pujan 16
151 MF 05-Nov-2021 Friday Diwali-Balipratipada 17
152 MF 19-Nov-2021 Friday Gurunanak Jayanti 18
153 MF 25-Dec-2021 Saturday Christmas 19
154 NDM 26-Jan-2021 Tuesday Republic Day 1
155 NDM 19-Feb-2021 Friday Chatrapati Shivaji Jayanti 2
156 NDM 11-Mar-2021 Thursday Mahashivratri 3
157 NDM 29-Mar-2021 Monday Holi 4
158 NDM 01-Apr-2021 Thursday Annual Bank Closing 5
159 NDM 02-Apr-2021 Friday Good Friday 6
160 NDM 13-Apr-2021 Tuesday Gudi Padwa 7
161 NDM 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 8
162 NDM 21-Apr-2021 Wednesday Ram Navami 9
163 NDM 25-Apr-2021 Sunday Mahavir Jayanti 10
164 NDM 01-May-2021 Saturday Maharashtra Day 11
165 NDM 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 12
166 NDM 26-May-2021 Wednesday Buddha Pournima 13
167 NDM 21-Jul-2021 Wednesday Bakri Id 14
168 NDM 15-Aug-2021 Sunday Independence Day 15
169 NDM 16-Aug-2021 Monday Parsi New Year 16
170 NDM 19-Aug-2021 Thursday Moharram 17
171 NDM 10-Sep-2021 Friday Ganesh Chaturthi 18
172 NDM 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 19
173 NDM 15-Oct-2021 Friday Dussehra 20
174 NDM 19-Oct-2021 Tuesday Id-E-Milad 21
175 NDM 04-Nov-2021 Thursday Diwali-Laxmi Pujan 22
176 NDM 05-Nov-2021 Friday Diwali-Balipratipada 23
177 NDM 19-Nov-2021 Friday Gurunanak Jayanti 24
178 NDM 25-Dec-2021 Saturday Christmas 25
179 NTRP 26-Jan-2021 Tuesday Republic Day 1
180 NTRP 19-Feb-2021 Friday Chatrapati Shivaji Jayanti 2
181 NTRP 11-Mar-2021 Thursday Mahashivratri 3
182 NTRP 29-Mar-2021 Monday Holi 4
183 NTRP 01-Apr-2021 Thursday Annual Bank Closing 5
184 NTRP 02-Apr-2021 Friday Good Friday 6
185 NTRP 13-Apr-2021 Tuesday Gudi Padwa 7
186 NTRP 14-Apr-2021 Wednesday Dr.Baba Saheb Ambedkar Jayanti 8
187 NTRP 21-Apr-2021 Wednesday Ram Navami 9
188 NTRP 25-Apr-2021 Sunday Mahavir Jayanti 10
189 NTRP 01-May-2021 Saturday Maharashtra Day 11
190 NTRP 13-May-2021 Thursday Id-Ul-Fitr (Ramzan ID) 12
191 NTRP 26-May-2021 Wednesday Buddha Pournima 13
192 NTRP 21-Jul-2021 Wednesday Bakri Id 14
193 NTRP 15-Aug-2021 Sunday Independence Day 15
194 NTRP 16-Aug-2021 Monday Parsi New Year 16
195 NTRP 19-Aug-2021 Thursday Moharram 17
196 NTRP 10-Sep-2021 Friday Ganesh Chaturthi 18
197 NTRP 02-Oct-2021 Saturday Mahatma Gandhi Jayanti 19
198 NTRP 15-Oct-2021 Friday Dussehra 20
199 NTRP 19-Oct-2021 Tuesday Id-E-Milad 21
200 NTRP 04-Nov-2021 Thursday Diwali-Laxmi Pujan 22
[ reached 'max' / getOption("max.print") -- omitted 21 rows ]
did we get the real time data using this?
ReplyDeleteUse Power Query of Excel to fetch live data of option chain for Nifty and Bank Nifty together
ReplyDelete