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

# 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 6
#>    timestamp           coin_id vs_currency  price  total_volume    market_cap
#>    <dttm>              <chr>   <chr>        <dbl>         <dbl>         <dbl>
#>  1 2021-01-01 00:00:00 bitcoin usd         29022.  43503516563. 539438036436.
#>  2 2021-01-02 00:00:00 bitcoin usd         29352.  34089717988. 545593282215.
#>  3 2021-01-03 00:00:00 bitcoin usd         32164.  57273436641. 597887713054.
#>  4 2021-01-04 00:00:00 bitcoin usd         33008. 178894068361. 613616917626.
#>  5 2021-01-05 00:00:00 bitcoin usd         31516.  74657165356. 585726270249.
#>  6 2021-01-06 00:00:00 bitcoin usd         34082.  67420500716. 633651671519.
#>  7 2021-01-07 00:00:00 bitcoin usd         36934.  71027195898. 682743683563.
#>  8 2021-01-08 00:00:00 bitcoin usd         39547.  78642959829. 735339707368.
#>  9 2021-01-09 00:00:00 bitcoin usd         40816.  82846468856. 758971014787.
#> 10 2021-01-10 00:00:00 bitcoin usd         40297.  55921808672. 749347201463.
#> # ... 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: 169 x 6
#>    timestamp           coin_id vs_currency  price total_volume market_cap
#>    <dttm>              <chr>   <chr>        <dbl>        <dbl>      <dbl>
#>  1 2021-10-25 10:08:06 bitcoin usd         63063. 33543647700.    1.19e12
#>  2 2021-10-25 11:02:52 bitcoin usd         63081. 33803461984.    1.19e12
#>  3 2021-10-25 12:03:57 bitcoin usd         63071. 33616526475.    1.19e12
#>  4 2021-10-25 13:11:50 bitcoin usd         62864. 31821229171.    1.19e12
#>  5 2021-10-25 14:01:14 bitcoin usd         63401. 32418786297.    1.19e12
#>  6 2021-10-25 15:02:46 bitcoin usd         63684. 32660241934.    1.20e12
#>  7 2021-10-25 16:14:51 bitcoin usd         63894. 31387424447.    1.20e12
#>  8 2021-10-25 17:02:47 bitcoin usd         63917. 31338439518.    1.21e12
#>  9 2021-10-25 18:05:00 bitcoin usd         63432. 31782358312.    1.20e12
#> 10 2021-10-25 19:00:48 bitcoin usd         63355. 31901138418.    1.19e12
#> # ... with 159 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"
)
#> Warning: Missing values found in column(s)
#> * market_cap
#> # A tibble: 3,108 x 6
#>    timestamp           coin_id vs_currency price total_volume market_cap
#>    <dttm>              <chr>   <chr>       <dbl>        <dbl>      <dbl>
#>  1 2013-04-28 00:00:00 bitcoin usd         135.             0 1500517590
#>  2 2013-04-29 00:00:00 bitcoin usd         142.             0 1575032004
#>  3 2013-04-30 00:00:00 bitcoin usd         135.             0 1501657493
#>  4 2013-05-01 00:00:00 bitcoin usd         117              0 1298951550
#>  5 2013-05-02 00:00:00 bitcoin usd         103.             0 1148667722
#>  6 2013-05-03 00:00:00 bitcoin usd          91.0            0 1011066494
#>  7 2013-05-04 00:00:00 bitcoin usd         111.             0 1236351844
#>  8 2013-05-05 00:00:00 bitcoin usd         117.             0 1298377788
#>  9 2013-05-06 00:00:00 bitcoin usd         118.             0 1315992304
#> 10 2013-05-07 00:00:00 bitcoin usd         106.             0 1183766500
#> # ... with 3,098 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-11-01 00:00:00 bitcoin usd         61472. 32494948461.    1.16e12
#> 2 2021-11-01 09:49:40 bitcoin usd         62187. 35443954864.    1.17e12

# 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-10-23 00:00:00 bitcoin usd         61029. 40766460605.    1.15e12
#>  2 2021-10-24 00:00:00 bitcoin usd         61572. 28505039148.    1.16e12
#>  3 2021-10-25 00:00:00 bitcoin usd         61173. 29982535557.    1.15e12
#>  4 2021-10-26 00:00:00 bitcoin usd         63228. 30852852368.    1.19e12
#>  5 2021-10-27 00:00:00 bitcoin usd         60604. 33233702858.    1.14e12
#>  6 2021-10-28 00:00:00 bitcoin usd         58641. 43976252157.    1.11e12
#>  7 2021-10-29 00:00:00 bitcoin usd         60768. 52318597557.    1.15e12
#>  8 2021-10-30 00:00:00 bitcoin usd         62283. 38648967245.    1.18e12
#>  9 2021-10-31 00:00:00 bitcoin usd         61837. 31810216330.    1.17e12
#> 10 2021-11-01 00:00:00 bitcoin usd         61472. 32494948461.    1.16e12
#> 11 2021-11-01 09:50:53 bitcoin usd         62183. 35455287604.    1.17e12

