invest.data provides tidy wrappers to collect and standardize
investment data from online sources.
The package focuses on four workflows:
- Stock and crypto OHLCV time series (Yahoo Finance, Tiingo, Binance)
- Dividend history (Yahoo Finance)
- Market universes and benchmarks (SP500, ASX, B3 + benchmark list)
- Fundamentals datasets (DoltHub earnings repository)
Most data-retrieval functions return a standardized object:
data: tibble/list with successful resultserrors: tibble with failed symbols/files and error details (NULLwhen no errors)
Several R packages already provide access to financial data:
-
tidyquant::tq_get()
Flexible and general-purpose. Supports Yahoo, Tiingo, Alpha Vantage, and more.
Difference:invest.databuilds ontq_get()but adds query chunking, error logging, and consistent output structures (data+errors). -
quantmod::getSymbols()
One of the oldest tools for financial data in R. Primarily designed for xts/zoo workflows.
Difference:invest.datareturns tibbles, integrates with the tidyverse, and provides crypto support. -
riingo
Official Tiingo client. Useful for low-level Tiingo queries.
Difference:invest.dataabstracts over multiple providers (Yahoo, Tiingo, Binance) with the same interface and adds error handling. -
BatchGetSymbols
Convenient batch retrieval of daily Yahoo Finance data.
Difference:invest.datais more general (intraday + crypto + dividends) and maintains the same return structure across all data types.
In short, invest.data focuses less on being a full-featured API
client, and more on being a unified, resilient interface for
fetching stock, crypto, and dividend data with minimal friction.
You can install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("henrique-anatole/invest.data", dependencies = TRUE)library(invest.data)
# 1) Configure credentials (prefer keyring-backed setup)
set_tiingo_api_key()
# 2) Fetch a symbol universe
universe <- scrap_sp500_symbols()
# 3) Pick symbols and download daily prices
symbols <- universe |> dplyr::slice_head(n = 3) |> dplyr::pull(symbol)
prices <- load_stock_timeseries(
symbols = symbols,
interval = "1d",
start_date = "2024-01-01",
end_date = "2024-03-01"
)
# 4) Work with successful data + errors
prices$data
prices$errorsFor a full walkthrough and troubleshooting guidance, see
vignettes/getting-started.Rmd.
load_stock_timeseries()- Loads stock OHLCV from Yahoo (
"1d") or Tiingo intraday ("1m"to"12h") - Optional split adjustment for Tiingo data (
adjust_splits = TRUE)
- Loads stock OHLCV from Yahoo (
load_crypto_timeseries()- Loads crypto OHLCV from Binance (currently the only supported source)
load_yahoo_dividends()- Loads dividends from Yahoo Finance
scrap_sp500_symbols()scrap_asx_symbols()scrap_b3_symbols()create_benchmarks()
get_fundamentals_data()- Downloads multiple fundamentals tables from DoltHub
is_valid_date()- Validates
YYYY-MM-DDdate strings
- Validates
classificacao_b3: B3 company classification table (sector/subsector metadata)fake_page: sample HTML content for scraping-related tests
Some functions require API keys (e.g., Tiingo for stock data and Binance for crypto data). Both are managed with helper functions:
set_tiingo_api_key()set_binance_api_key()
Each function looks for your credentials in the following order:
- Direct input via function arguments
- Values already set in
options() - Environment variables (
TIINGO_KEY,BIN_KEY,BIN_SECRET) - Secure storage in the system keyring (
keyringpackage)
If no valid key is found, an error is raised with setup instructions.
This is the safest way to store credentials, since they are encrypted and managed by the OS.
# Store keys securely (one-time setup)
keyring::key_set("tiingo_key")
keyring::key_set("binance_key")
keyring::key_set("binance_secret")
# Later, in your session just call:
set_tiingo_api_key()
set_binance_api_key()-
Direct Input (scripts / quick testing):
set_tiingo_api_key("YOUR_TIINGO_KEY") set_binance_api_key("YOUR_BINANCE_KEY", "YOUR_BINANCE_SECRET")
-
Environment Variables (persistent, but stored in plain text): Add to
~/.Renvironor set in your shell:Sys.setenv(TIINGO_KEY = "YOUR_TIINGO_KEY") Sys.setenv(BIN_KEY = "YOUR_BINANCE_KEY", BIN_SECRET = "YOUR_BINANCE_SECRET")
-
Options (only lasts for the current session):
options(tiingo_key = "YOUR_TIINGO_KEY") options(bin_key = "YOUR_BINANCE_KEY", bin_secret = "YOUR_BINANCE_SECRET")
Use keyring whenever possible. Avoid hardcoding keys in scripts.
Load daily prices from Yahoo Finance:
library(invest.data)
stock_test <- load_stock_timeseries(
symbols = c("AAPL", "MSFT", "INVALID"),
interval = "1d",
start_date = "2021-10-01",
end_date = "2022-02-01"
)
head(stock_test$data)
head(stock_test$errors)Load hourly intraday data (Tiingo):
stock_test_hour <- load_stock_timeseries(
symbols = c("AAPL", "MSFT", "INVALID"),
interval = "1h",
start_date = "2021-10-01",
end_date = "2021-10-02"
)
head(stock_test_hour$data)
head(stock_test_hour$errors)Load crypto OHLCV from Binance:
crypto_test <- load_crypto_timeseries(
pair = c("ETHUSDT", "BTCUSDT", "INVALID"),
interval = "1m",
start_date = "2021-01-01",
end_date = "2021-01-02"
)
head(crypto_test$data)
head(crypto_test$errors)Load dividends from Yahoo Finance:
dividends <- load_yahoo_dividends(
symbols = c("AAPL", "MSFT", "INVALID"),
start_date = "2020-01-01",
end_date = "2022-01-01"
)
head(dividends$data)
head(dividends$errors)sp500 <- scrap_sp500_symbols()
benchmarks <- create_benchmarks()
sp500 |> dplyr::count(sector, sort = TRUE)
benchmarks |> dplyr::slice_head(n = 5)fundamentals <- get_fundamentals_data(
files_to_get = c("income_statement", "cash_flow_statement")
)
names(fundamentals$data)
fundamentals$errorsload_*andget_fundamentals_data()return a list withdataanderrors.- Symbol scraping functions (
scrap_*) return a tibble on success and may returnNULLwith a warning on failure. scrap_asx_symbols()andscrap_b3_symbols()require Selenium + Firefox.load_stock_timeseries()uses Tiingo for intraday intervals and Yahoo for daily.load_crypto_timeseries()currently supports only Binance (source = "binance").- Date inputs are expected in
YYYY-MM-DD; useis_valid_date()for validation.
- Upstream providers can enforce rate limits and may throttle or block repeated requests.
- Web scraping sources (ASX/B3 pages) can break when site layout or download flows change.
- Intraday data coverage can vary by provider/symbol and may have gaps.
- Source outages or temporary network issues can produce partial
results; check
errors. RSeleniumworkflows depend on local browser/driver compatibility.
scrap_asx_symbols() and scrap_b3_symbols() depend on browser
automation.
Prerequisites:
- Firefox installed on the machine
- Java runtime available (required by Selenium server)
RSeleniumpackage installed
If Selenium setup fails, start by checking browser/driver compatibility and whether the chosen port is already in use.
For production pipelines, prefer one of these approaches:
- Run scraping in an isolated process/container
- Use fixed environment setup (same Firefox + geckodriver versions)
- Cache scraped outputs and refresh on a schedule
- Record your R and package versions (
sessionInfo()) in analysis outputs. - Pin dependency versions for production jobs (for example with
renv). - For Selenium workflows, keep Firefox and geckodriver versions fixed in CI/containers.
- Rebuild universes and fundamentals on a defined cadence to avoid drift.
- This README was last updated on 2026-06-03.
Extended documentation is available in vignettes/getting-started.Rmd.
| Function | Primary source(s) |
|---|---|
load_stock_timeseries() |
Yahoo Finance (tidyquant::tq_get), Tiingo (tidyquant/riingo) |
load_crypto_timeseries() |
Binance (binancer) |
load_yahoo_dividends() |
Yahoo Finance (tidyquant::tq_get) |
scrap_sp500_symbols() |
tidyquant index + Wikipedia + StockAnalysis |
scrap_asx_symbols() |
MarketIndex |
scrap_b3_symbols() |
B3 IBOV page + internal/external classification |
get_fundamentals_data() |
DoltHub earnings repository |