Historical market data"

knitr::opts_chunk$set(
  error = TRUE,
  collapse = TRUE,
  comment = "#>"
)

Sys.sleep(60)
library(geckor)

There are several functions in geckor that can retrieve historical market data for cryptocurrencies. All of these functions have the "coin_history" prefix in their names. In all of the examples described below, we will be collecting historical data for Bitcoin.

Coin-specific market data for a given historical date can be obtained using the coin_history_snapshot() function:

coin_history_snapshot(
  coin_id = "bitcoin",
  date = as.Date("2021-01-01"),
  vs_currencies = c("usd", "eur", "gbp")
)

The coin_history_range() function can be used to query a range of historical dates (specified by the from and to arguments, which expect POSIXct timestamps). Granularity of the returned data depends on the requested range. Hourly data will be retrieved for periods of up to 90 days, and daily data for periods that are longer than 90 days:

# Range of less than 1 day:
coin_history_range(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  from = as.POSIXct("2020-01-01 10:00:10"),
  to = as.POSIXct("2020-01-01 20:45:10")
)

# Range of >90 days:
coin_history_range(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  from = as.POSIXct("2021-01-01 00:00:00"),
  to = as.POSIXct("2021-05-01 00:00:00")
)

To retrieve historical data from the last n days only, use the coin_history():

coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 7
)

In addition to numeric values, the days argument also accepts a character value "max", which results in retrieving the entire existing history of market data for a coin (please note: querying the entire history can take some time):

coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = "max"
)

Notice the different data granularity in the last two examples. Generally, if days = 1 the data will be presented for approximately every 3-8 minutes. If days is between 2 and 90 (inclusive), an hourly time step will be used. Daily data are used for days above 90. One can use the interval argument to control this granularity (by default, interval = NULL). However, at the moment the only value it accepts is "daily":

# Within-day data, with `interval = "daily"`:
coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 1, 
  interval = "daily"
)

# Less than 90 days, with `interval = "daily"`:
coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 10, 
  interval = "daily"
)

# More than 90 days, with `interval = "daily"`:
coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 100, 
  interval = "daily"
)

The open-high-low-close (OHLC) data characterise within-date and between-date price movements of a financial asset. In geckor, this type of data can be retrieved using the coin_history_ohlc() function, which has the same arguments as coin_history(), except for not having the interval argument. Granularity of the retrieved data (i.e. candle's body) depends on the value of days as follows:

The only values currently accepted by the days argument are 1, 7, 14, 30, 90, 180, 365 and "max". Here are some examples:

# 30-minutes granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = 1
)

# 4-hours granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = 7
)

# 4-days granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = 90
)

# 4-days granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = "max"
)

As of v0.2.0 of the package, all of the coin_history_* functions can retrieve data for multiple coins (up to 30) in one call. All one needs to do for that is pass a vector of coin IDs to the coin_id argument. In the following example, we collect market data from the last 2 days for Bitcoin, Cardano and Polkadot:

coin_history(
  coin_id = c("bitcoin", "cardano", "polkadot"),
  vs_currency = "usd",
  days = 2, 
  interval = "daily"
)


Try the geckor package in your browser

Any scripts or data that you put into this service are public.

geckor documentation built on Nov. 1, 2021, 5:07 p.m.