diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION deleted file mode 100644 index 92768b4..0000000 --- a/CRAN-SUBMISSION +++ /dev/null @@ -1,3 +0,0 @@ -Version: 0.0.1 -Date: 2023-10-02 14:27:47 UTC -SHA: b26e618be784ccc04485963fdfb5f13f8058a8bc diff --git a/README.md b/README.md index c90e554..1feddeb 100644 --- a/README.md +++ b/README.md @@ -1,84 +1,36 @@ # Get data from vndirect API -## Installation - -```{r} -# install.packages("devtools") -devtools::install_github("nguyenngocbinh/vnstock") -``` - -## Get data - -``` r -library(vnstock) -dt <- getData('VCB', 100) -head(dt) -``` - - ## # A tibble: 6 x 25 - ## ticker_name date time floor type basicPrice ceilingPrice floorPrice - ## - ## 1 VCB 2022-03-01 14:44:58 HOSE STOCK 84.5 90.4 78.6 - ## 2 VCB 2022-02-28 15:02:03 HOSE STOCK 85.1 91 79.2 - ## 3 VCB 2022-02-25 15:02:04 HOSE STOCK 85.3 91.2 79.4 - ## 4 VCB 2022-02-24 15:02:03 HOSE STOCK 86.9 92.9 80.9 - ## 5 VCB 2022-02-23 15:02:03 HOSE STOCK 86.8 92.8 80.8 - ## 6 VCB 2022-02-22 15:02:02 HOSE STOCK 87.4 93.5 81.3 - ## # ... with 17 more variables: open , high , low , close , - ## # average , adOpen , adHigh , adLow , adClose , - ## # adAverage , nmVolume , nmValue , ptVolume , - ## # ptValue , change , adChange , pctChange - -``` r -dplyr::glimpse(dt) -``` - - ## Rows: 100 - ## Columns: 25 - ## $ ticker_name "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "~ - ## $ date 2022-03-01, 2022-02-28, 2022-02-25, 2022-02-24, 2022-02-~ - ## $ time "14:44:58", "15:02:03", "15:02:04", "15:02:03", "15:02:03~ - ## $ floor "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "~ - ## $ type "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "ST~ - ## $ basicPrice 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.0, 87.~ - ## $ ceilingPrice 90.4, 91.0, 91.2, 92.9, 92.8, 93.5, 93.3, 93.6, 93.0, 93.~ - ## $ floorPrice 78.6, 79.2, 79.4, 80.9, 80.8, 81.3, 81.1, 81.4, 81.0, 81.~ - ## $ open 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ - ## $ high 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ - ## $ low 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ - ## $ close 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ - ## $ average 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ - ## $ adOpen 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ - ## $ adHigh 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ - ## $ adLow 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ - ## $ adClose 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ - ## $ adAverage 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ - ## $ nmVolume 1295400, 1322100, 1616500, 2078700, 986300, 1540200, 1017~ - ## $ nmValue 109913000000, 112046270000, 137812170000, 178190380000, 8~ - ## $ ptVolume 7900, 0, 60000, 79000, 0, 60000, 79000, 0, 60000, 79000, ~ - ## $ ptValue 6.588400e+03, 0.000000e+00, 5.217000e+09, 6.768100e+09, 0~ - ## $ change 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ - ## $ adChange 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ - ## $ pctChange 0.5917, -0.7051, -0.2345, -1.8412, 0.1152, -0.6865, 0.229~ - - ``` vndirect -├─ cran-comments.md -├─ CRAN-SUBMISSION -├─ DESCRIPTION -├─ LICENSE -├─ man -│ └─ getData.Rd -├─ NAMESPACE -├─ R -│ └─ vnstock.R -├─ README.md -├─ tests -│ └─ test-getData.R -├─ vignettes -│ ├─ .gitignore -│ └─ getdata.Rmd -└─ vnstock.Rproj - -``` +├─ python-package +│ ├─ README.md +│ ├─ requirements.txt +│ ├─ setup.py +│ └─ vnstock +│ ├─ vnstock.py +│ └─ __init__.py +├─ r-package +│ ├─ cran-comments.md +│ ├─ CRAN-SUBMISSION +│ ├─ DESCRIPTION +│ ├─ doc +│ │ ├─ getdata.html +│ │ ├─ getdata.R +│ │ └─ getdata.Rmd +│ ├─ LICENSE +│ ├─ LICENSE.md +│ ├─ man +│ │ └─ getData.Rd +│ ├─ Meta +│ │ └─ vignette.rds +│ ├─ NAMESPACE +│ ├─ R +│ │ └─ vnstock.R +│ ├─ README.md +│ ├─ tests +│ │ └─ test-getData.R +│ ├─ vignettes +│ │ └─ getdata.Rmd +│ └─ vndirect.Rproj +└─ README.md +``` \ No newline at end of file diff --git a/r-package/.Rbuildignore b/r-package/.Rbuildignore new file mode 100644 index 0000000..4c6d848 --- /dev/null +++ b/r-package/.Rbuildignore @@ -0,0 +1,7 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^doc$ +^Meta$ +^CRAN-SUBMISSION$ +^LICENSE\.md$ +^cran-comments\.md$ diff --git a/r-package/.gitignore b/r-package/.gitignore new file mode 100644 index 0000000..7a7155e --- /dev/null +++ b/r-package/.gitignore @@ -0,0 +1,7 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata +inst/doc +/doc/ +/Meta/ diff --git a/r-package/CRAN-SUBMISSION b/r-package/CRAN-SUBMISSION new file mode 100644 index 0000000..fe89a3f --- /dev/null +++ b/r-package/CRAN-SUBMISSION @@ -0,0 +1,3 @@ +Version: 0.0.1 +Date: 2023-10-03 14:48:18 UTC +SHA: cd3d5c9fdae23f5ad301daf30caf2621c063296a diff --git a/DESCRIPTION b/r-package/DESCRIPTION similarity index 100% rename from DESCRIPTION rename to r-package/DESCRIPTION diff --git a/LICENSE b/r-package/LICENSE similarity index 100% rename from LICENSE rename to r-package/LICENSE diff --git a/LICENSE.md b/r-package/LICENSE.md similarity index 100% rename from LICENSE.md rename to r-package/LICENSE.md diff --git a/NAMESPACE b/r-package/NAMESPACE similarity index 100% rename from NAMESPACE rename to r-package/NAMESPACE diff --git a/R/vnstock.R b/r-package/R/vnstock.R similarity index 96% rename from R/vnstock.R rename to r-package/R/vnstock.R index a9ec99a..1be1790 100644 --- a/R/vnstock.R +++ b/r-package/R/vnstock.R @@ -1,80 +1,80 @@ -#' REQUEST THE VNDIRECT API -#' -#' Retrieves historical stock data from the VNDIRECT API for multiple tickers. -#' -#' @param .tickers A character vector of stock symbols. -#' @param .size Length of historical data. -#' @return A data frame containing historical stock data for all tickers. -#' @importFrom magrittr `%>%` -#' @importFrom dplyr bind_rows -#' @import httr -#' -#' @examples -#' getData(c('TPB', 'VCB', 'HCM'), 1000) -#' -#' @export -getData <- function(.tickers = NULL, .size = 100) { - - # Check if .tickers and .size are valid - if (is.null(.tickers) || length(.tickers) == 0) { - stop('.tickers is not set') - } - - if (.size <= 0) { - stop('.size must be > 0') - } - - # Define the base URL - base <- "https://finfo-api.vndirect.com.vn/v4/stock_prices/" - - # Initialize an empty data frame to store the results - df <- data.frame() - - for (ticker in .tickers) { - - # Check if the ticker is valid (e.g., it should be a non-empty string) - if (!is.character(ticker) || nchar(ticker) != 3) { - stop('Invalid ticker:', ticker) - } - - # Define the endpoint for each ticker - endpoint <- paste0('code:', ticker) - print(endpoint) - - # Set query parameters - params <- list( - sort = "date", - size = .size, - page = 1, - q = endpoint - ) - - # Send the HTTP request - res <- httr::GET(url = base, query = params) - - # Check if the HTTP request was successful - if (httr::http_error(res)) { - warning(paste('Failed to retrieve data for ticker:', ticker)) - next # Skip to the next ticker on error - } - - # Extract and process the data - list_data <- httr::content(res, "parsed")$data - - # Convert to a data frame - if (length(list_data) > 0) { - df_ticker <- purrr::map_dfr(list_data, bind_rows) %>% - dplyr::mutate(date = as.Date(date)) - - # Add the data for the current ticker to the results - df <- dplyr::bind_rows(df, df_ticker) - } else { - warning(paste('No data available for ticker:', ticker)) - } - } - - return(df) -} - - - +#' REQUEST THE VNDIRECT API +#' +#' Retrieves historical stock data from the VNDIRECT API for multiple tickers. +#' +#' @param .tickers A character vector of stock symbols. +#' @param .size Length of historical data. +#' @return A data frame containing historical stock data for all tickers. +#' @importFrom magrittr `%>%` +#' @importFrom dplyr bind_rows +#' @import httr +#' +#' @examples +#' getData(c('TPB', 'VCB', 'HCM'), 1000) +#' +#' @export +getData <- function(.tickers = NULL, .size = 100) { + + # Check if .tickers and .size are valid + if (is.null(.tickers) || length(.tickers) == 0) { + stop('.tickers is not set') + } + + if (.size <= 0) { + stop('.size must be > 0') + } + + # Define the base URL + base <- "https://finfo-api.vndirect.com.vn/v4/stock_prices/" + + # Initialize an empty data frame to store the results + df <- data.frame() + + for (ticker in .tickers) { + + # Check if the ticker is valid (e.g., it should be a non-empty string) + if (!is.character(ticker) || nchar(ticker) != 3) { + stop('Invalid ticker:', ticker) + } + + # Define the endpoint for each ticker + endpoint <- paste0('code:', ticker) + print(endpoint) + + # Set query parameters + params <- list( + sort = "date", + size = .size, + page = 1, + q = endpoint + ) + + # Send the HTTP request + res <- httr::GET(url = base, query = params) + + # Check if the HTTP request was successful + if (httr::http_error(res)) { + warning(paste('Failed to retrieve data for ticker:', ticker)) + next # Skip to the next ticker on error + } + + # Extract and process the data + list_data <- httr::content(res, "parsed")$data + + # Convert to a data frame + if (length(list_data) > 0) { + df_ticker <- purrr::map_dfr(list_data, bind_rows) %>% + dplyr::mutate(date = as.Date(date)) + + # Add the data for the current ticker to the results + df <- dplyr::bind_rows(df, df_ticker) + } else { + warning(paste('No data available for ticker:', ticker)) + } + } + + return(df) +} + + + diff --git a/r-package/README.md b/r-package/README.md new file mode 100644 index 0000000..c90e554 --- /dev/null +++ b/r-package/README.md @@ -0,0 +1,84 @@ +# Get data from vndirect API + +## Installation + +```{r} +# install.packages("devtools") +devtools::install_github("nguyenngocbinh/vnstock") +``` + +## Get data + +``` r +library(vnstock) +dt <- getData('VCB', 100) +head(dt) +``` + + ## # A tibble: 6 x 25 + ## ticker_name date time floor type basicPrice ceilingPrice floorPrice + ## + ## 1 VCB 2022-03-01 14:44:58 HOSE STOCK 84.5 90.4 78.6 + ## 2 VCB 2022-02-28 15:02:03 HOSE STOCK 85.1 91 79.2 + ## 3 VCB 2022-02-25 15:02:04 HOSE STOCK 85.3 91.2 79.4 + ## 4 VCB 2022-02-24 15:02:03 HOSE STOCK 86.9 92.9 80.9 + ## 5 VCB 2022-02-23 15:02:03 HOSE STOCK 86.8 92.8 80.8 + ## 6 VCB 2022-02-22 15:02:02 HOSE STOCK 87.4 93.5 81.3 + ## # ... with 17 more variables: open , high , low , close , + ## # average , adOpen , adHigh , adLow , adClose , + ## # adAverage , nmVolume , nmValue , ptVolume , + ## # ptValue , change , adChange , pctChange + +``` r +dplyr::glimpse(dt) +``` + + ## Rows: 100 + ## Columns: 25 + ## $ ticker_name "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "~ + ## $ date 2022-03-01, 2022-02-28, 2022-02-25, 2022-02-24, 2022-02-~ + ## $ time "14:44:58", "15:02:03", "15:02:04", "15:02:03", "15:02:03~ + ## $ floor "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "~ + ## $ type "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "ST~ + ## $ basicPrice 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.0, 87.~ + ## $ ceilingPrice 90.4, 91.0, 91.2, 92.9, 92.8, 93.5, 93.3, 93.6, 93.0, 93.~ + ## $ floorPrice 78.6, 79.2, 79.4, 80.9, 80.8, 81.3, 81.1, 81.4, 81.0, 81.~ + ## $ open 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ + ## $ high 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ + ## $ low 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ + ## $ close 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ + ## $ average 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ + ## $ adOpen 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ + ## $ adHigh 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ + ## $ adLow 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ + ## $ adClose 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ + ## $ adAverage 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ + ## $ nmVolume 1295400, 1322100, 1616500, 2078700, 986300, 1540200, 1017~ + ## $ nmValue 109913000000, 112046270000, 137812170000, 178190380000, 8~ + ## $ ptVolume 7900, 0, 60000, 79000, 0, 60000, 79000, 0, 60000, 79000, ~ + ## $ ptValue 6.588400e+03, 0.000000e+00, 5.217000e+09, 6.768100e+09, 0~ + ## $ change 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ + ## $ adChange 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ + ## $ pctChange 0.5917, -0.7051, -0.2345, -1.8412, 0.1152, -0.6865, 0.229~ + + +``` +vndirect +├─ cran-comments.md +├─ CRAN-SUBMISSION +├─ DESCRIPTION +├─ LICENSE +├─ man +│ └─ getData.Rd +├─ NAMESPACE +├─ R +│ └─ vnstock.R +├─ README.md +├─ tests +│ └─ test-getData.R +├─ vignettes +│ ├─ .gitignore +│ └─ getdata.Rmd +└─ vnstock.Rproj + +``` diff --git a/cran-comments.md b/r-package/cran-comments.md similarity index 100% rename from cran-comments.md rename to r-package/cran-comments.md diff --git a/man/getData.Rd b/r-package/man/getData.Rd similarity index 100% rename from man/getData.Rd rename to r-package/man/getData.Rd diff --git a/tests/test-getData.R b/r-package/tests/test-getData.R similarity index 100% rename from tests/test-getData.R rename to r-package/tests/test-getData.R diff --git a/vignettes/.gitignore b/r-package/vignettes/.gitignore similarity index 100% rename from vignettes/.gitignore rename to r-package/vignettes/.gitignore diff --git a/vignettes/getdata.Rmd b/r-package/vignettes/getdata.Rmd similarity index 100% rename from vignettes/getdata.Rmd rename to r-package/vignettes/getdata.Rmd diff --git a/vndirect.Rproj b/r-package/vndirect.Rproj similarity index 100% rename from vndirect.Rproj rename to r-package/vndirect.Rproj