Introducing nse2r

[This article was first published on Rsquared Academy Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We are excited to announce the nse2r package. NSE (National Stock Exchange) is the leading stock exchange of India, located in the city of Mumbai. While users can manually download data from NSE through a browser, importing this data into R becomes cumbersome. The nse2r R package implements the retrieval of data from NSE and aims to reduce the pre-processing steps needed in analyzing such data.

nse2r is inspired by and a port of the Python package nsetools. The authors and contributors for this R package are not affiliated with NSE and NSE does not offer support for this R package.

With nse2r, you can fetch the following data related to:

  • stocks
    • quote for a given stock
    • stock description
    • validate stock symbol/ticker
    • most actively traded stocks in a month
    • 52 week high/low
    • top gainers/losers for the last trading session
  • index
    • list of NSE indices
    • validate index symbol/ticker
    • quote for a given index
  • futures & options
    • top gainers/losers for the last trading session
  • pre open market data
    • nifty
    • nifty bank
  • indices advances & declines

Installation

# Install release version from CRAN
install.packages("nse2r")

# Install development version from GitHub
# install.packages("devtools")
devtools::install_github("rsquaredacademy/nse2r")

Usage

nse2r uses consistent prefix nse_ for easy tab completion.

  • nse_index_ for index
  • nse_stock_ for stocks
  • nse_fo_ for futures and options
  • nse_preopen_ for preopen data

Preprocessing

nse2r does basic data preprocessing which are listed below:

  • modify column data types from character to numeric and Date
  • modify column names
    • make them more descriptive
    • to snake_case from camelCase

Users can retain the names and format as returned by NSE using the clean_names argument and setting it to FALSE.

Quick Overview

Fetch Indices Quote

nse_index_quote()
## # A tibble: 55 x 4
##    index_name        last_traded_price  change percent_change
##    <chr>                         <dbl>   <dbl>          <dbl>
##  1 NIFTY 50 Pre Open           12328.   -27.1          -0.22 
##  2 NIFTY 50                    12371.    15.4           0.12 
##  3 NIFTY NEXT 50               29009.    30.2           0.1  
##  4 NIFTY100 LIQ 15              3794.   -11.2          -0.290
##  5 NIFTY BANK                  31661.  -193.           -0.6  
##  6 INDIA VIX                      14.4    0.24          1.67 
##  7 NIFTY 100                   12486.    15.2           0.12 
##  8 NIFTY 500                   10125.    15.4           0.15 
##  9 NIFTY MIDCAP 100            18037.    52.1           0.290
## 10 NIFTY MIDCAP 50              4978.     9.5           0.19 
## # ... with 45 more rows
# retain original column names as returned by NSE
nse_index_quote(clean_names = FALSE)
## # A tibble: 55 x 4
##    name              lastPrice  change pChange
##    <chr>                 <dbl>   <dbl>   <dbl>
##  1 NIFTY 50 Pre Open   12328.   -27.1   -0.22 
##  2 NIFTY 50            12371.    15.4    0.12 
##  3 NIFTY NEXT 50       29009.    30.2    0.1  
##  4 NIFTY100 LIQ 15      3794.   -11.2   -0.290
##  5 NIFTY BANK          31661.  -193.    -0.6  
##  6 INDIA VIX              14.4    0.24   1.67 
##  7 NIFTY 100           12486.    15.2    0.12 
##  8 NIFTY 500           10125.    15.4    0.15 
##  9 NIFTY MIDCAP 100    18037.    52.1    0.290
## 10 NIFTY MIDCAP 50      4978.     9.5    0.19 
## # ... with 45 more rows

Top gainers for the last trading session.

nse_stock_top_gainers()
## # A tibble: 10 x 12
##    symbol series last_corp_annou~ last_corp_annou~ open_price high_price
##    <chr>  <chr>  <date>           <chr>                 <dbl>      <dbl>
##  1 BHART~ EQ     2019-04-23       Rights 19:67 @ ~       475.       499.
##  2 RELIA~ EQ     2019-08-02       Annual General ~      1554.      1572.
##  3 HEROM~ EQ     2019-07-16       Annual General ~      2445       2475.
##  4 WIPRO  EQ     2020-01-24       Interim Dividen~       252        254.
##  5 M&M    EQ     2019-07-18       Annual General ~       567.       574.
##  6 NESTL~ EQ     2019-12-10       Interim Dividen~     15399      15600 
##  7 DRRED~ EQ     2019-07-15       Annual General ~      2944.      2990 
##  8 UPL    EQ     2019-07-02       Bonus 1:2              590.       597.
##  9 EICHE~ EQ     2019-07-24       Annual General ~     21509.     21770 
## 10 ULTRA~ EQ     2019-07-10       Annual General ~      4440       4522.
## # ... with 6 more variables: low_price <dbl>, last_traded_price <dbl>,
## #   prev_close_price <dbl>, percent_change <dbl>, traded_quantity <dbl>,
## #   turnover_in_lakhs <dbl>
# retain original column names as returned by NSE
nse_stock_top_gainers(clean_names = FALSE)
## # A tibble: 10 x 12
##    symbol series lastCorpAnnounc~ lastCorpAnnounc~ openPrice highPrice
##    <chr>  <chr>  <date>           <chr>                <dbl>     <dbl>
##  1 BHART~ EQ     2019-04-23       Rights 19:67 @ ~      475.      499.
##  2 RELIA~ EQ     2019-08-02       Annual General ~     1554.     1572.
##  3 HEROM~ EQ     2019-07-16       Annual General ~     2445      2475.
##  4 WIPRO  EQ     2020-01-24       Interim Dividen~      252       254.
##  5 M&M    EQ     2019-07-18       Annual General ~      567.      574.
##  6 NESTL~ EQ     2019-12-10       Interim Dividen~    15399     15600 
##  7 DRRED~ EQ     2019-07-15       Annual General ~     2944.     2990 
##  8 UPL    EQ     2019-07-02       Bonus 1:2             590.      597.
##  9 EICHE~ EQ     2019-07-24       Annual General ~    21509.    21770 
## 10 ULTRA~ EQ     2019-07-10       Annual General ~     4440      4522.
## # ... with 6 more variables: lowPrice <dbl>, ltp <dbl>,
## #   previousPrice <dbl>, netPrice <dbl>, tradedQuantity <dbl>,
## #   turnoverInLakhs <dbl>

Stocks that have touched their 52 week highs during the day

nse_stock_year_high()
## # A tibble: 55 x 10
##    symbol symbol_desc date       new_high   year last_traded_pri~ prev_high
##    <chr>  <chr>       <date>        <dbl>  <dbl>            <dbl>     <dbl>
##  1 AGCNET AGC Networ~ 2020-01-16    202.   202.             202.      193. 
##  2 ALKYL~ Alkyl Amin~ 2020-01-16   1300   1300             1297.     1300  
##  3 APOLL~ Apollo Pip~ 2020-01-16    408.   408.             403       399  
##  4 AUBANK AU Small F~ 2020-01-16    888    888              874.      885. 
##  5 AVANT~ Avanti Fee~ 2020-01-16    770    770              732.      756  
##  6 BAJAJ~ Bajaj Fins~ 2020-01-16   9681.  9681.            9656      9640  
##  7 BALKR~ Balkrishna~ 2020-01-16   1112.  1112.            1096      1107. 
##  8 BCP    B.C. Power~ 2020-01-16     20.2   20.2             19.0      20.0
##  9 BERGE~ Berger Pai~ 2020-01-16    556.   556.             554.      550  
## 10 BHART~ Bharti Air~ 2019-12-02    499.   499.             493.      486. 
## # ... with 45 more rows, and 3 more variables: prev_close <dbl>,
## #   change <dbl>, percent_change <dbl>
# retain original column names as returned by NSE
nse_stock_year_high(clean_names = FALSE)
## # A tibble: 55 x 10
##    symbol symbolDesc dt          value   year    ltp value_old   prev
##    <chr>  <chr>      <date>      <dbl>  <dbl>  <dbl>     <dbl>  <dbl>
##  1 AGCNET AGC Netwo~ 2020-01-16  202.   202.   202.      193.   193. 
##  2 ALKYL~ Alkyl Ami~ 2020-01-16 1300   1300   1297.     1300   1296. 
##  3 APOLL~ Apollo Pi~ 2020-01-16  408.   408.   403       399    395. 
##  4 AUBANK AU Small ~ 2020-01-16  888    888    874.      885.   877. 
##  5 AVANT~ Avanti Fe~ 2020-01-16  770    770    732.      756    746. 
##  6 BAJAJ~ Bajaj Fin~ 2020-01-16 9681.  9681.  9656      9640   9622. 
##  7 BALKR~ Balkrishn~ 2020-01-16 1112.  1112.  1096      1107.  1103. 
##  8 BCP    B.C. Powe~ 2020-01-16   20.2   20.2   19.0      20.0   19.6
##  9 BERGE~ Berger Pa~ 2020-01-16  556.   556.   554.      550    547. 
## 10 BHART~ Bharti Ai~ 2019-12-02  499.   499.   493.      486.   474. 
## # ... with 45 more rows, and 2 more variables: change <dbl>, pChange <dbl>

Learning More

Feedback

All feedback is welcome. Issues (bugs and feature requests) can be posted to github tracker. For help with code or other related questions, feel free to reach out to us at .

To leave a comment for the author, please follow the link and comment on their blog: Rsquared Academy Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)