Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
internal code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindhebbali committed Dec 11, 2019
1 parent baae241 commit 944b60d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 90 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export(nse_stock_top_losers)
export(nse_stock_valid)
export(nse_stock_year_high)
export(nse_stock_year_low)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(utils,install.packages)
importFrom(utils,menu)
Expand Down
11 changes: 3 additions & 8 deletions R/nse-advance-decline.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@
#'
nse_advances_declines <- function(clean_names = TRUE) {

url <- "http://www.nseindia.com/common/json/indicesAdvanceDeclines.json"

data <-
url %>%
nse_base() %>%
nse_format(cols_skip = 1, cols_modify = 2:4)
url <- "http://www.nseindia.com/common/json/indicesAdvanceDeclines.json"
data <- nse_format(nse_base(url), 1, 2:4)

if (clean_names) {
data %<>%
magrittr::set_names(., c("index_name", "advances", "declines", "unchanged"))
names(data) <- c("index_name", "advances", "declines", "unchanged")
}

data
Expand Down
32 changes: 11 additions & 21 deletions R/nse-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ nse_index_quote <- function(clean_names = TRUE) {

url <- "http://www.nseindia.com/homepage/Indices1.json"

result <-
url %>%
url %>%
nse_base() %>%
magrittr::extract(-5) %>%
nse_format_num(cols_skip = 1, cols_modify = 2:4) %>%
nse_format(cols_skip = 1, cols_modify = 2:4)
nse_format_num(1, 2:4) %>%
nse_format(1, 2:4) -> result

if (clean_names) {
result %<>%
magrittr::set_names(., c("index_name", "last_traded_price", "change",
"percent_change"))
names(result) <- c("index_name", "last_traded_price", "change",
"percent_change")
}

return(result)
result

}

Expand All @@ -66,19 +64,14 @@ nse_index_quote <- function(clean_names = TRUE) {
#'
nse_index_list <- function(clean_names = TRUE) {

url <- "http://www.nseindia.com/homepage/Indices1.json"

result <-
url %>%
nse_base() %>%
magrittr::extract(1)
url <- "http://www.nseindia.com/homepage/Indices1.json"
result <- nse_base(url)[1]

if (clean_names) {
result %<>%
magrittr::set_names(., c("index_name"))
names(result) <- c("index_name")
}

return(result)
result

}

Expand All @@ -98,10 +91,7 @@ nse_index_list <- function(clean_names = TRUE) {
#'
nse_index_valid <- function(index_code) {

valid_index <-
nse_index_list() %>%
magrittr::extract2(1)

valid_index <- nse_index_list()[[1]]
toupper(index_code) %in% valid_index

}
61 changes: 23 additions & 38 deletions R/nse-stock.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ nse_stock_most_traded <- function(clean_names = TRUE) {

url <- "https://www.nseindia.com/products/dynaContent/equities/equities/json/mostActiveMonthly.json"

result <-
url %>%
url %>%
nse_base() %>%
nse_format_num(cols_skip = 1, cols_modify = 2:6) %>%
nse_format(cols_skip = 1, cols_modify = 2:6)
nse_format_num(1, 2:6) %>%
nse_format(1, 2:6) -> result

if (clean_names) {
result %<>%
magrittr::set_names(., c("security", "share_turnover", "traded_quantity",
"no_of_trades", "avg_daily_turnonver", "turnover"))
names(result) <- c("security", "share_turnover", "traded_quantity",
"no_of_trades", "avg_daily_turnonver", "turnover")
}

return(result)
result

}

Expand Down Expand Up @@ -90,20 +88,16 @@ NULL
#'
nse_stock_year_high <- function(clean_names = TRUE) {

url <- "https://www.nseindia.com/products/dynaContent/equities/equities/json/online52NewHigh.json"

result <-
url %>%
nse_stock_year_base()
url <- "https://www.nseindia.com/products/dynaContent/equities/equities/json/online52NewHigh.json"
result <- nse_stock_year_base(url)

if (clean_names) {
result %<>%
magrittr::set_names(., c("symbol", "symbol_desc", "date", "new_high", "year",
"last_traded_price", "prev_high", "prev_close",
"change", "percent_change"))
names(result) <- c("symbol", "symbol_desc", "date", "new_high", "year",
"last_traded_price", "prev_high", "prev_close", "change",
"percent_change")
}

return(result)
result

}

Expand All @@ -112,20 +106,16 @@ nse_stock_year_high <- function(clean_names = TRUE) {
#'
nse_stock_year_low <- function(clean_names = TRUE) {

url <- "https://www.nseindia.com/products/dynaContent/equities/equities/json/online52NewLow.json"

result <-
url %>%
nse_stock_year_base()
url <- "https://www.nseindia.com/products/dynaContent/equities/equities/json/online52NewLow.json"
result <- nse_stock_year_base(url)

if (clean_names) {
result %<>%
magrittr::set_names(., c("symbol", "symbol_desc", "date", "new_low", "year",
"last_traded_price", "prev_low", "prev_close",
"change", "percent_change"))
names(result) <- c("symbol", "symbol_desc", "date", "new_low", "year",
"last_traded_price", "prev_low", "prev_close", "change",
"percent_change")
}

return(result)
result

}

Expand Down Expand Up @@ -156,19 +146,17 @@ nse_stock_code <- function(clean_names = TRUE) {

url <- "http://www.nseindia.com/content/equities/EQUITY_L.csv"

result <-
url %>%
url %>%
utils::read.csv() %>%
magrittr::extract(., 1:2) %>%
tibble::as_tibble() %>%
purrr::map_dfc(as.character)
purrr::map_dfc(as.character) -> result

if (clean_names) {
result %<>%
magrittr::set_names(., c("symbol", "company"))
names(result) <- c("symbol", "company")
}

return(result)
result

}

Expand All @@ -188,10 +176,7 @@ nse_stock_code <- function(clean_names = TRUE) {
#'
nse_stock_valid <- function(stock_code) {

valid_stock <-
nse_stock_code() %>%
magrittr::extract2(1)

valid_stock <- nse_stock_code()[[1]]
toupper(stock_code) %in% valid_stock

}
Expand Down
39 changes: 17 additions & 22 deletions R/nse-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @param url URL of data.
#'
#' @importFrom magrittr %>% %<>%
#' @importFrom magrittr %>%
#'
#' @noRd
#'
Expand Down Expand Up @@ -36,15 +36,14 @@ nse_stock_year_base <- function(url) {
cols_to_skip <- c(1, 2, 7)
cols_to_modify <- c(3:6, 8:10)

result <-
url %>%
url %>%
nse_base() %>%
nse_format_num(cols_skip = cols_to_skip, cols_modify = cols_to_modify) %>%
nse_format(cols_skip = 1:3, cols_modify = 4:10)
nse_format_num(cols_to_skip, cols_to_modify) %>%
nse_format(1:3, 4:10) -> result

result$dt <- as.Date(result$dt, format = "%d-%b-%Y")

return(result)
result

}

Expand All @@ -53,50 +52,46 @@ nse_fo_base <- function(url, clean_names) {
cols_to_skip <- c(1, 2, 11, 12)
cols_to_modify <- 3:10

result <-
url %>%
url %>%
nse_base() %>%
nse_format_num(cols_skip = cols_to_skip, cols_modify = cols_to_modify) %>%
nse_format(cols_skip = 1:4, cols_modify = 5:12)
nse_format_num(cols_to_skip, cols_to_modify) %>%
nse_format(1:4, 5:12) -> result

result$lastCorpAnnouncementDate <- as.Date(result$lastCorpAnnouncementDate,
format = "%d-%b-%Y")
if (clean_names) {
result %<>%
magrittr::set_names(., c("symbol", "series", "last_corp_announcement_date",
names(result) <- c("symbol", "series", "last_corp_announcement_date",
"last_corp_announcement", "open_price", "high_price",
"low_price", "last_traded_price",
"prev_close_price", "percent_change",
"traded_quantity", "turnover_in_lakhs"))
"traded_quantity", "turnover_in_lakhs")
}

return(result)
result

}

nse_preopen_base <- function(url, clean_names) {

result <-
url %>%
url %>%
nse_base() %>%
nse_format_num(cols_skip = 1:4, cols_modify = 5:17) %>%
nse_format(cols_skip = 1:4, cols_modify = 5:17)
nse_format_num(1:4, 5:17) %>%
nse_format(cols_skip = 1:4, cols_modify = 5:17) -> result

result$xDt <- as.Date(result$xDt, format = "%d-%b-%Y")
result$caAct[result$caAct == "-"] <- NA
result$caAct <- trimws(result$caAct)

if (clean_names) {
result %<>%
magrittr::set_names(., c("symbol", "series", "corp_action_date",
names(result) <- c("symbol", "series", "corp_action_date",
"corp_action", "price", "change",
"percent_change", "prev_close", "quantity",
"value", "mkt_cap", "year_high", "year_low",
"sum_val", "sum_quantity", "fin_quantity",
"sum_fin_quantity"))
"sum_fin_quantity")
}

return(result)
result

}

Expand Down

0 comments on commit 944b60d

Please sign in to comment.