Skip to content

Commit

Permalink
ver 1.1.1 -- new check_internet() function
Browse files Browse the repository at this point in the history
  • Loading branch information
msperlin committed Jun 24, 2024
1 parent b2f2c5c commit c46b60d
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 38 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: yfR
Title: Downloads and Organizes Financial Data from Yahoo Finance
Version: 1.1.0
Version: 1.1.1
Authors@R: c(person("Marcelo", "Perlin", email = "[email protected]", role = c("aut", "cre")),
person("Nic", "Crane", role = "rev",
comment = "Nic reviewed the package (v. 0.0.5) for
Expand Down Expand Up @@ -41,7 +41,7 @@ Imports:
jsonlite
License: MIT + file LICENSE
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
knitr,
rmarkdown,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 1.1.1 (2024-06-24) -- minor

- new function check_internet() that will check for an active internet connection every R session. It will not repeat the test once it is passed.

## Version 1.1.0 (2023-01-30) -- major version

- new function yf_live_prices() (see this [pr](https://github.com/ropensci/yfR/pull/24))
Expand Down
10 changes: 4 additions & 6 deletions R/yf_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ yf_get <- function(tickers,
be_quiet = FALSE) {

# check for internet
if (!pingr::is_online()) {
stop("Can't find an active internet connection...")
}
check_internet()

# check cache folder
if ((do_cache) & (!dir.exists(cache_folder))) dir.create(cache_folder,
Expand Down Expand Up @@ -197,14 +195,14 @@ yf_get <- function(tickers,

# disable dplyr group message and respect user choice
# will be null if options is not set
default_dplyr_summ <- options("dplyr.summarise.inform")[[1]]
default_dplyr_sum <- options("dplyr.summarise.inform")[[1]]

# disable and enable at end with on exit
options(dplyr.summarise.inform = FALSE)

on.exit({
if (is.logical(default_dplyr_summ)) {
options(dplyr.summarise.inform = default_dplyr_summ)
if (is.logical(default_dplyr_sum)) {
options(dplyr.summarise.inform = default_dplyr_sum)
}
})

Expand Down
31 changes: 31 additions & 0 deletions R/yf_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,34 @@ substitute_tickers <- function(df_index) {

return(df_index)
}


#' Tests for an internet connection
#'
#' Tests for internet connect in every R session. If already tested, just skip it and saves time.
#'
#' @noRd
check_internet <- function() {
f_flag <- paste0(
tempdir(), "/",
"yf-flag-internet-ok"
)

if (file.exists(f_flag)) {

flag <- TRUE

} else {

flag <- pingr::is_online()

if (!flag) {
stop("Can't find an active internet connection...")
} else {
readr::write_lines("internet ok", f_flag)
}

}

return(flag)
}
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ The easiest way to find the tickers of a company stock is to search for
it in [Yahoo Finance’s](https://finance.yahoo.com/) website. At the top
page you’ll find a search bar:

![YF
Search](/inst/figures/search-yf.png?raw=true "Example of search in YF")
<figure>
<img src="/inst/figures/search-yf.png?raw=true"
title="Example of search in YF" alt="YF Search" />
<figcaption aria-hidden="true">YF Search</figcaption>
</figure>

A company can have many different stocks traded at different markets
(see picture above). As the example shows, Petrobras is traded at NYQ
Expand Down Expand Up @@ -157,35 +160,37 @@ df_yf <- yf_get(tickers = my_ticker,
first_date = first_date,
last_date = last_date)
#>
#> ── Running yfR for 1 stocks | 2023-01-17 --> 2023-02-16 (30 days) ──
#> ── Running yfR for 1 stocks | 2024-05-25 --> 2024-06-24 (30 days) ──
#>
#> ℹ Downloading data for benchmark ticker ^GSPC
#> ℹ (1/1) Fetching data for META
#> ! - not cached
#> ✔ - cache saved successfully
#> ✔ - got 22 valid rows (2023-01-17 --> 2023-02-15)
#> ✔ - got 18 valid rows (2024-05-28 --> 2024-06-21)
#> ✔ - got 100% of valid prices -- Time for some tea?
#> ℹ Binding price data
#>
#> ── Diagnostics ─────────────────────────────────────────────────────────────────
#> ✔ Returned dataframe with 22 rows -- Youre doing good!
#> ℹ Using 6.3 kB at /tmp/RtmpvCnCwr/yf_cache for 2 cache files
#> ✔ Returned dataframe with 18 rows -- You got it msperlin!
#> ℹ Using 5.5 kB at /tmp/Rtmp8A98vn/yf_cache for 2 cache files
#> ℹ Out of 1 requested tickers, you got 1 (100%)
```

``` r

# output is a tibble with data
head(df_yf)
#> # A tibble: 6 × 11
#> ticker ref_date price_open price_h…¹ price…² price…³ volume price…⁴ ret_ad…⁵
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 META 2023-01-17 136. 137. 134. 135. 2.11e7 135. NA
#> 2 META 2023-01-18 136. 137. 133. 133. 2.02e7 133. -1.73e-2
#> 3 META 2023-01-19 132. 137. 132. 136. 2.86e7 136. 2.35e-2
#> 4 META 2023-01-20 136. 140. 135. 139. 2.86e7 139. 2.37e-2
#> 5 META 2023-01-23 139. 144. 139. 143. 2.75e7 143. 2.80e-2
#> 6 META 2023-01-24 142. 145 141. 143. 2.20e7 143. -9.07e-4
#> # … with 2 more variables: ret_closing_prices <dbl>,
#> # cumret_adjusted_prices <dbl>, and abbreviated variable names ¹​price_high,
#> # ²​price_low, ³​price_close, ⁴​price_adjusted, ⁵​ret_adjusted_prices
#> ticker ref_date price_open price_high price_low price_close volume
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 META 2024-05-28 477. 481. 475. 480. 10175800
#> 2 META 2024-05-29 475. 480. 474. 474. 9226200
#> 3 META 2024-05-30 472. 472. 465. 467. 10735200
#> 4 META 2024-05-31 466. 469. 454. 467. 16919800
#> 5 META 2024-06-03 471. 480. 468. 477. 11279400
#> 6 META 2024-06-04 477 479. 473. 477. 7088700
#> # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
#> # ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>
```

# Acknowledgements
Expand Down
27 changes: 14 additions & 13 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ Acknowledgements
BUE
BatchGetSymbols
Buenos
CMD
Codecov
DJI
EOD
Economática
FB
FTSE
GSPC
Henrique
IBOV
NYQ
PETR
Expand All @@ -18,39 +21,37 @@ Reais
SAO
Sao
SimFin
TSLA
YF
YYYY
api
arit
cachefolder
cumret
dir
dontrun
donttest
furrr
futureverse
github
https
json
magrittr
md
mkt
openminded
pingr
pre
quantmod
rOpenSci
rcmdcheck
rds
readme
repo
ret
ropensci
roxygen
tempdir
tibble
yf
cachefolder
github
repo
ropensci
dontrun
donttest
TSLA
pingr
tidyselect
DJI
json
roxygen
Henrique
yf
Binary file modified inst/extdata/fallback-indices/DOW.rds
Binary file not shown.
Binary file modified inst/extdata/fallback-indices/FTSE.rds
Binary file not shown.
Binary file modified inst/extdata/fallback-indices/IBOV.rds
Binary file not shown.
Binary file modified inst/extdata/fallback-indices/SP500.rds
Binary file not shown.
Binary file modified inst/extdata/fallback-indices/testthat-collection.rds
Binary file not shown.

0 comments on commit c46b60d

Please sign in to comment.