knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The IndianStocksR package is used to download the end of day data of all stocks in the two primary Indian stock markets, NSE and BSE. The end of data data is provided free by the two stock exchanges from their websites and consists of information like the open, high, low, close among others for each script that's traded in them.

The data can be accessed from their websites based on the date formatted in a certain way. The R-Bloggers article was the source of inspiration for the package. However, the package organizes the code there and creates a much more accessible api that's more powerful in a sense.

It is adviced to create a folder and set the working directory to that folder before we start work. Even better, if you're working from Rstudio is to create an R project for downloading the data and working on your analysis.

After installation, we load the packages necessary.

library(IndianStocksR)

Main Functions

The main workhorse function in the package download_stocks. However, you will rarely have to use it. It still pays to udnerstand the paramters as it is the basis of the other functions that you will probably use.

download_stocks(date = "2018-07-20", exchange = c("nse", "bse"), dest_path = "./data", quiet = FALSE)

The main purpose of this function is to download data from the exchange specified on the date specified. If data is not available for the date you specified, you will get an error.

The function you'll probably have to use first is the download_stocks_period

df_period <- 
  download_stocks_period(start = "2018-07-21",
                         end = "2018-07-26",
                         exchange = c("both", "nse", "bse"),
                         dest_path = "./data",
                         compile = TRUE,
                         delete_component_files = TRUE,
                         quiet = FALSE)
df_period

The function returns the compiled files apart from writing them out as a csv.

Once you have the download_stocks_period run, you can update the database later by running the update_stocks

df_updated <- 
  update_stocks(data_path = "./data",
                till = lubridate::today(),
                exchange = c("both", "nse", "bse"),
                compile = TRUE,
                delete_component_files = TRUE)
df_updated

Most of this parameters have been discussed before. This function scans all the files in the directory and finds out the date till which there is data and downloads data from the day after till the date mentioned by till. If there are no files inside the specified folder, it downloads data from today - 8 till the date mentioned by till. You rarely have to tweak the till function we use the function primarily to update till the current day.

A note on defaults

Except the date parameters, one rarely has to tweak the defaults. The defaults are designed to work optimally.



ilangurudev/IndianStocksR documentation built on May 12, 2019, 4:36 a.m.