Skip to content

Commit

Permalink
fixed docs and added missing cache to get_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
msperlin committed Jun 20, 2024
1 parent 8774a77 commit 560f97d
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 45 deletions.
2 changes: 1 addition & 1 deletion R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ write_cache <- function(x, f_out) {
#' @noRd
read_cache <- function(f_cache) {

cli::cli_alert_info("\tusing local cache with file {basename(f_cache)}")
cli::cli_alert_info("using local cache with file {basename(f_cache)}")

x <- readr::read_rds(f_cache)

Expand Down
4 changes: 2 additions & 2 deletions R/dividends.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ get_dividends <- function(ticker = "AAPL", exchange = "US",
content <- query_api(url)

if (content == "[]") {
cli::cli_alert_danger("\tcant find dividend data for {ticker}|{exchange}")
cli::cli_alert_danger("cant find dividend data for {ticker}|{exchange}")

df_div <- dplyr::tibble()

Expand All @@ -56,7 +56,7 @@ get_dividends <- function(ticker = "AAPL", exchange = "US",
write_cache(df_div, f_out)
}

cli::cli_alert_success("\tgot {nrow(df_div)} rows of dividend data")
cli::cli_alert_success("got {nrow(df_div)} rows of dividend data")

return(df_div)

Expand Down
2 changes: 2 additions & 0 deletions R/exchanges.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' @return a dataframe with information about available exchanges
#' @export
#'
#' @inheritParams get_fundamentals
#'
#' @examples
#'
#' # you need a valid token (not test) for this to work
Expand Down
1 change: 0 additions & 1 deletion R/financials.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ parse_financials <- function(l_out, type_table = "long") {

}


# exit if no data is found
if (nrow(df_financials) == 0){
cli::cli_warn("cant find any financial data.. returning empty dataframe ")
Expand Down
11 changes: 5 additions & 6 deletions R/fundamentals.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#' * Financial highlights
#' * Valuation
#' * Raw financial data (see [eodhd2::parse_financials()] for parsing this data)
#' * and many more (see example for more details)
#' * and many more (see example for more details regarding the output)
#'
#' @param ticker A company ticker (e.g. AAPL). You can find all tickers for a particular exchange with [eodhd2::get_tickers()].
#' @param exchange A exchange symbol (e.g. US). You can find all tickers for a particular exchange with [eodhd2::get_tickers()].
#' @param exchange A exchange symbol (e.g. US). You can find all tickers for a particular exchange with [eodhd2::get_tickers()]. Be aware that, for US companies, the exchange symbols is simply "US"
#' @param cache_folder A local directory to store cache files. By default, all functions use a temporary path, meaning that the caching system
#' is session persistent (it will remove all files when you exit your R session). If you want a persistent caching system, simply point argument
#' cache_folder to a local directory in your filesystem. Be aware, however, that a persistent cache will not refresh your data for new api queries.
#' @param check_quota A flag (TRUE/FALSE) for whether to check the current quota status from the api. This option has a small cost of execution
#' @param check_quota A flag (TRUE/FALSE) for whether to check the current quota status from the api. This option implies a small cost of execution
#' time. If you need speed, just set it to FALSE.
#'
#' @return a list with several fundamental information
Expand Down Expand Up @@ -44,13 +44,12 @@ get_fundamentals <- function(ticker = "AAPL",
fs::dir_create(dirname(f_out), recurse = TRUE)

if (fs::file_exists(f_out)) {
cli::cli_alert_success("\treading cache file {f_out}")

l_out <- readr::read_rds(f_out)

} else {

cli::cli_alert_success("\tquerying API")
cli::cli_alert_success("querying API")

url <- glue::glue('{get_base_url()}/fundamentals/{ticker}.{exchange}?api_token={token}&fmt=json')

Expand All @@ -66,7 +65,7 @@ get_fundamentals <- function(ticker = "AAPL",
readr::write_rds(l_out, f_out)
}

cli::cli_alert_success("\tgot {length(l_out)} elements in raw list")
cli::cli_alert_success("got {length(l_out)} elements in raw list")

return(l_out)

Expand Down
10 changes: 5 additions & 5 deletions R/prices.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Retrieves adjusted and unadjusted stock prices
#'
#' This function will return daily stock price from a set of ticker and exchange.
#' This function will query the price end point of eodhd and return daily stock price from a set of ticker and exchange.
#' It also includes the daily stock return (percentage variation).
#'
#' @inheritParams get_fundamentals
#'
Expand All @@ -17,7 +18,7 @@ get_prices <- function(ticker = "AAPL",

cli::cli_h1("retrieving price data for ticker {ticker}|{exchange}")

if (check_quota) {
if (check_quota) {
get_quota_status()
}

Expand All @@ -26,7 +27,6 @@ get_prices <- function(ticker = "AAPL",
f_out <-get_cache_file(ticker, exchange, cache_folder, "prices")

if (fs::file_exists(f_out)) {
cli::cli_alert_success("\treading cache file {f_out}")

df_out <- read_cache(f_out)

Expand All @@ -47,8 +47,8 @@ get_prices <- function(ticker = "AAPL",

write_cache(df_prices, f_out)

cli::cli_alert_success("\tgot {nrow(df_prices)} rows of prices")
cli::cli_alert_info("\tgot daily data from {min(df_prices$date)} to {max(df_prices$date)}")
cli::cli_alert_success("got {nrow(df_prices)} rows of prices")
cli::cli_alert_info("got daily data from {min(df_prices$date)} to {max(df_prices$date)}")

return(df_prices)

Expand Down
14 changes: 8 additions & 6 deletions R/splits.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#' Retrieves splits data from eodhd
#'
#' This function will query the splits end point of eodhd and return all split information for a given stock/exchange.
#'
#' @inheritParams get_fundamentals
#'
#' @return A dataframe with splits information
#' @return A dataframe with split information
#'
#' @export
#'
Expand All @@ -14,13 +16,14 @@ get_splits <- function(ticker = "AAPL", exchange = "US",
cache_folder = get_default_cache(),
check_quota = TRUE) {

cli::cli_h1("retrieving splits for ticker {ticker}|{exchange}")

token <- get_token()

if (token == get_demo_token()) {
cli::cli_abort("You need a proper token (not demonstration) for exchange list..")
}

cli::cli_h1("retrieving splits for ticker {ticker}|{exchange}")

if (check_quota) {
get_quota_status()
}
Expand All @@ -30,7 +33,6 @@ get_splits <- function(ticker = "AAPL", exchange = "US",
f_out <- get_cache_file(ticker, exchange, cache_folder, "splits")

if (fs::file_exists(f_out)) {
cli::cli_alert_success("\tfile {f_out} already exists..")

df_split <- read_cache(f_out)

Expand All @@ -42,7 +44,7 @@ get_splits <- function(ticker = "AAPL", exchange = "US",
content <- query_api(url)

if (content == "[]") {
cli::cli_alert_danger("\tcant find split data for {ticker}|{exchange}")
cli::cli_alert_danger("cant find split data for {ticker}|{exchange}")

df_split <- dplyr::tibble()

Expand All @@ -58,7 +60,7 @@ get_splits <- function(ticker = "AAPL", exchange = "US",

write_cache(df_split, f_out)

cli::cli_alert_success("\tgot {nrow(df_split)} rows of dividend data")
cli::cli_alert_success("got {nrow(df_split)} rows of dividend data")

return(df_split)

Expand Down
22 changes: 18 additions & 4 deletions R/tickers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,34 @@
#' \dontrun{ # requires a subscription (paid) token
#' df_tickers <- get_tickers("US")
#' }
get_tickers <- function(exchange = "US") {
get_tickers <- function(exchange = "US",
cache_folder = get_default_cache()) {

token <- get_token()

if (token == get_demo_token()) {
cli::cli_abort("You need a proper token (not demonstration) for the ticker list..")
}

cli::cli_h1("retrieving tickers for {exchange}")

url <- glue::glue('{get_base_url()}/exchange-symbol-list/{exchange}?api_token={token}&fmt=json')
f_out <- get_cache_file("exchange", exchange, cache_folder, "ticker-list")

if (fs::file_exists(f_out)) {

df_tickers <- read_cache(f_out)

} else {

content <- query_api(url)
url <- glue::glue('{get_base_url()}/exchange-symbol-list/{exchange}?api_token={token}&fmt=json')

df_tickers <- jsonlite::fromJSON(content)
content <- query_api(url)

df_tickers <- jsonlite::fromJSON(content)

write_cache(df_tickers, f_out)

}

cli::cli_alert_success("got {nrow(df_tickers)} rows for {exchange}")

Expand Down
7 changes: 6 additions & 1 deletion R/token.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#' Uses the token from <https://eodhd.com/cp/dashboard> to authenticate your R session. You can find your own eodhd token from the website.
#' Alternatively, a demo token is also available for testing purposes, with a limited supply of data.
#'
#' @param token the token from eodhd. The default value is a demo token "demo", which allows for partial access to the data
#' @param token the token from eodhd. The default value is a demo token "demo", which allows for partial access to the data.
#' See [eodhd2::get_demo_token()] for using a demo token.
#'
#' @return Nothing
#' @export
Expand Down Expand Up @@ -32,6 +33,7 @@ set_token <- function(token = get_demo_token()) {
cli::cli_alert_info("Subscription: {my_quota$subscriptionMode}")

if (token == get_demo_token()) {
cat('\n')
cli::cli_alert_danger(
"You are using a **DEMONSTRATION** token for testing pourposes, with limited access to the data repositories.
See <https://eodhd.com/> for registration and, after finding your token, use it with function eodhd2::set_token(TOKEN).")
Expand All @@ -49,11 +51,14 @@ set_token <- function(token = get_demo_token()) {
#' @examples
#' get_demo_token()
get_demo_token <- function() {

token = "demo"

return(token)
}

get_token <- function() {

token <- Sys.getenv("eodhd-token")

if (token == "") {
Expand Down
4 changes: 2 additions & 2 deletions man/get_dividends.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions man/get_exchanges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/get_fundamentals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/get_prices.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/get_splits.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/get_tickers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/set_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 560f97d

Please sign in to comment.