# 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-25 00:00:00 bitcoin usd         34214. 22120323672. 642012590041.
#>  2 2021-07-26 00:00:00 bitcoin usd         35456. 20929083221. 664681184169.
#>  3 2021-07-27 00:00:00 bitcoin usd         37282. 53550491998. 701921012035.
#>  4 2021-07-28 00:00:00 bitcoin usd         39077. 36401287454. 732311951673.
#>  5 2021-07-29 00:00:00 bitcoin usd         40031. 41369107423. 751371299911.
#>  6 2021-07-30 00:00:00 bitcoin usd         39978. 28717414113. 750407963580.
#>  7 2021-07-31 00:00:00 bitcoin usd         41936. 35530501783. 786531956384.
#>  8 2021-08-01 00:00:00 bitcoin usd         41754. 28437941447. 784039345926.
#>  9 2021-08-02 00:00:00 bitcoin usd         39915. 28555173662. 747403845432.
#> 10 2021-08-03 00:00:00 bitcoin usd         39279. 28057528866. 737440183776.
#> # ... 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. 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-10-31 10:00:00 bitcoin usd             60876.     60876.    60673.
#>  2 2021-10-31 10:30:00 bitcoin usd             60736.     60895.    60629.
#>  3 2021-10-31 11:00:00 bitcoin usd             60960.     61209.    60960.
#>  4 2021-10-31 11:30:00 bitcoin usd             61033.     61033.    60763.
#>  5 2021-10-31 12:00:00 bitcoin usd             60723.     60723.    60601.
#>  6 2021-10-31 12:30:00 bitcoin usd             60528.     60900.    60491.
#>  7 2021-10-31 13:00:00 bitcoin usd             60860.     60860.    60307.
#>  8 2021-10-31 13:30:00 bitcoin usd             60510.     60737.    60510.
#>  9 2021-10-31 14:00:00 bitcoin usd             60475.     60517.    60366.
#> 10 2021-10-31 14:30:00 bitcoin usd             60542.     60798.    60485.
#> # ... 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-10-25 12:00:00 bitcoin usd             63063.     63081.    63063.
#>  2 2021-10-25 16:00:00 bitcoin usd             63071.     63684.    62864.
#>  3 2021-10-25 20:00:00 bitcoin usd             63894.     63917.    63355.
#>  4 2021-10-26 00:00:00 bitcoin usd             63038.     63333.    62892.
#>  5 2021-10-26 04:00:00 bitcoin usd             63228.     63267.    63034.
#>  6 2021-10-26 08:00:00 bitcoin usd             63153.     63153.    62675.
#>  7 2021-10-26 12:00:00 bitcoin usd             63031.     63238.    62930.
#>  8 2021-10-26 16:00:00 bitcoin usd             63022.     63022.    61973.
#>  9 2021-10-26 20:00:00 bitcoin usd             62881.     62881.    62191.
#> 10 2021-10-27 00:00:00 bitcoin usd             62281.     62336.    60290.
#> # ... 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: 24 x 7
#>    timestamp           coin_id vs_currency price_open price_high price_low
#>    <dttm>              <chr>   <chr>            <dbl>      <dbl>     <dbl>
#>  1 2021-08-07 00:00:00 bitcoin usd             38368.     42802.    38368.
#>  2 2021-08-11 00:00:00 bitcoin usd             44648.     46311     43753.
#>  3 2021-08-15 00:00:00 bitcoin usd             45652.     47717.    44495.
#>  4 2021-08-19 00:00:00 bitcoin usd             47025      47025     44534.
#>  5 2021-08-23 00:00:00 bitcoin usd             46745.     49251.    46745.
#>  6 2021-08-27 00:00:00 bitcoin usd             49519.     49519.    47229.
#>  7 2021-08-31 00:00:00 bitcoin usd             49083.     49083.    47124.
#>  8 2021-09-03 00:00:00 bitcoin usd             47335.     49339.    47335.
#>  9 2021-09-07 00:00:00 bitcoin usd             49935.     52740.    49935.
#> 10 2021-09-11 00:00:00 bitcoin usd             46995.     46995.    44803.
#> # ... with 14 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: 812 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 802 more rows, and 1 more variable: price_close <dbl>

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"
)
#> # A tibble: 9 x 6
#>   timestamp           coin_id  vs_currency    price total_volume market_cap
#>   <dttm>              <chr>    <chr>          <dbl>        <dbl>      <dbl>
#> 1 2021-10-31 00:00:00 bitcoin  usd         61837.   31810216330.    1.17e12
#> 2 2021-11-01 00:00:00 bitcoin  usd         61472.   32494948461.    1.16e12
#> 3 2021-11-01 09:33:52 bitcoin  usd         62090.   35003885800.    1.17e12
#> 4 2021-10-31 00:00:00 cardano  usd             1.95  1583695724.    6.26e10
#> 5 2021-11-01 00:00:00 cardano  usd             1.97  1841470221.    6.32e10
#> 6 2021-11-01 09:51:01 cardano  usd             2.00  2003998194.    6.35e10
#> 7 2021-10-31 00:00:00 polkadot usd            42.7    921954043.    4.46e10
#> 8 2021-11-01 00:00:00 polkadot usd            42.8   1166429150.    4.49e10
#> 9 2021-11-01 09:51:06 polkadot usd            44.3   1330855309.    4.57e10