Historical market data

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")
)
#> # A tibble: 3 x 8
#>   coin_id symbol name    date       vs_currency  price   market_cap total_volume
#>   <chr>   <chr>  <chr>   <date>     <chr>        <dbl>        <dbl>        <dbl>
#> 1 bitcoin btc    Bitcoin 2021-01-01 eur         23759.      4.42e11 35613370771.
#> 2 bitcoin btc    Bitcoin 2021-01-01 gbp         21228.      3.95e11 31819429092.
#> 3 bitcoin btc    Bitcoin 2021-01-01 usd         29022.      5.39e11 43503516563.

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). The 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")
)
#> # A tibble: 11 x 5
#>    timestamp           vs_currency price total_volume    market_cap
#>    <dttm>              <chr>       <dbl>        <dbl>         <dbl>
#>  1 2020-01-01 20:00:46 usd         7226. 18034097111. 131054114141.
#>  2 2020-01-01 19:09:39 usd         7234. 18082279778. 131291561459.
#>  3 2020-01-01 18:00:18 usd         7235. 17760548574. 131290371444.
#>  4 2020-01-01 17:00:34 usd         7216. 18563380452. 130600986770.
#>  5 2020-01-01 16:09:55 usd         7213. 18694377689. 130761256011.
#>  6 2020-01-01 15:00:24 usd         7221. 19536563317. 130659235317.
#>  7 2020-01-01 14:07:19 usd         7197. 19829886768. 130590596115.
#>  8 2020-01-01 13:05:26 usd         7203. 19252707591. 130621414174.
#>  9 2020-01-01 12:08:49 usd         7200. 19201215025. 130449996904.
#> 10 2020-01-01 11:05:36 usd         7199. 19197826855. 130544600075.
#> 11 2020-01-01 10:01:04 usd         7189. 19341132038. 130485645003.

# 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")
)
#> # A tibble: 120 x 5
#>    timestamp           vs_currency  price total_volume market_cap
#>    <dttm>              <chr>        <dbl>        <dbl>      <dbl>
#>  1 2021-04-30 00:00:00 usd         53597. 46958036373.    1.00e12
#>  2 2021-04-29 00:00:00 usd         54811. 48072930663.    1.02e12
#>  3 2021-04-28 00:00:00 usd         54992. 48468560013.    1.03e12
#>  4 2021-04-27 00:00:00 usd         53979. 60226926860.    1.01e12
#>  5 2021-04-26 00:00:00 usd         48981. 46233432154.    9.16e11
#>  6 2021-04-25 00:00:00 usd         50133. 49296256746.    9.37e11
#>  7 2021-04-24 00:00:00 usd         51191. 91138220375.    9.57e11
#>  8 2021-04-23 00:00:00 usd         51966. 77897442832.    9.71e11
#>  9 2021-04-22 00:00:00 usd         54190. 56115370322.    1.01e12
#> 10 2021-04-21 00:00:00 usd         56295. 68166802216.    1.06e12
#> # ... with 110 more rows

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

coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 7
)
#> # A tibble: 170 x 6
#>    timestamp           coin_id vs_currency  price total_volume    market_cap
#>    <dttm>              <chr>   <chr>        <dbl>        <dbl>         <dbl>
#>  1 2021-07-12 12:30:00 bitcoin usd         33647. 19996288312. 631260188542.
#>  2 2021-07-12 12:03:22 bitcoin usd         33743. 19808232820. 633653526455.
#>  3 2021-07-12 11:03:35 bitcoin usd         33747. 19452141518. 632679635740.
#>  4 2021-07-12 10:01:10 bitcoin usd         34185. 19123518019. 641341225415.
#>  5 2021-07-12 09:01:16 bitcoin usd         34323. 19132119089. 643695504310.
#>  6 2021-07-12 08:00:25 bitcoin usd         34357. 19020435107. 645137768238.
#>  7 2021-07-12 07:00:49 bitcoin usd         34437. 18900002368. 645846833924.
#>  8 2021-07-12 06:01:40 bitcoin usd         34257. 18490179943. 641826036668.
#>  9 2021-07-12 05:01:03 bitcoin usd         34371. 18719209402. 645243754813.
#> 10 2021-07-12 04:01:38 bitcoin usd         34597. 18402000751. 646774406080.
#> # ... with 160 more rows

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"
)
#> # A tibble: 2,996 x 6
#>    timestamp           coin_id vs_currency  price total_volume    market_cap
#>    <dttm>              <chr>   <chr>        <dbl>        <dbl>         <dbl>
#>  1 2021-07-12 05:15:22 bitcoin usd         34299. 18651967339. 643971913128.
#>  2 2021-07-11 00:00:00 bitcoin usd         33705. 20558304484. 632185737683.
#>  3 2021-07-10 00:00:00 bitcoin usd         33971. 24282108111. 637000370375.
#>  4 2021-07-09 00:00:00 bitcoin usd         32934. 29065577052. 617861633772.
#>  5 2021-07-08 00:00:00 bitcoin usd         33932. 23692220470. 636246944189.
#>  6 2021-07-07 00:00:00 bitcoin usd         34150. 26106856612. 640405812762.
#>  7 2021-07-06 00:00:00 bitcoin usd         33928. 25894820185. 636061920915.
#>  8 2021-07-05 00:00:00 bitcoin usd         35394. 22727930418. 663571591568.
#>  9 2021-07-04 00:00:00 bitcoin usd         34730. 22986053605. 650382046625.
#> 10 2021-07-03 00:00:00 bitcoin usd         33951. 25084046353. 633684390026.
#> # ... with 2,986 more rows

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"
)
#> # A tibble: 2 x 6
#>   timestamp           coin_id vs_currency  price total_volume    market_cap
#>   <dttm>              <chr>   <chr>        <dbl>        <dbl>         <dbl>
#> 1 2021-07-12 12:38:58 bitcoin usd         33593. 20030047163. 630451607644.
#> 2 2021-07-12 00:00:00 bitcoin usd         34300. 17798828844. 643335853171.

