
yfinancer provides access to Yahoo Finance’s API,
offering functions to download historical prices, company information,
and financial statements.
You can install the released version of yfinancer from CRAN with:
install.packages("yfinancer")You can install the development version of yfinancer from GitHub with:
# install.packages("devtools")
devtools::install_github("gacolitti/yfinancer")# Get a ticker object for Apple Inc.
apple <- get_tickers("AAPL")
# Get historical market data (default: 1 month of daily data)
history <- get_history(apple)
# Get 1 year of daily data
history_1y <- get_history(apple, period = "1y")
# Get 1 month of hourly data
history_hourly <- get_history(apple, period = "1mo", interval = "1h")
# Get data for a custom date range
history_custom <- get_history(
apple,
start = "2022-01-01",
end = "2022-12-31"
)
# Get company information
info <- get_info(apple)
# Get financial statements
# Annual statements (default)
income_stmt <- get_income_statement(apple)
balance_sheet <- get_balance_sheet(apple)
cash_flow <- get_cash_flow(apple)
# Get all financial statements at once
financials <- get_financial_statements(apple)
# Quarterly statements
quarterly_income <- get_income_statement(apple, freq = "quarterly")# Get multiple tickers
tech_tickers <- get_tickers(c("AAPL", "MSFT", "GOOG"))
# Get historical data for all tickers
tech_history <- get_tickers_history(tech_tickers, period = "1y")
# Get company information for all tickers
tech_info <- get_tickers_info(tech_tickers)# Search for companies with "tech" in their name
tech_search <- search_tickers("tech", limit = 20)
# Search with news results
results <- search_tickers("Apple", quotes_only = FALSE)
quotes <- results$quotes
news <- results$newsYahoo Finance requires two key authentication components for API access of certain endpoints:
The authentication process in yfinancer follows these
steps:
YFINANCE_CRUMB and YFINANCE_A1
environment variables~/.yfinance/auth~/.yfinance/auth for future
use# Set environment variables for authentication
Sys.setenv(YFINANCE_CRUMB = "your-crumb-value")
Sys.setenv(YFINANCE_A1 = "your-a1-cookie-value")This environment variable approach is useful for: - Avoiding authentication failures in automated environments - Testing with specific authentication values
Currently, only the get_info() function requires
authentication.
yfinancer provides access to the following
information:
Yahoo!, Y!Finance, and Yahoo! Finance are registered trademarks of Yahoo, Inc. yfinancer is not affiliated, endorsed, or vetted by Yahoo, Inc. It’s an open-source tool that uses Yahoo’s publicly available APIs, and is intended for research and educational purposes. Users should refer to Yahoo!’s terms of use for details on rights to use the actual data downloaded.