Author: Marcelo Perlin
Date: 2026-02-02

# Getting Started

#### Marcelo Perlin

#### 2026-02-02

## Examples

Here you’ll find a series of example of calls to `yf_get()`. Most arguments are self-explanatory, but you can find more details at the help files.

The steps of the algorithm are:

1. check cache files for existing data
2. if not in cache, fetch stock prices from YF and clean up the raw data
3. write cache file if not available
4. calculate all returns
5. build diagnostics
6. return the data to the user

### Fetching a single stock price

    library(yfR)

    # set options for algorithm
    my_ticker <- 'GM'
    first_date <- Sys.Date() - 30
    last_date <- Sys.Date()

    # fetch data
    df_yf <- yf_get(tickers = my_ticker,
                    first_date = first_date,
                    last_date = last_date)

    # output is a tibble with data
    head(df_yf)

    ## # A tibble: 6 × 11
    ##   ticker ref_date   price_open price_high price_low price_close   volume
    ##   <chr>  <date>          <dbl>      <dbl>     <dbl>       <dbl>    <dbl>
    ## 1 GM     2026-01-05       80.5       83.4      80.0        83.2 10468400
    ## 2 GM     2026-01-06       82.7       82.8      81.2        82.2  7972900
    ## 3 GM     2026-01-07       82.1       82.7      81.6        81.9  6362000
    ## 4 GM     2026-01-08       82.8       85.2      82.4        85.1 11643400
    ## 5 GM     2026-01-09       83.5       84.4      81          82.9 12142900
    ## 6 GM     2026-01-12       82.2       82.9      81.1        82.9  7376400
    ## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
    ## #   ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>

### Fetching many stock prices

    library(yfR)
    library(ggplot2)

    my_ticker <- c('TSLA', 'GM', 'MMM')
    first_date <- Sys.Date() - 100
    last_date <- Sys.Date()

    df_yf_multiple <- yf_get(tickers = my_ticker,
                             first_date = first_date,
                             last_date = last_date)

    p <- ggplot(df_yf_multiple, aes(x = ref_date, y = price_adjusted,
                                    color = ticker)) +
      geom_line()

    p

### Fetching daily/weekly/monthly/yearly price data

    library(yfR)
    library(ggplot2)
    library(dplyr)

    my_ticker <- 'GE'
    first_date <- '2005-01-01'
    last_date <- Sys.Date()

    df_dailly <- yf_get(tickers = my_ticker,
                        first_date, last_date,
                        freq_data = 'daily') %>%
      mutate(freq = 'daily')

    df_weekly <- yf_get(tickers = my_ticker,
                        first_date, last_date,
                        freq_data = 'weekly') %>%
      mutate(freq = 'weekly')

    df_monthly <- yf_get(tickers = my_ticker,
                         first_date, last_date,
                         freq_data = 'monthly') %>%
      mutate(freq = 'monthly')

    df_yearly <- yf_get(tickers = my_ticker,
                        first_date, last_date,
                        freq_data = 'yearly') %>%
      mutate(freq = 'yearly')

    # bind it all together for plotting
    df_allfreq <- bind_rows(
      list(df_dailly, df_weekly, df_monthly, df_yearly)
    ) %>%
      mutate(freq = factor(freq,
                           levels = c('daily',
                                      'weekly',
                                      'monthly',
                                      'yearly'))) # make sure the order in plot is right

    p <- ggplot(df_allfreq, aes(x = ref_date, y = price_adjusted)) +
      geom_line() +
      facet_grid(freq ~ ticker) +
      theme_minimal() +
      labs(x = '', y = 'Adjusted Prices')

    print(p)

### Changing format to wide

    library(yfR)
    library(ggplot2)

    my_ticker <- c('TSLA', 'GM', 'MMM')
    first_date <- Sys.Date() - 100
    last_date <- Sys.Date()

    df_yf_multiple <- yf_get(tickers = my_ticker,
                             first_date = first_date,
                             last_date = last_date)

    print(df_yf_multiple)

    ## # A tibble: 198 × 11
    ##    ticker ref_date   price_open price_high price_low price_close   volume
    ##  * <chr>  <date>          <dbl>      <dbl>     <dbl>       <dbl>    <dbl>
    ##  1 GM     2025-10-27       69.6       69.6      68.8        69.4  9724800
    ##  2 GM     2025-10-28       69.5       70.2      68.7        69.9 13072200
    ##  3 GM     2025-10-29       69.6       70.2      68.6        69.1 10575900
    ##  4 GM     2025-10-30       68.7       69.8      68.4        68.7  7559500
    ##  5 GM     2025-10-31       68.8       69.3      68.6        69.1  6993600
    ##  6 GM     2025-11-03       68.9       68.9      67.8        68.2 10252300
    ##  7 GM     2025-11-04       68         68.1      66.8        66.9  8478200
    ##  8 GM     2025-11-05       67.0       69.3      67.0        68.8  7807000
    ##  9 GM     2025-11-06       68.7       69.4      68.2        68.8  6086300
    ## 10 GM     2025-11-07       68.9       70.8      68.6        70.8  8408500
    ## # ℹ 188 more rows
    ## # ℹ 4 more variables: price_adjusted <dbl>, ret_adjusted_prices <dbl>,
    ## #   ret_closing_prices <dbl>, cumret_adjusted_prices <dbl>

    l_wide <- yf_convert_to_wide(df_yf_multiple)

    names(l_wide)

    ## [1] "price_open"             "price_high"             "price_low"
    ## [4] "price_close"            "volume"                 "price_adjusted"
    ## [7] "ret_adjusted_prices"    "ret_closing_prices"     "cumret_adjusted_prices"

    prices_wide <- l_wide$price_adjusted
    head(prices_wide)

    ## # A tibble: 6 × 4
    ##   ref_date      GM   MMM  TSLA
    ##   <date>     <dbl> <dbl> <dbl>
    ## 1 2025-10-27  69.2  168.  452.
    ## 2 2025-10-28  69.7  166.  461.
    ## 3 2025-10-29  69.0  164.  462.
    ## 4 2025-10-30  68.5  166.  440.
    ## 5 2025-10-31  69.0  166.  457.
    ## 6 2025-11-03  68.1  161.  468.