# Less than 90 days, with `interval = "daily"`:
coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 10, 
  interval = "daily"
)
#> # A tibble: 11 x 6
#>    timestamp           coin_id vs_currency  price total_volume    market_cap
#>    <dttm>              <chr>   <chr>        <dbl>        <dbl>         <dbl>
#>  1 2021-07-12 12:19:39 bitcoin usd         33635. 19943353772. 631008558036.
#>  2 2021-07-12 00:00:00 bitcoin usd         34300. 17798828844. 643335853171.
#>  3 2021-07-11 00:00:00 bitcoin usd         33705. 20558304484. 632185737683.
#>  4 2021-07-10 00:00:00 bitcoin usd         33971. 24282108111. 637000370375.
#>  5 2021-07-09 00:00:00 bitcoin usd         32934. 29065577052. 617861633772.
#>  6 2021-07-08 00:00:00 bitcoin usd         33932. 23692220470. 636246944189.
#>  7 2021-07-07 00:00:00 bitcoin usd         34150. 26106856612. 640405812762.
#>  8 2021-07-06 00:00:00 bitcoin usd         33928. 25894820185. 636061920915.
#>  9 2021-07-05 00:00:00 bitcoin usd         35394. 22727930418. 663571591568.
#> 10 2021-07-04 00:00:00 bitcoin usd         34730. 22986053605. 650382046625.
#> 11 2021-07-03 00:00:00 bitcoin usd         33951. 25084046353. 633684390026.

# More than 90 days, with `interval = "daily"`:
coin_history(
  coin_id = "bitcoin",
  vs_currency = "usd",
  days = 100, 
  interval = "daily"
)
#> # A tibble: 101 x 6
#>    timestamp           coin_id vs_currency  price total_volume    market_cap
#>    <dttm>              <chr>   <chr>        <dbl>        <dbl>         <dbl>
#>  1 2021-07-12 12:19:54 bitcoin usd         33636. 19946321239. 631008558036.
#>  2 2021-07-12 00:00:00 bitcoin usd         34300. 17798828844. 643335853171.
#>  3 2021-07-11 00:00:00 bitcoin usd         33705. 20558304484. 632185737683.
#>  4 2021-07-10 00:00:00 bitcoin usd         33971. 24282108111. 637000370375.
#>  5 2021-07-09 00:00:00 bitcoin usd         32934. 29065577052. 617861633772.
#>  6 2021-07-08 00:00:00 bitcoin usd         33932. 23692220470. 636246944189.
#>  7 2021-07-07 00:00:00 bitcoin usd         34150. 26106856612. 640405812762.
#>  8 2021-07-06 00:00:00 bitcoin usd         33928. 25894820185. 636061920915.
#>  9 2021-07-05 00:00:00 bitcoin usd         35394. 22727930418. 663571591568.
#> 10 2021-07-04 00:00:00 bitcoin usd         34730. 22986053605. 650382046625.
#> # ... with 91 more rows

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. The 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
)
#> # A tibble: 49 x 7
#>    timestamp           coin_id vs_currency price_open price_high price_low
#>    <dttm>              <chr>   <chr>            <dbl>      <dbl>     <dbl>
#>  1 2021-07-11 13:00:00 bitcoin usd             33802.     33852.    33802.
#>  2 2021-07-11 13:30:00 bitcoin usd             33819.     33819.    33763.
#>  3 2021-07-11 14:00:00 bitcoin usd             33755.     33905.    33755.
#>  4 2021-07-11 14:30:00 bitcoin usd             33929.     34009.    33920.
#>  5 2021-07-11 15:00:00 bitcoin usd             33909.     34067.    33909.
#>  6 2021-07-11 15:30:00 bitcoin usd             34093.     34099.    34041.
#>  7 2021-07-11 16:00:00 bitcoin usd             34025.     34025.    33951.
#>  8 2021-07-11 16:30:00 bitcoin usd             34017.     34037.    33879.
#>  9 2021-07-11 17:00:00 bitcoin usd             33976.     34094.    33976.
#> 10 2021-07-11 17:30:00 bitcoin usd             34080.     34080.    34037.
#> # ... with 39 more rows, and 1 more variable: price_close <dbl>

