Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

invest.data

invest.data provides tidy wrappers to collect and standardize investment data from online sources.

The package focuses on four workflows:

  1. Stock and crypto OHLCV time series (Yahoo Finance, Tiingo, Binance)
  2. Dividend history (Yahoo Finance)
  3. Market universes and benchmarks (SP500, ASX, B3 + benchmark list)
  4. Fundamentals datasets (DoltHub earnings repository)

Most data-retrieval functions return a standardized object:

  • data: tibble/list with successful results
  • errors: tibble with failed symbols/files and error details (NULL when no errors)

How does it compare to other packages?

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.data builds on tq_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.data returns tibbles, integrates with the tidyverse, and provides crypto support.

  • riingo
    Official Tiingo client. Useful for low-level Tiingo queries.
    Difference: invest.data abstracts 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.data is 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.

Installation

You can install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("henrique-anatole/invest.data", dependencies = TRUE)

Minimal quickstart

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$errors

For a full walkthrough and troubleshooting guidance, see vignettes/getting-started.Rmd.

Main functions

Time series and dividends

  • load_stock_timeseries()
    • Loads stock OHLCV from Yahoo ("1d") or Tiingo intraday ("1m" to "12h")
    • Optional split adjustment for Tiingo data (adjust_splits = TRUE)
  • load_crypto_timeseries()
    • Loads crypto OHLCV from Binance (currently the only supported source)
  • load_yahoo_dividends()
    • Loads dividends from Yahoo Finance

Symbol universes and benchmarks

  • scrap_sp500_symbols()
  • scrap_asx_symbols()
  • scrap_b3_symbols()
  • create_benchmarks()

Fundamentals and utilities

  • get_fundamentals_data()
    • Downloads multiple fundamentals tables from DoltHub
  • is_valid_date()
    • Validates YYYY-MM-DD date strings

Included datasets

  • classificacao_b3: B3 company classification table (sector/subsector metadata)
  • fake_page: sample HTML content for scraping-related tests

API Key Setup

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:

  1. Direct input via function arguments
  2. Values already set in options()
  3. Environment variables (TIINGO_KEY, BIN_KEY, BIN_SECRET)
  4. Secure storage in the system keyring (keyring package)

If no valid key is found, an error is raised with setup instructions.

Recommended method: keyring (secure storage)

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()

Alternative methods (less secure)

  • 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 ~/.Renviron or 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.

Examples

Stock OHLCV (daily via Yahoo)

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)

Stock OHLCV (intraday via Tiingo)

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)

Crypto OHLCV (Binance)

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)

Dividends (Yahoo)

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)

Market universes and benchmarks

sp500 <- scrap_sp500_symbols()
benchmarks <- create_benchmarks()

sp500 |> dplyr::count(sector, sort = TRUE)
benchmarks |> dplyr::slice_head(n = 5)

Fundamentals tables

fundamentals <- get_fundamentals_data(
  files_to_get = c("income_statement", "cash_flow_statement")
)

names(fundamentals$data)
fundamentals$errors

Operational notes

  • load_* and get_fundamentals_data() return a list with data and errors.
  • Symbol scraping functions (scrap_*) return a tibble on success and may return NULL with a warning on failure.
  • scrap_asx_symbols() and scrap_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; use is_valid_date() for validation.

Known limitations

  • 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.
  • RSelenium workflows depend on local browser/driver compatibility.

RSelenium setup (ASX/B3)

scrap_asx_symbols() and scrap_b3_symbols() depend on browser automation.

Prerequisites:

  • Firefox installed on the machine
  • Java runtime available (required by Selenium server)
  • RSelenium package 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

Reproducibility notes

  • 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.

Data sources

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages