Made a quick little script for getting the current price and MA(9), MA(20), MA(50) and MA(100) from CoinGecko-API (https://www.npmjs.com/package/coingecko-api).
The reason for using CoinGecko is simple: It is free and CryptoCompare is not (only for limited calls).
You’ll need to npm install coingecko-api for this and do additional audit request on two packages!!
// Get CoinGecko API
const CoinGecko = require(‘coingecko-api’);
const CoinGeckoClient = new CoinGecko();
// Formatter for formatting currency
const formatter = new Intl.NumberFormat(‘en-US’, {
style: ‘currency’,
currency: ‘USD’,
minimumFractionDigits: 2
});
// Dates in unixtimestamps
var currentDate = Math.floor(Date.now() / 1000);
var dateMinus100Hours = currentDate - 360000;
// Helper function to transform unixtempstamps into readable dates
// Coingecko does not return hourly data which is accurate on the exact hour
// which is why I set the hour to 00 in the return value
function Unix_timestamp(t)
{
var dt = new Date(t);
var day = paddy(dt.getDate(), 2);
var month = paddy(dt.getMonth()+1, 2);
var year = dt.getFullYear();
var hr = paddy(dt.getHours(), 2);
var m = “0” + dt.getMinutes();
var s = “0” + dt.getSeconds();
return year + ‘-’ + month + ‘-’ + day + ’ ’ + hr + ‘:00’; // + m.substr(-2) + ‘:’ + s.substr(-2);
}
// Padding function for adding leading zero’s
function paddy(num, padlen, padchar) {
var pad_char = typeof padchar !== ‘undefined’ ? padchar : ‘0’;
var pad = new Array(1 + padlen).join(pad_char);
return (pad + num).slice(-pad.length);
}
// Call to CoinGecko to fetch the latest 100 hours price for bitcoin
// Also calculate the MA9, MA20, MA50 and MA100
function fetchMovingAverages(cryptoAsset, bOnlyMAs, callback) {
// results will be the string passed to the callback function
// with all the gathered information
var results = “”;
CoinGeckoClient.coins.fetch(cryptoAsset,
{
tickers: false,
market_data: true,
community_data: false,
developer_data: false,
localization: false,
sparkline: false
})
.then(coinData => {
results += “--------------------------------\r\n”;
results += cryptoAsset + " " + formatter.format(coinData.data.market_data.current_price.usd) + “\r\n”;
results += “--------------------------------\r\n”;
CoinGeckoClient.coins.fetchMarketChartRange(
cryptoAsset,
{
from: dateMinus100Hours,
to: currentDate
}
).then(coinData =>
{
//console.log("--------------------------------");
//console.log(cryptoAsset);
//console.log("--------------------------------");
// coinData has data section with prices section
var prices = coinData.data.prices.reverse(); // from newest to oldest
var sum100 = 0;
var sum9 = 0;
var sum20 = 0;
var sum50 = 0;
// Loop over prices and return readable data to the console log
prices.forEach((item, i) => {
var timeStamp = Unix_timestamp(item[0]);
var priceInUSD = formatter.format(item[1]);
sum100 += item[1];
if (i < 9) sum9 += item[1];
if (i < 20) sum20 += item[1];
if (i < 50) sum50 += item[1];
if (bOnlyMAs == false) results += timeStamp + " " + priceInUSD;
});
var movingAverage100 = formatter.format(sum100/100);
var movingAverage9 = formatter.format(sum9/9);
var movingAverage20 = formatter.format(sum20/20);
var movingAverage50 = formatter.format(sum50/50);
results += "MA(9) = " + movingAverage9 + "\r\n";
results += "MA(20) = " + movingAverage20 + "\r\n";
results += "MA(50) = " + movingAverage50 + "\r\n";
results += "MA(100) = " + movingAverage100 + "\r\n";
callback(results);
}
).catch(error =>
{
console.log(error)
}
);
})
.catch(error => {console.log(error)});
}
// Actual call to fetch information
fetchMovingAverages(‘bitcoin’, true, function(results)
{
console.log(results);
}
);
fetchMovingAverages(‘ethereum’, true, function(results)
{
console.log(results);
}
);
fetchMovingAverages(‘mantra-dao’, true, function(results)
{
console.log(results);
}
);