# 4-hours granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = 7
)
#> # A tibble: 43 x 7
#>    timestamp           coin_id vs_currency price_open price_high price_low
#>    <dttm>              <chr>   <chr>            <dbl>      <dbl>     <dbl>
#>  1 2021-07-05 16:00:00 bitcoin usd             33591.     33813.    33591.
#>  2 2021-07-05 20:00:00 bitcoin usd             33740.     34067.    33478.
#>  3 2021-07-06 00:00:00 bitcoin usd             34207.     34261.    33919.
#>  4 2021-07-06 04:00:00 bitcoin usd             33907.     34174.    33894.
#>  5 2021-07-06 08:00:00 bitcoin usd             33977.     34787.    33977.
#>  6 2021-07-06 12:00:00 bitcoin usd             34901.     34982.    33905.
#>  7 2021-07-06 16:00:00 bitcoin usd             34250.     34441.    34106.
#>  8 2021-07-06 20:00:00 bitcoin usd             34103.     34103.    33895.
#>  9 2021-07-07 00:00:00 bitcoin usd             34105.     34147.    33790.
#> 10 2021-07-07 04:00:00 bitcoin usd             34198.     34251.    34198.
#> # ... with 33 more rows, and 1 more variable: price_close <dbl>

# 4-days granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = 90
)
#> # A tibble: 25 x 7
#>    timestamp           coin_id vs_currency price_open price_high price_low
#>    <dttm>              <chr>   <chr>            <dbl>      <dbl>     <dbl>
#>  1 2021-04-15 00:00:00 bitcoin usd             63577.     63577.    62807.
#>  2 2021-04-19 00:00:00 bitcoin usd             63180.     63180.    56289.
#>  3 2021-04-23 00:00:00 bitcoin usd             55721.     56295.    51966.
#>  4 2021-04-27 00:00:00 bitcoin usd             51191.     53979.    48981.
#>  5 2021-04-30 00:00:00 bitcoin usd             54992.     54992.    53597.
#>  6 2021-05-03 00:00:00 bitcoin usd             57829.     57829.    56601.
#>  7 2021-05-07 00:00:00 bitcoin usd             57200.     57432.    53464.
#>  8 2021-05-11 00:00:00 bitcoin usd             57362.     58772.    55902.
#>  9 2021-05-15 00:00:00 bitcoin usd             56929.     56929.    49913.
#> 10 2021-05-19 00:00:00 bitcoin usd             46781.     46781.    43091.
#> # ... with 15 more rows, and 1 more variable: price_close <dbl>

# 4-days granularity:
coin_history_ohlc(
  coin_id = "bitcoin", 
  vs_currency = "usd",
  days = "max"
)
#> # A tibble: 783 x 7
#>    timestamp           coin_id vs_currency price_open price_high price_low
#>    <dttm>              <chr>   <chr>            <dbl>      <dbl>     <dbl>
#>  1 2013-04-30 00:00:00 bitcoin usd               135.       142.     135. 
#>  2 2013-05-03 00:00:00 bitcoin usd               117        117       91.0
#>  3 2013-05-07 00:00:00 bitcoin usd               111.       118.     106. 
#>  4 2013-05-11 00:00:00 bitcoin usd               113.       119.     113. 
#>  5 2013-05-15 00:00:00 bitcoin usd               115.       117.     114. 
#>  6 2013-05-19 00:00:00 bitcoin usd               116.       124.     116. 
#>  7 2013-05-23 00:00:00 bitcoin usd               123.       126.     123. 
#>  8 2013-05-27 00:00:00 bitcoin usd               132.       135.     129. 
#>  9 2013-05-31 00:00:00 bitcoin usd               129.       132.     127. 
#> 10 2013-06-03 00:00:00 bitcoin usd               129.       129.     121. 
#> # ... with 773 more rows, and 1 more variable: price_close <dbl>