Retrieves open-high-low-close price data for the last n days

coin_history_ohlc(coin_id, vs_currency = "usd", days, max_attempts = 3)

Arguments

coin_id

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

vs_currency

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

days

(numeric or "max"): number of days to look back. The only acceptable values are 1, 7, 14, 30, 90, 180, 365 and "max". Attempts to assign any other values will fail with the corresponding error message. If days = "max", the entire available history will be retrieved. Depending on the value of days, the time interval used to present the data will differ - see "Details".

max_attempts

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

Value

If the API call succeeds, the function returns a tibble with the following columns:

  • timestamp (POSIXct): beginning of the candle's time interval;

  • coin_id (character): same as the argument coin_id;

  • vs_currency (character): same as the argument vs_currency;

  • price_open (double): coin price in the beginning of a time interval;

  • price_high (double): highest coin price observed within a time interval;

  • price_low (double): lowest coin price observed within a time interval;

  • price_close (double): coin price in the end of a time interval.

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

Details

Granularity of the retrieved data (i.e. candle's body) depends on the value of days as follows:

  • 1 day: 30 minutes;

  • 7 - 30 days: 4 hours;

  • above 30: 4 days.

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.

Examples

if (FALSE) {
r <- coin_history_ohlc(
  coin_id = "cardano",
  vs_currency = "usd",
  days = 7
)
print(r)
}