Skip to content

Instantly share code, notes, and snippets.

@gadelkareem
Last active November 26, 2021 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadelkareem/b2dfc122263975264fc11917063b8957 to your computer and use it in GitHub Desktop.
Save gadelkareem/b2dfc122263975264fc11917063b8957 to your computer and use it in GitHub Desktop.
Cryptocurrency prices calculation in Google Sheets
  • Add your coinMarketCap private key YOUR_PRIVATE_KEY
  • Use =$COUNT_CELL*coingecko($SYMBOL_CELL) in your sheet
function binance(symbol) {
var cache = CacheService.getScriptCache();
var listKey = 'binancePriceList';
var runningKey = 'running_binance';
var response = cache.get(listKey);
while (cache.get(runningKey) && !response) {
Utilities.sleep(500);
}
if(!response){
cache.put(runningKey, true, 1);
const url = `https://api.binance.com/api/v1/ticker/allPrices`
const options = {
"method" : "get",
};
try{
response = UrlFetchApp.fetch(url, options).getContentText();
}catch(e){}
cache.put(listKey, response, 1500);
}
list = JSON.parse(response) || [];
var price
fullSymbol = symbol+'USDT'
for ( i of list) {
if(i.symbol.toLowerCase() === fullSymbol.toLowerCase()){
price = i.price;
break;
}
}
cache.put(runningKey, false, 1);
if(!price){
return coinMarketCap(symbol);
}
return price;
}
/*
* use =$COUNT_CELL*coingecko($SYMBOL_CELL) in your sheet
*/
function coingecko(symbol) {
var cache = CacheService.getScriptCache();
var listKey = 'coingeckoPriceList';
var runningKey = 'running_coingecko';
var response = cache.get(listKey);
while (cache.get(runningKey) && !response) {
Utilities.sleep(500);
}
if(!response){
cache.put(runningKey, true, 1);
const url = `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd`
const options = {
"method" : "get",
};
try{
response = UrlFetchApp.fetch(url, options).getContentText();
}catch(e){}
cache.put(listKey, response, 1500);
}
list = JSON.parse(response) || [];
var price
for ( i of list) {
if(i.symbol.toLowerCase() === symbol.toLowerCase()){
price = i.current_price;
break;
}
}
cache.put(runningKey, false, 1);
if(!price){
return binance(symbol);
}
return price;
}
function coinMarketCap(symbol) {
var cache = CacheService.getScriptCache();
var listKey = `coinMarketCapPriceList_${symbol}`;
var runningKey = 'running_coinMarketCap';
var response = cache.get(listKey);
while (cache.get(runningKey) && !response) {
Utilities.sleep(500);
}
if(!response){
cache.put(runningKey, true, 1);
const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${symbol}&convert=USD`
const headers = {
"X-CMC_PRO_API_KEY": 'YOUR_PRIVATE_KEY'
};
const options = {
"method" : "get",
"headers" : headers
};
try{
response = UrlFetchApp.fetch(url, options).getContentText();
}catch(e){}
cache.put(listKey, response, 1500);
}
list = JSON.parse(response) || [];
if(!list.data[symbol]){
return -1;
}
var price = list.data[symbol].quote.USD.price;
cache.put(runningKey, false, 1);
return price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment