next-game-solutions / geckor

An R client for the public CoinGecko API
https://next-game-solutions.github.io/geckor/
Other
26 stars 9 forks source link

historical daily ohlc data of all coins #43

Closed waynelapierre closed 1 year ago

waynelapierre commented 2 years ago

Thank you for this great package. Is there a way to obtain the historical daily ohlc data of all coins? I divided the 13,253 coins supported by CoinGecko into many subsamples with each containing 30 coins and then used the function coin_history_ohlc to get the data in a for loop. This approach failed with an error message saying too many requests.

next-game-solutions commented 2 years ago

Hi @waynelapierre, glad you find the package useful. As per README, "The public CoinGecko API offers a rate limit of 50 calls per minute". Thus, in order to use this free version of their API (and currently it's the only version implemented in geckor), you'd have to introduce a waiting time between calls according to your use case (e.g., using Sys.sleep()), so that you are not going over their API rate limit.

waynelapierre commented 2 years ago

Thanks for the quick reply. Could you incorporate this feature into the package? Another package crypto2 uses this feature to get data from CoinMarketCap: https://github.com/sstoeckl/crypto2

next-game-solutions commented 2 years ago

@waynelapierre many thanks for the suggestion. However, far from all users will need to pull data for large sets of coins, and thus we won't be implementing that feature for the time being (unless we receive more requests from other users). Our initial idea was that users would implement the waiting time logic themselves, in line with their use cases.

waynelapierre commented 2 years ago

OK, I will go ahead and implement it myself. Does one call mean 30 coin ids?

next-game-solutions commented 2 years ago

OK, I will go ahead and implement it myself. Does one call mean 30 coin ids?

One call to a coin_history_* function can retrieve data for up to 30 coin ids; however, this means that under the hood the Coingecko API would be called 30 times.

waynelapierre commented 2 years ago

So one call means one coin regardless of how many rows are queried? Also, I am trying to find the optimal sleep time. Could you provide some insights into that? Below are my codes:

require(data.table)
require(geckor)
coin <- supported_coins()
coin <- coin$coin_id
query_limit <- 30
coin_id <- vector("list", length = ceiling(length(coin)/query_limit))  
coin_ohlc <- vector("list", length = length(coin_id))  

for (i in seq_along(coin_id))
{
  coin_id[[i]] <- coin[(1 + (i - 1)*30):min(i*30, length(coin))]
  coin_ohlc[[i]] <- coin_history_ohlc(coin_id[[i]], vs_currency = "usd", days = "max")
  setDT(coin_ohlc[[i]])
  Sys.sleep(120)
}

coin_ohlc1 <- rbindlist(coin_ohlc)

I got the error message:

Error in curl::curl_fetch_memory(url, handle = handle) : 
  URL using bad/illegal format or missing URL
waynelapierre commented 1 year ago

I am still struggling with the codes above, any suggestions?