Retrieves coin-specific market data for the last n days
coin_history(
coin_id,
vs_currency = "usd",
days,
interval = NULL,
max_attempts = 3
)
(character): ID of the coin of interest or a vector with several IDs.
The maximum number of coins that can be queried simultaneously is 5.
An up-to-date list of supported coins and their IDs can be retrieved
with the supported_coins()
function.
(character): name of the reference currency to
express the price in. An up-to-date list of supported reference currencies (both
fiat and cryptocurrencies) can be obtained with the supported_currencies()
function. If an unsupported vs_currency
is requested, the call will
fail with the respective error message.
(numeric or "max"
): number of days to look back.
If days = "max"
, the entire available history for coin_id
will be
retrieved. Depending on the value of days
, the time interval used to
present the data will differ - see "Details".
(character or NULL
): time interval used to present the data.
The only currently supported value is daily
. Defaults to NULL
.
(double, positive): specifies the maximum number of attempts to call the CoinGecko API (e.g., if the first call fails for some reason). Additional attempts are implemented with an exponential backoff. Defaults to 3.
If the API call succeeds, the function returns a tibble with the following columns:
timestamp
(POSIXct);
coin_id
(character): same as the argument coin_id
;
vs_currency
(character): same as the argument vs_currency
;
price
(double): coin price, as of timestamp
;
total_volume
(double): a 24 hours rolling-window trading volume, as
of timestamp
;
market_cap
(double): market capitalisation, as of timestamp
.
If no data can be retrieved (e.g., because of going over the API
rate limit or mis-specifying the query parameters), the function
returns nothing (NULL
).
If days = 1
and interval = NULL
, the data will be returned for
every few minutes (typically 3-8 minutes). If days
is between 2 and 90
(inclusive) and interval = NULL
, an (approximately) hourly time step will
be used. Daily data are used for days
above 90. If interval = "daily"
,
daily data will be used irrespective of the value of days
.
Sometimes, the retrieved data will contain missing values. In such cases, the function will issue a warning and show a list of columns that have missing values.
This function is based on the public CoinGecko API, which has a rate limit of 10-30 calls per minute. Please keep this limit in mind when developing your applications.
if (FALSE) {
r <- coin_history(coin_id = "bitcoin", vs_currency = "usd", days = 30)
print(r)
}