Get quantitative data in tibble
format
Arguments
- x
A single character string, a character vector or tibble representing a single (or multiple) stock symbol, metal symbol, currency combination, FRED code, etc.
- get
A character string representing the type of data to get for
x
. Options include:"stock.prices"
: Get the open, high, low, close, volume and adjusted stock prices for a stock symbol from Yahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getSymbols()
."dividends"
: Get the dividends for a stock symbol from Yahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getDividends()
."splits"
: Get the split ratio for a stock symbol from Yahoo Finance (https://finance.yahoo.com/). Wrapper forquantmod::getSplits()
."stock.prices.japan"
: Get the open, high, low, close, volume and adjusted stock prices for a stock symbol from Yahoo Finance Japan. Wrapper forquantmod::getSymbols.yahooj()
."economic.data"
: Get economic data from FRED. rapper forquantmod::getSymbols.FRED()
."quandl"
: Get data sets from Quandl. Wrapper forQuandl::Quandl()
. See alsoquandl_api_key()
."quandl.datatable"
: Get data tables from Quandl. Wrapper forQuandl::Quandl.datatable()
. See alsoquandl_api_key()
."tiingo"
: Get data sets from Tiingo. Wrapper forriingo::riingo_prices()
. See alsotiingo_api_key()
."tiingo.iex"
: Get data sets from Tiingo. Wrapper forriingo::riingo_iex_prices()
. See alsotiingo_api_key()
."tiingo.crypto"
: Get data sets from Tiingo. Wrapper forriingo::riingo_crypto_prices()
. See alsotiingo_api_key()
."alphavantager"
: Get data sets from Alpha Vantage. Wrapper foralphavantager::av_get()
. See alsoav_api_key()
."rblpapi"
: Get data sets from Bloomberg. Wrapper forRblpapi
. See alsoRblpapi::blpConnect()
to connect to Bloomberg terminal (required). Use the argumentrblpapi_fun
to set the function such as "bdh" (default), "bds", or "bdp".
- complete_cases
Removes symbols that return an NA value due to an error with the get call such as sending an incorrect symbol "XYZ" to get = "stock.prices". This is useful in scaling so user does not need to add an extra step to remove these rows.
TRUE
by default, and a warning message is generated for any rows removed.- ...
Additional parameters passed to the "wrapped" function. Investigate underlying functions to see full list of arguments. Common optional parameters include:
from
: Standardized for time series functions inquantmod
,quandl
,tiingo
,alphavantager
packages. A character string representing a start date in YYYY-MM-DD format.to
: Standardized for time series functions inquantmod
,quandl
,tiingo
,alphavantager
packages. A character string representing a end date in YYYY-MM-DD format.
Details
tq_get()
is a consolidated function that gets data from various
web sources. The function is a wrapper for several quantmod
functions, Quandl
functions, and also gets data from websources unavailable
in other packages.
The results are always returned as a tibble
. The advantages
are (1) only one function is needed for all data sources and (2) the function
can be seamlessly used with the tidyverse: purrr
, tidyr
, and
dplyr
verbs.
tq_get_options()
returns a list of valid get
options you can
choose from.
tq_get_stock_index_options()
Is deprecated and will be removed in the
next version. Please use tq_index_options()
instead.
See also
tq_index()
to get a ful list of stocks in an index.tq_exchange()
to get a ful list of stocks in an exchange.quandl_api_key()
to set the api key for collecting data via the"quandl"
get option.tiingo_api_key()
to set the api key for collecting data via the"tiingo"
get option.av_api_key()
to set the api key for collecting data via the"alphavantage"
get option.
Examples
# Load libraries
# Get the list of `get` options
tq_get_options()
#> [1] "stock.prices" "stock.prices.japan" "dividends"
#> [4] "splits" "economic.data" "quandl"
#> [7] "quandl.datatable" "tiingo" "tiingo.iex"
#> [10] "tiingo.crypto" "alphavantager" "alphavantage"
#> [13] "rblpapi"
# Get stock prices for a stock from Yahoo
aapl_stock_prices <- tq_get("AAPL")
# Get stock prices for multiple stocks
mult_stocks <- tq_get(c("META", "AMZN"),
get = "stock.prices",
from = "2016-01-01",
to = "2017-01-01")
if (FALSE) { # \dontrun{
# --- Quandl ---
if (rlang::is_installed("quandl")) {
quandl_api_key('<your_api_key>')
tq_get("EIA/PET_MTTIMUS1_M", get = "quandl", from = "2010-01-01")
}
# Energy data from EIA
# --- Tiingo ---
if (rlang::is_installed("riingo")) {
tiingo_api_key('<your_api_key>')
# Tiingo Prices (Free alternative to Yahoo Finance!)
tq_get(c("AAPL", "GOOG"), get = "tiingo", from = "2010-01-01")
# Sub-daily prices from IEX ----
tq_get(c("AAPL", "GOOG"),
get = "tiingo.iex",
from = "2020-01-01",
to = "2020-01-15",
resample_frequency = "5min")
# Tiingo Bitcoin Prices ----
tq_get(c("btcusd", "btceur"),
get = "tiingo.crypto",
from = "2020-01-01",
to = "2020-01-15",
resample_frequency = "5min")
}
# --- Alpha Vantage ---
if (rlang::is_installed("alphavantager")) {
av_api_key('<your_api_key>')
# Daily Time Series
tq_get("AAPL",
get = "alphavantager",
av_fun = "TIME_SERIES_DAILY_ADJUSTED",
outputsize = "full")
# Intraday 15 Min Interval
tq_get("AAPL",
get = "alphavantage",
av_fun = "TIME_SERIES_INTRADAY",
interval = "15min",
outputsize = "full")
# FX DAILY
tq_get("USD/EUR", get = "alphavantage", av_fun = "FX_DAILY", outputsize = "full")
# FX REAL-TIME QUOTE
tq_get("USD/EUR", get = "alphavantage", av_fun = "CURRENCY_EXCHANGE_RATE")
}
} # }