Did it somewhat more general…
indicators.js became:
const CryptoCompareAPI = require("cryptocompare");
const CryptoCompareKey = "..."
CryptoCompareAPI.setApiKey(CryptoCompareKey);
module.exports = {
MovingAverage: function (cryptoAsset, currency, period, depth, callback) {
if (depth>169) {
console.error('Max depth is 169');
} else {
switch (period) {
case 'hour':
CryptoCompareAPI.histoHour(cryptoAsset, currency)
.then(data => callback( calcMA(data, depth)) ).catch(console.error);
break;
case 'day':
CryptoCompareAPI.histoDay(cryptoAsset, currency)
.then(data => callback( calcMA(data, depth)) ).catch(console.error);
break;
case 'minute':
CryptoCompareAPI.histoMinute(cryptoAsset, currency)
.then(data => callback( calcMA(data, depth)) ).catch(console.error);
break;
default:
console.error('Period not available!');
break;
}
}
}
}
function calcMA(data, depth) {
if (data.length > depth) {
data = data.reverse();
var sum = 0;
for(var i=0; i<depth; i++) {
sum += data[i].close;
}
return Math.floor(sum/depth);
} else {
console.error('No data received or only ' + data.length + ' points !');
return -1;
}
}
and is called as (‘day’ can also be ‘hour’ or ‘minute’):
indicators.MovingAverage(‘BTC’, ‘EUR’, ‘day’, 100, function(result) {
console.log(result);
});
With day I get (with 100 as depth): No data received or only 31 points !
(I protected the function calcMA for this as seems needed).