Skip to contents

Based on a ticker (id of a stock) and time period, this function will download stock price data from Yahoo Finance and organizes it in the long format. Yahoo Finance <https://finance.yahoo.com/> provides a vast repository of stock price data around the globe. It cover a significant number of markets and assets, being used extensively in academic research and teaching. In the website you can lookup the ticker of a company.

Usage

yf_get(
  tickers,
  first_date = Sys.Date() - 30,
  last_date = Sys.Date(),
  thresh_bad_data = 0.75,
  bench_ticker = "^GSPC",
  type_return = "arit",
  freq_data = "daily",
  how_to_aggregate = "last",
  do_complete_data = FALSE,
  do_cache = TRUE,
  cache_folder = yf_cachefolder_get(),
  do_parallel = FALSE,
  be_quiet = FALSE
)

Arguments

tickers

A single or vector of tickers. If not sure whether the ticker is available, search for it in YF <https://finance.yahoo.com/>.

first_date

The first date of query (Date or character as YYYY-MM-DD)

last_date

The last date of query (Date or character as YYYY-MM-DD)

thresh_bad_data

A percentage threshold for defining bad data. The dates of the benchmark ticker are compared to each asset. If the percentage of non-missing dates with respect to the benchmark ticker is lower than thresh_bad_data, the function will ignore the asset (default = 0.75)

bench_ticker

The ticker of the benchmark asset used to compare dates. My suggestion is to use the main stock index of the market from where the data is coming from (default = ^GSPC (SP500, US market))

type_return

Type of price return to calculate: 'arit' - arithmetic (default), 'log' - log returns.

freq_data

Frequency of financial data: 'daily' (default), 'weekly', 'monthly', 'yearly'

how_to_aggregate

Defines whether to aggregate the data using the first observations of the aggregating period or last ('first', 'last'). For example, if freq_data = 'yearly' and how_to_aggregate = 'last', the last available day of the year will be used for all aggregated values such as price_adjusted. (Default = "last")

do_complete_data

Return a complete/balanced dataset? If TRUE, all missing pairs of ticker-date will be replaced by NA or closest price (see input do_fill_missing_prices). Default = FALSE.

do_cache

Use cache system? (default = TRUE)

cache_folder

Where to save cache files? (default = yfR::yf_cachefolder_get() )

do_parallel

Flag for using parallel or not (default = FALSE). Before using parallel, make sure you call function future::plan() first. See <https://furrr.futureverse.org/> for more details.

be_quiet

Flag for not printing statements (default = FALSE)

Value

A dataframe with the financial data for working days, when markets are open. All price data is measured at the unit of the financial exchange. For example, price data for META (NYSE/US) is measures in dollars, while price data for PETR3.SA (B3/BR) is measured in Reais (Brazilian currency).

The return dataframe contains the following columns:

ticker

The requested tickers (ids of stocks)

ref_date

The reference day (this can also be year/month/week when using argument freq_data)

price_open

The opening price of the day/period

price_high

The highest price of the day/period

price_close

The close/last price of the day/period

volume

The financial volume of the day/period

price_adjusted

The stock price adjusted for corporate events such as splits, dividends and others -- this is usually what you want/need for studying stocks as it represents the actual financial performance of stockholders

ret_adjusted_prices

The arithmetic or log return (see input type_return) for the adjusted stock prices

ret_adjusted_prices

The arithmetic or log return (see input type_return) for the closing stock prices

cumret_adjusted_prices

The accumulated arithmetic/log return for the period (starts at 100%)

The cache system

The yfR`s cache system is basically a bunch of rds files that are saved every time data is imported from YF. It indexes all data by ticker and time period. Whenever a user asks for a dataset, it first checks if the ticker/time period exists in cache and, if it does, loads the data from the rds file.

By default, a temporary folder is used (see function yf_cachefolder_get, which means that all cache files are session-persistent. In practice, whenever you restart your R/RStudio session, all cache files are lost. This is a choice I've made due to the fact that merging adjusted stock price data after corporate events (dividends/splits) is a mess and prone to errors. This only happens for stock price data, and not indices data.

If you really need a persistent cache folder, which is Ok for indices data, simply set a path with argument cache_folder (see warning section).

Warning

Be aware that when using cache system in a local folder (and not the default tempdir()), the aggregate prices series might not match if a split or dividends event happens in between cache files.

Examples


# \donttest{
tickers <- c("TSLA", "MMM")

first_date <- Sys.Date() - 30
last_date <- Sys.Date()

df_yf <- yf_get(
  tickers = tickers,
  first_date = first_date,
  last_date = last_date
)
#> 
#> ── Running yfR for 2 stocks | 2022-12-31 --> 2023-01-30 (30 days) ──
#> 
#>  Downloading data for benchmark ticker ^GSPC
#>  (1/2) Fetching data for MMM
#> ! 	- not cached
#>  	- cache saved successfully
#>  	- got 18 valid rows (2023-01-03 --> 2023-01-27)
#>  	- got 100% of valid prices -- Youre doing good!
#>  (2/2) Fetching data for TSLA
#> ! 	- not cached
#>  	- cache saved successfully
#>  	- got 18 valid rows (2023-01-03 --> 2023-01-27)
#>  	- got 100% of valid prices -- Time for some tea?
#>  Binding price data
#> 
#> ── Diagnostics ─────────────────────────────────────────────────────────────────
#>  Returned dataframe with 36 rows -- Nice!
#>  Using 280.6 kB at /tmp/Rtmpbzumde/yf_cache for 90 cache files
#>  Out of 2 requested tickers, you got 2 (100%)

print(df_yf)
#> # A tibble: 36 × 11
#>    ticker ref_date   price_open price_…¹ price…² price…³ volume price…⁴ ret_ad…⁵
#>  * <chr>  <date>          <dbl>    <dbl>   <dbl>   <dbl>  <dbl>   <dbl>    <dbl>
#>  1 MMM    2023-01-03       122.     123.    120.    122. 2.61e6    122. NA      
#>  2 MMM    2023-01-04       123.     125.    123.    125. 2.77e6    125.  2.19e-2
#>  3 MMM    2023-01-05       124.     125.    122.    123. 2.61e6    123. -1.75e-2
#>  4 MMM    2023-01-06       125.     127.    124.    127. 2.42e6    127.  3.06e-2
#>  5 MMM    2023-01-09       127      129.    126.    127. 2.87e6    127.  5.52e-4
#>  6 MMM    2023-01-10       127.     128.    127.    128. 1.65e6    128.  1.06e-2
#>  7 MMM    2023-01-11       129.     129.    128.    129. 2.01e6    129.  7.73e-3
#>  8 MMM    2023-01-12       130.     130.    128.    129. 1.84e6    129.  1.78e-3
#>  9 MMM    2023-01-13       128.     130.    128.    130. 1.69e6    130.  1.24e-3
#> 10 MMM    2023-01-17       130.     130.    126.    127. 3.51e6    127. -2.25e-2
#> # … with 26 more rows, 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
# }