Introducing nse2r

[This article was first published on Rsquared Academy Blog - Explore Discover Learn, 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           10073.   26.0            0.26
##  2 NIFTY 50                    10116.   69.5            0.69
##  3 NIFTY NEXT 50               25154.   -8.9           -0.04
##  4 NIFTY100 LIQ 15              2587    17.3            0.67
##  5 NIFTY BANK                  21100.  375.             1.81
##  6 INDIA VIX                      29.4  -0.77          -2.54
##  7 NIFTY 100                   10292.   60.4            0.59
##  8 NIFTY 500                    8293.   55.2            0.67
##  9 NIFTY MIDCAP 100            14342   173.             1.22
## 10 NIFTY MIDCAP 50              3993.   52              1.32
## # ... 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   10073.   26.0     0.26
##  2 NIFTY 50            10116.   69.5     0.69
##  3 NIFTY NEXT 50       25154.   -8.9    -0.04
##  4 NIFTY100 LIQ 15      2587    17.3     0.67
##  5 NIFTY BANK          21100.  375.      1.81
##  6 INDIA VIX              29.4  -0.77   -2.54
##  7 NIFTY 100           10292.   60.4     0.59
##  8 NIFTY 500            8293.   55.2     0.67
##  9 NIFTY MIDCAP 100    14342   173.      1.22
## 10 NIFTY MIDCAP 50      3993.   52       1.32
## # ... 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 INDUS~ EQ     2019-08-08       Annual General ~       464        510.
##  2 HINDA~ EQ     2019-08-14       Annual General ~       144.       148 
##  3 AXISB~ EQ     2019-07-04       Annual General ~       422.       433.
##  4 SHREE~ EQ     2020-02-24       Interim Dividen~     21171.     21611.
##  5 KOTAK~ EQ     2019-07-12       Annual General ~      1304.      1344.
##  6 RELIA~ EQ     2020-05-13       Rights 1:15 @ P~      1543.      1580.
##  7 SBIN   EQ     2018-06-15       Annual General ~       185.       189 
##  8 ICICI~ EQ     2019-07-22       Annual General ~       349.       357.
##  9 HDFC   EQ     2020-07-09       Annual General ~      1784       1820 
## 10 TCS    EQ     2020-06-03       Dividend - Rs 6~      2077.      2132 
## # ... 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 lowPrice
##    <chr>  <chr>  <date>           <chr>                <dbl>     <dbl>    <dbl>
##  1 INDUS~ EQ     2019-08-08       Annual General ~      464       510.     463.
##  2 HINDA~ EQ     2019-08-14       Annual General ~      144.      148      142.
##  3 AXISB~ EQ     2019-07-04       Annual General ~      422.      433.     415.
##  4 SHREE~ EQ     2020-02-24       Interim Dividen~    21171.    21611.   21150.
##  5 KOTAK~ EQ     2019-07-12       Annual General ~     1304.     1344.    1304.
##  6 RELIA~ EQ     2020-05-13       Rights 1:15 @ P~     1543.     1580.    1540.
##  7 SBIN   EQ     2018-06-15       Annual General ~      185.      189      184.
##  8 ICICI~ EQ     2019-07-22       Annual General ~      349.      357.     345.
##  9 HDFC   EQ     2020-07-09       Annual General ~     1784      1820     1782 
## 10 TCS    EQ     2020-06-03       Dividend - Rs 6~     2077.     2132     2074.
## # ... with 5 more variables: 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: 34 x 10
##    symbol symbol_desc date       new_high   year last_traded_pri~ prev_high
##    <chr>  <chr>       <date>        <dbl>  <dbl>            <dbl>     <dbl>
##  1 ADANI~ Adani Gree~ 2020-06-09   328.   328.             328.      313.  
##  2 ALCHEM Alchemist ~ 2020-06-09     6.95   6.95             6.95      6.65
##  3 ALOKI~ Alok Indus~ 2020-06-09    25.8   25.8             25.8      24.6 
##  4 ANDHR~ Andhra Cem~ 2020-06-09     5.35   5.35             5.35      5.1 
##  5 AUROP~ Aurobindo ~ 2020-06-04   800.   800.             796.      791   
##  6 BCG    Brightcom ~ 2020-06-09     9.95   9.95             9.95      9.5 
##  7 BIRLA~ Birla Tyre~ 2020-06-09    23.5   23.5             23.5      22.4 
##  8 COMPU~ Compucom S~ 2020-06-09    10.8   10.8             10.8      10.6 
##  9 DAAWAT LT Foods L~ 2020-06-09    37.8   37.8             37.2      37   
## 10 DHANU~ Dhanuka Ag~ 2020-06-09   653.   653.             635.      620   
## # ... with 24 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: 34 x 10
##    symbol symbolDesc dt          value   year    ltp value_old   prev change
##    <chr>  <chr>      <date>      <dbl>  <dbl>  <dbl>     <dbl>  <dbl>  <dbl>
##  1 ADANI~ Adani Gre~ 2020-06-09 328.   328.   328.      313.   313.    15.6 
##  2 ALCHEM Alchemist~ 2020-06-09   6.95   6.95   6.95      6.65   6.65   0.3 
##  3 ALOKI~ Alok Indu~ 2020-06-09  25.8   25.8   25.8      24.6   24.6    1.2 
##  4 ANDHR~ Andhra Ce~ 2020-06-09   5.35   5.35   5.35      5.1    5.1    0.25
##  5 AUROP~ Aurobindo~ 2020-06-04 800.   800.   796.      791    775.    21.1 
##  6 BCG    Brightcom~ 2020-06-09   9.95   9.95   9.95      9.5    9.5    0.45
##  7 BIRLA~ Birla Tyr~ 2020-06-09  23.5   23.5   23.5      22.4   22.4    1.1 
##  8 COMPU~ Compucom ~ 2020-06-09  10.8   10.8   10.8      10.6   10.3    0.5 
##  9 DAAWAT LT Foods ~ 2020-06-09  37.8   37.8   37.2      37     34.7    2.55
## 10 DHANU~ Dhanuka A~ 2020-06-09 653.   653.   635.      620    594.    41.4 
## # ... with 24 more rows, and 1 more variable: 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 - Explore Discover Learn.

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)