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")
::install_github("gacolitti/yfinancer") devtools
# Get a ticker object for Apple Inc.
<- get_tickers("AAPL")
apple
# Get historical market data (default: 1 month of daily data)
<- get_history(apple)
history
# Get 1 year of daily data
<- get_history(apple, period = "1y")
history_1y
# Get 1 month of hourly data
<- get_history(apple, period = "1mo", interval = "1h")
history_hourly
# Get data for a custom date range
<- get_history(
history_custom
apple, start = "2022-01-01",
end = "2022-12-31"
)
# Get company information
<- get_info(apple)
info
# Get financial statements
# Annual statements (default)
<- get_income_statement(apple)
income_stmt <- get_balance_sheet(apple)
balance_sheet <- get_cash_flow(apple)
cash_flow
# Get all financial statements at once
<- get_financial_statements(apple)
financials
# Quarterly statements
<- get_income_statement(apple, freq = "quarterly") quarterly_income
# Get multiple tickers
<- get_tickers(c("AAPL", "MSFT", "GOOG"))
tech_tickers
# Get historical data for all tickers
<- get_tickers_history(tech_tickers, period = "1y")
tech_history
# Get company information for all tickers
<- get_tickers_info(tech_tickers) tech_info
# Search for companies with "tech" in their name
<- search_tickers("tech", limit = 20)
tech_search
# Search with news results
<- search_tickers("Apple", quotes_only = FALSE)
results <- results$quotes
quotes <- results$news news
Yahoo 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.