Skip to content

Commit

Permalink
added cache system to get_exchanges()
Browse files Browse the repository at this point in the history
  • Loading branch information
msperlin committed Jun 20, 2024
1 parent c7d8b34 commit 8774a77
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: eodhd2
Type: Package
Title: Second and backward-incompatible version of R package eodhd
Version: 0.5.0
Author: Who wrote it
Author: Marcelo S. Perlin <marceloperlin@gmail.com>
Maintainer: Marcelo S. Perlin <marceloperlin@gmail.com>
Description: R port of eodhd API <https://eodhd.com/>, extended with a cache and quota system,
also offering functions for cleaning and aggregating the financial data.
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Version 0.5 (2024-06-04)
## Version 0.5 (2024-06-20)

- first version
2 changes: 2 additions & 0 deletions R/base-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ query_api <- function(url) {
#'
#' @noRd
get_base_url <- function() {

base_url <- "https://eodhd.com/api/"

return(base_url)

}
7 changes: 5 additions & 2 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ get_cache_file <- function(ticker, exchange, cache_folder, type_data) {
return(f_out)
}

#' Writes file to cache
#' Writes file to cache using rds
#'
#' @noRd
write_cache <- function(x, f_out) {

readr::write_rds(x, f_out)

cli::cli_alert_info("cache file {f_out} saved")

return(invisible(TRUE))

}

#' Reads file to cache
#' Reads file to cache from rds
#'
#' @noRd
read_cache <- function(f_cache) {

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

x <- readr::read_rds(f_cache)

return(x)
Expand Down
1 change: 0 additions & 1 deletion R/dividends.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ get_dividends <- function(ticker = "AAPL", exchange = "US",
f_out <-get_cache_file(ticker, exchange, cache_folder, "dividends")

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

df_div <- read_cache(f_out)

Expand Down
29 changes: 21 additions & 8 deletions R/exchanges.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Retriees the list of exchanges available
#' Retrieves the list of available exchanges
#'
#' @return a dataframe with information about available exchanges
#' @export
Expand All @@ -7,26 +7,39 @@
#'
#' # you need a valid token (not test) for this to work
#' \dontrun{
#' set_token(YOUR_VALID_TOKEN)
#' set_token("YOUR_VALID_TOKEN")
#' df_exc <- get_exchanges()
#' }
#'
get_exchanges <- function() {
get_exchanges <- function(cache_folder = get_default_cache()) {

cli::cli_h1("retrieving exchange list")

token <- get_token()

if (token == get_demo_token()) {
cli::cli_abort("You need a proper token (not \"{get_demo_token()}\") for retrieving the list of exchanges.")
}

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

if (fs::file_exists(f_out)) {

df_div <- read_cache(f_out)

} else {

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

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

df_exc <- jsonlite::fromJSON(content)

write_cache(df_exc, f_out)

}

cli::cli_alert_success("got {nrow(df_exc)} rows")

Expand Down
3 changes: 2 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# source: https://community.rstudio.com/t/how-to-solve-no-visible-binding-
# for-global-variable-note/28887
my_globals <- c(
"filing_date"
"filing_date",
"adjusted_close"
)

utils::globalVariables(my_globals)
5 changes: 4 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ Package eodhd2 is the second and backwards incompatible version of [eodhd](https
devtools::install_github("msperlin/eodhd2")
```


# Usage

## Authentication

After registering at the [website](https://eodhd.com/) and choosing a subscription, all users will authenticate an R session using a token from the website. For that:
After registering in the [eodhd website](https://eodhd.com/) and choosing a subscription, all users will authenticate an R session using a token from the website. For that:

1) Create an account at [https://eodhd.com/](https://eodhd.com/)
2) Go in "Settings" and look for your API token
Expand Down Expand Up @@ -135,6 +136,7 @@ l_fun <- eodhd2::get_fundamentals(ticker, exchange)
names(l_fun)
```


## Parsing financials (wide table)

```{r}
Expand All @@ -143,6 +145,7 @@ wide_financials <- eodhd2::parse_financials(l_fun, "wide")
head(wide_financials)
```


## Parsing financials (long table)

```{r}
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ devtools::install_github("msperlin/eodhd2")

## Authentication

After registering at the [website](https://eodhd.com/) and choosing a
subscription, all users will authenticate an R session using a token
from the website. For that:
After registering in the [eodhd website](https://eodhd.com/) and
choosing a subscription, all users will authenticate an R session using
a token from the website. For that:

1) Create an account at <https://eodhd.com/>
2) Go in “Settings” and look for your API token
Expand All @@ -75,7 +75,7 @@ token <- eodhd2::get_demo_token()
eodhd2::set_token(token)
#> ✔ eodhd API token set
#> ℹ Account name: API Documentation 2 (supportlevel1@eodhistoricaldata.com)
#> ℹ Quota: 57033 | 10000000
#> ℹ Quota: 37531 | 10000000
#> ℹ Subscription: demo
#> ✖ 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 @@ -92,10 +92,10 @@ exchange <- "US"
df_prices <- eodhd2::get_prices(ticker, exchange)
#>
#> ── retrieving price data for ticker AAPL|US ────────────────────────────────────
#> ! Quota status: 57034|10000000, refreshing in 6.95 hours
#> ℹ cache file '/tmp/RtmpjBU71O/eodhd2-cache/AAPL_US_eodhd_prices.rds' saved
#> ✔ got 10959 rows of prices
#> ℹ got daily data from 1980-12-12 to 2024-06-03
#> ! Quota status: 37531|10000000, refreshing in 12.4 hours
#> ℹ cache file '/tmp/Rtmp1ImxEq/eodhd2-cache/AAPL_US_eodhd_prices.rds' saved
#> ✔ got 10970 rows of prices
#> ℹ got daily data from 1980-12-12 to 2024-06-18
```

``` r
Expand All @@ -108,13 +108,13 @@ head(df_prices)
#> 4 1980-12-17 25.8720 26.0064 25.8720 25.8720 0.0892 86441600 AAPL
#> 5 1980-12-18 26.6336 26.7456 26.6336 26.6336 0.0918 73449600 AAPL
#> 6 1980-12-19 28.2464 28.3808 28.2464 28.2464 0.0973 48630400 AAPL
#> exchange
#> 1 US
#> 2 US
#> 3 US
#> 4 US
#> 5 US
#> 6 US
#> exchange ret_adj_close
#> 1 US NA
#> 2 US -0.05151515
#> 3 US -0.07348243
#> 4 US 0.02528736
#> 5 US 0.02914798
#> 6 US 0.05991285
```

``` r
Expand Down Expand Up @@ -142,8 +142,8 @@ exchange <- "US"
df_div <- eodhd2::get_dividends(ticker, exchange)
#>
#> ── retrieving dividends for ticker AAPL|US ─────────────────────────────────────
#> ! Quota status: 57037|10000000, refreshing in 6.94 hours
#> ℹ cache file '/tmp/RtmpjBU71O/eodhd2-cache/AAPL_US_eodhd_dividends.rds' saved
#> ! Quota status: 37540|10000000, refreshing in 12.4 hours
#> ℹ cache file '/tmp/Rtmp1ImxEq/eodhd2-cache/AAPL_US_eodhd_dividends.rds' saved
#> ✔ got 83 rows of dividend data
```

Expand Down Expand Up @@ -190,7 +190,7 @@ exchange <- "US"
l_fun <- eodhd2::get_fundamentals(ticker, exchange)
#>
#> ── retrieving fundamentals for ticker AAPL|US ──────────────────────────────────
#> ! Quota status: 57038|10000000, refreshing in 6.94 hours
#> ! Quota status: 37543|10000000, refreshing in 12.4 hours
#> ✔ querying API
#> ✔ got 13 elements in raw list
```
Expand Down
Binary file modified man/figures/README-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion tests/testthat/test-fundamentals.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
test_that("fundamentals", {


temp_cache_folder <- fs::path_temp("oedhd-test-cache")

suppressMessages({
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-statuscode.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ test_that("status codes", {
expect_error(parse_status_code(400))
expect_error(parse_status_code(403))


})

0 comments on commit 8774a77

Please sign in to comment.