-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2900307
commit c91f780
Showing
17 changed files
with
213 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
## <chr> <date> <chr> <chr> <chr> <dbl> <dbl> <dbl> | ||
## 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 <dbl>, high <dbl>, low <dbl>, close <dbl>, | ||
## # average <dbl>, adOpen <dbl>, adHigh <dbl>, adLow <dbl>, adClose <dbl>, | ||
## # adAverage <dbl>, nmVolume <dbl>, nmValue <dbl>, ptVolume <dbl>, | ||
## # ptValue <dbl>, change <dbl>, adChange <dbl>, pctChange <dbl> | ||
|
||
``` r | ||
dplyr::glimpse(dt) | ||
``` | ||
|
||
## Rows: 100 | ||
## Columns: 25 | ||
## $ ticker_name <chr> "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "~ | ||
## $ date <date> 2022-03-01, 2022-02-28, 2022-02-25, 2022-02-24, 2022-02-~ | ||
## $ time <chr> "14:44:58", "15:02:03", "15:02:04", "15:02:03", "15:02:03~ | ||
## $ floor <chr> "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "~ | ||
## $ type <chr> "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "ST~ | ||
## $ basicPrice <dbl> 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.0, 87.~ | ||
## $ ceilingPrice <dbl> 90.4, 91.0, 91.2, 92.9, 92.8, 93.5, 93.3, 93.6, 93.0, 93.~ | ||
## $ floorPrice <dbl> 78.6, 79.2, 79.4, 80.9, 80.8, 81.3, 81.1, 81.4, 81.0, 81.~ | ||
## $ open <dbl> 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ | ||
## $ high <dbl> 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ | ||
## $ low <dbl> 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ | ||
## $ close <dbl> 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ | ||
## $ average <dbl> 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ | ||
## $ adOpen <dbl> 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ | ||
## $ adHigh <dbl> 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ | ||
## $ adLow <dbl> 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ | ||
## $ adClose <dbl> 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ | ||
## $ adAverage <dbl> 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ | ||
## $ nmVolume <dbl> 1295400, 1322100, 1616500, 2078700, 986300, 1540200, 1017~ | ||
## $ nmValue <dbl> 109913000000, 112046270000, 137812170000, 178190380000, 8~ | ||
## $ ptVolume <dbl> 7900, 0, 60000, 79000, 0, 60000, 79000, 0, 60000, 79000, ~ | ||
## $ ptValue <dbl> 6.588400e+03, 0.000000e+00, 5.217000e+09, 6.768100e+09, 0~ | ||
## $ change <dbl> 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ | ||
## $ adChange <dbl> 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ | ||
## $ pctChange <dbl> 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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^doc$ | ||
^Meta$ | ||
^CRAN-SUBMISSION$ | ||
^LICENSE\.md$ | ||
^cran-comments\.md$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata | ||
inst/doc | ||
/doc/ | ||
/Meta/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Version: 0.0.1 | ||
Date: 2023-10-03 14:48:18 UTC | ||
SHA: cd3d5c9fdae23f5ad301daf30caf2621c063296a |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
## <chr> <date> <chr> <chr> <chr> <dbl> <dbl> <dbl> | ||
## 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 <dbl>, high <dbl>, low <dbl>, close <dbl>, | ||
## # average <dbl>, adOpen <dbl>, adHigh <dbl>, adLow <dbl>, adClose <dbl>, | ||
## # adAverage <dbl>, nmVolume <dbl>, nmValue <dbl>, ptVolume <dbl>, | ||
## # ptValue <dbl>, change <dbl>, adChange <dbl>, pctChange <dbl> | ||
|
||
``` r | ||
dplyr::glimpse(dt) | ||
``` | ||
|
||
## Rows: 100 | ||
## Columns: 25 | ||
## $ ticker_name <chr> "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "VCB", "~ | ||
## $ date <date> 2022-03-01, 2022-02-28, 2022-02-25, 2022-02-24, 2022-02-~ | ||
## $ time <chr> "14:44:58", "15:02:03", "15:02:04", "15:02:03", "15:02:03~ | ||
## $ floor <chr> "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "HOSE", "~ | ||
## $ type <chr> "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "STOCK", "ST~ | ||
## $ basicPrice <dbl> 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.0, 87.~ | ||
## $ ceilingPrice <dbl> 90.4, 91.0, 91.2, 92.9, 92.8, 93.5, 93.3, 93.6, 93.0, 93.~ | ||
## $ floorPrice <dbl> 78.6, 79.2, 79.4, 80.9, 80.8, 81.3, 81.1, 81.4, 81.0, 81.~ | ||
## $ open <dbl> 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ | ||
## $ high <dbl> 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ | ||
## $ low <dbl> 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ | ||
## $ close <dbl> 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ | ||
## $ average <dbl> 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ | ||
## $ adOpen <dbl> 84.5, 85.1, 85.3, 86.7, 87.0, 87.0, 86.6, 86.4, 87.0, 87.~ | ||
## $ adHigh <dbl> 85.4, 85.1, 85.9, 86.8, 87.0, 87.0, 87.6, 87.5, 87.7, 87.~ | ||
## $ adLow <dbl> 84.1, 84.4, 85.0, 85.0, 85.8, 85.0, 86.6, 86.4, 86.8, 86.~ | ||
## $ adClose <dbl> 85.0, 84.5, 85.1, 85.3, 86.9, 86.8, 87.4, 87.2, 87.5, 87.~ | ||
## $ adAverage <dbl> 84.8487, 84.7500, 85.2500, 85.7200, 86.7600, 86.0500, 87.~ | ||
## $ nmVolume <dbl> 1295400, 1322100, 1616500, 2078700, 986300, 1540200, 1017~ | ||
## $ nmValue <dbl> 109913000000, 112046270000, 137812170000, 178190380000, 8~ | ||
## $ ptVolume <dbl> 7900, 0, 60000, 79000, 0, 60000, 79000, 0, 60000, 79000, ~ | ||
## $ ptValue <dbl> 6.588400e+03, 0.000000e+00, 5.217000e+09, 6.768100e+09, 0~ | ||
## $ change <dbl> 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ | ||
## $ adChange <dbl> 0.5, -0.6, -0.2, -1.6, 0.1, -0.6, 0.2, -0.3, 0.5, 0.0, 1.~ | ||
## $ pctChange <dbl> 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 | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.