Fetch CoinGecko history by numeric ID (incl. partial survivorship-bias correction)
Source:R/cg_recover.R
cg_history_by_id.RdCompanion to cg_history() that addresses coins by their numeric
CoinGecko ID instead of their slug. Useful in two scenarios:
Arguments
- ids
Integer vector of numeric IDs to fetch. Default
NULL-> usescg_list()$id(active universe). To extend coverage to delisted coins, supply the union of historically-observed IDs from your accumulated snapshots.- what
Subset of streams to fetch. Any combination of
"price","market_cap", and"ohlc". Default all three.- vs_currency
Quote currency, default
"usd".- start_date, end_date
Client-side date filter applied after fetch.
NULLreturns full history.- coin_list
Optional
cg_list()output used to joinslug/name/symbolonto recovered rows for coins still in the active universe. IfNULL, callscg_list()automatically. Set toFALSEto skip the join (rows then have onlyid).- sleep, wait, max_retries
Rate-limit knobs. Defaults
0.6 / 60 / 3matchcg_history().- quiet
If
FALSE, prints a progress bar.- finalWait
Sleep 60 s after the last call (mirrors
crypto_history()).- date_convention
Either
"end_of_day"(the default) or"raw". Seecg_history()for the explanation – this argument applies the same -1 day shift to midnight-UTC ticks so dates align with the CMC / CRSP / Compustat end-of-day convention.
Value
Tibble with one row per (id, date) using crypto2-compatible column names. Columns:
- id
CoinGecko numeric id (always populated).
- slug, name, symbol
Coin identifiers –
NAfor ids no longer in the active universe (i.e. delisted on the slug side).- timestamp
POSIXct UTC midnight of the trading day.
- ref_cur_id, ref_cur_name
Quote currency.
- open, high, low, close, volume, market_cap
Daily values.
Details
Coins that have been delisted and whose slug no longer resolves.
Cronjob-style accumulation: when you persist
cg_list()snapshots over time, the union of all numeric IDs ever observed is the survivorship-bias-corrected universe.cg_history_by_id()lets you refetch each historical ID directly without needing its current slug to still resolve.
Important caveats – please read:
The numeric-ID space is sparse, not dense. Blind iteration over
1:Ndoes NOT recover the full universe – most numeric IDs in that range have no data. The defaultids = NULLtherefore uses the active universe fromcg_list(), not a numeric range. To recover delisted coins you must supply the IDs explicitly (e.g., the union of accumulatedcg_list()snapshots, or the historic mapping fromcg_id_mapping()).Slug recovery for delisted coins is not generally available on the free tier. Active coins get their slug/name joined back in from
cg_list(); rows whose numeric ID is no longer in the active universe come back withslug = NAandname = NA. Use theidcolumn as the join key in downstream code. For a one-shot complete recovery of the full historic universe seevignette("coingecko-pro-backfill").
Examples
if (FALSE) { # \dontrun{
# Scan first 200 numeric IDs (will include both active and delisted coins)
h <- cg_history_by_id(ids = 1:200, what = c("price", "market_cap"))
# Full sweep -- survivorship-bias-free price history of the entire
# CoinGecko universe. Slow (10+ hours). Run via cronjob package.
h_all <- cg_history_by_id()
} # }