Creating Daily and Minutely MA - Practical Assignment

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).

My indicators build :

module.exports = {

HourlyMovingAverage:function(cryptoAsset,fiatcurrency,hours,callback){
if (hours > 169){
console.error(“Only hours Up to 169 allowed”)
return
}
CryptoCompareAPI.histoHour( cryptoAsset, fiatcurrency)
.then(data => {
data = data.reverse() // to reverse array index first => last; last becomes first
var sum = 0;
for(var i = 0;i<hours;i++){
console.log(i);
console.log(data[i].close)
sum+=data[i].close ;
}
var MovingAvarage = sum/hours ;
callback(MovingAvarage);
//console.log(data[0]) // console.log(data) to print all 169
// console.log(data.length) // just print the nr of samples …//
})
.catch(console.error)
}

//==========================================================================
, DailyMovingAverage:function(cryptoAsset,fiatcurrency,days,callback){
if (days > 31){
console.error(“Only 31 max days allowed”)
return
}
CryptoCompareAPI.histoDay( cryptoAsset, fiatcurrency)
.then(data => {
data = data.reverse() // to reverse array index first => last; last becomes first
var sum = 0;
for(var i = 0;i<days;i++){
console.log(i);
console.log(data[i].close)
sum+=data[i].close ;
}
var MovingAvarage = sum/days ;
callback(MovingAvarage);
//console.log(data[0]) // console.log(data) to print all 169
console.log(data.length) // just print the nr of samples …//
})
.catch(console.error)
}

//==========================================================================
, MinuteMovingAverage:function(cryptoAsset,fiatcurrency,minutes,callback){
if (minutes > 1440){
console.error(“Only 1440 minutes allowed”)
return
}
CryptoCompareAPI.histoMinute( cryptoAsset, fiatcurrency)
.then(data => {
data = data.reverse() // to reverse array index first => last; last becomes first
var sum = 0;
for(var i = 0;i<minutes;i++){
console.log(i);
console.log(data[i].close)
sum+=data[i].close ;
}
var MovingAvarage = sum/minutes ;
callback(MovingAvarage);
//console.log(data[0]) // console.log(data) to print all 169
console.log(data.length) // just print the nr of samples …//
})
.catch(console.error)
}
}

The index.js file

// Homework building indicators Houtly,Minutes,Daily
// Date 25 juni 2020
indicators.HourlyMovingAverage(“btc”,“usd”,30,function(result){
console.log("MA Hourly: ",result)
})

indicators.DailyMovingAverage(“eth”,“usd”,20,function(result){
console.log("MA daily: ",result)
})

indicators.MinuteMovingAverage(“bch”,“usd”,15,function(result){
console.log("MA minute: ",result)
})

INDEX.JS

indicators.minutelyMovingAverage(“ADA”,“USD”,1000,function(result){
console.log("MinMA: ", result)
})
indicators.hourlyMovingAverage(“ADA”,“USD”,169,function(result){
console.log("HourMA: ", result)
})
indicators.dailyMovingAverage(“ADA”,“USD”,31,function(result){
console.log("DayMA: ", result)
})

INDICATORS.JS

module.exports = {

dailyMovingAverage:function(cryptoAsset,fiatCurrency,days,callback){

if(days>31){
console.log(“Only up to 31 days allowed”)
}
CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
.then(data => {

data = data.reverse(i)
var sum = 0;
for(var i = 0;i<days;i++){
sum+=data[i].close;

}

var movingAverage = sum/days;
callback(movingAverage);
})
.catch(console.error)
},
hourlyMovingAverage:function(cryptoAsset,fiatCurrency,hours,callback){

if(hours>169){
console.log(“Only up to 169 hours allowed”)
}
CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
.then(data => {

data = data.reverse(i)
var sum = 0;
for(var i = 0;i<hours;i++){
sum+=data[i].close;

}

var movingAverage = sum/hours;
callback(movingAverage);
})
.catch(console.error)
},
minutelyMovingAverage:function(cryptoAsset,fiatCurrency,minutes,callback){

if(minutes>1440){
console.log(“Only up to 1440 minutes allowed”)
}
CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
.then(data => {

data = data.reverse(i)
var sum = 0;
for(var i = 0;i<minutes;i++){
sum+=data[i].close;

}

var movingAverage = sum/minutes;
callback(movingAverage);
})
.catch(console.error)
}
}

Try

histoDay(BTC,USD,limit('none'))

It should give all available data.

1 Like

Note the functions are separated by a comma after the curly brackets:-

module.exports = {


//hourly MA
  hourlyMovingAverage:function(crytoAsset,fiat,hours,callback){
    if (hours>169) {
      console.log("Max hrs allowed 169");
      return
    }
// Get data from CryptoCompare
CryptoCompareAPI.histoHour(crytoAsset,fiat)
.then(data => {

  //Calculate MA in hrs
  data = data.reverse()
  var sum=0;
  for (var i = 0; i < hours; i++) {
    sum+=data[i].close;
  }
  var movingAverage = sum/hours;
  callback(movingAverage);
  

 })
  .catch(console.error);

},    //<<<<<note the comma to separate functions

//MA by minute
  perminuteMovingAverage:function(crytoAsset,fiat,minutes,callback) {
  if (minutes>1440) {
    console.log("Max minutes allowed 1440");
    return
  }

// Get data from CryptoCompare
CryptoCompareAPI.histoMinute(crytoAsset, fiat)
.then(data => {

//Calculate MA in hrs
data = data.reverse()
var sum=0;
for (var i = 0; i < minutes; i++) {
  sum+=data[i].close;
}
var movingAverage = sum/minutes;
callback(movingAverage);

})
.catch(console.error)

},    //<<<<<note the comma to separate functions


//MA by daily
dailyMovingAverage:function(crytoAsset,fiat,days,callback){
  if (days>30) {
    console.log("Max days allowed 30");
    return
  }

// Get data from CryptoCompare
CryptoCompareAPI.histoDay(crytoAsset,fiat)
.then(data => {

//Calculate MA in days
data = data.reverse()
var sum=0;
for (var i = 0; i < days; i++) {
  sum+=data[i].close;
}
var movingAverage = sum/days;
callback(movingAverage);

})
.catch(console.error)

}

}



Got it to work making the three moving averages was the easy part after doing the first one in the video tutorial. The hard part was putting them together with the right syntax. It was the commas that got me for a bit. Also there was a comment from @Ouzo69 about the histoDay not working. I tried and I got same error code. Changed it to hourly and didn’t have a problemScreen Shot 2020-07-23 at 10.22.18 PM

module.exports = {

  minuteMovingAverage:function(cryptoAsset,fiatCurrency,minutes,callback){

    if(minutes>1441){
      console.error("Only up to 169 hours allowed");
      return;
    }

    CryptoCompareAPI.histoMinute('BTC', 'USD')
    .then(data => {
      data = data.reverse();
      var sum = 0;
      for(var i = 0;i<minutes;i++){
        sum=+data[i].close;
      }

      var movingAverage = sum/minutes;
      callback(movingAverage);
      })
    .catch(console.error);
  },



  hourlyMovingAverage:function(cryptoAsset,fiatCurrency,hours,callback){

    if(hours>169){
      console.error("Only up to 169 hours allowed");
      return;
    }

    CryptoCompareAPI.histoHour('BTC', 'USD')
    .then(data => {
      data = data.reverse();
      var sum = 0;
      for(var i = 0;i<hours;i++){
        sum=+data[i].close;
      }

      var movingAverage = sum/hours;
      callback(movingAverage);
      })
    .catch(console.error);
    },



      dailyMovingAverage:function(cryptoAsset,fiatCurrency,days,callback){

        if(days>169){
          console.error("Only up to 169 days allowed");
          return;
        }

        CryptoCompareAPI.histoHour('BTC', 'USD')
        .then(data => {
          data = data.reverse();
          var sum = 0;
          for(var i = 0;i<days;i++){
            sum=+data[i].close;
          }

          var movingAverage = sum/days;
          callback(movingAverage);
        })
        .catch(console.error);
      }}


//index.js file

global.fetch = require("node-fetch");

const indicators = require("./indicators.js");
const exchange = require("./exchange.js");




  indicators.minuteMovingAverage("BTC","USD",100,function(result){
      console.log("Minute MA: ", result);
    });

  indicators.hourlyMovingAverage("BTC","USD",100,function(result){
    console.log("Hourly MA: ", result);
  });

  indicators.dailyMovingAverage("BTC","USD",100,function(result){
      console.log("Daily MA: ", result);
    });


I had similar issues as above, namely the 30 day limit for day to day data. The commas between the export modules was also a hold-up… Thanks to many in the for like @Boki I was able to get the daily moving average to also retrieve 100 daily data points.

Thanks :+1:

index.js --> The only thing I added was to remove the variables from each function call… by putting them at the top, as seen below, it made it easy to change.

var coinSymbol = "BTC";
var fiatMoney = "USD";
var duration = 21;

indicators.dailyMovingAverage(coinSymbol, fiatMoney, duration, function(ma, days){
  console.log("The " + coinSymbol + " " + days + " Day Moving Average is now: " + ma)
})

indicators.hourlyMovingAverage(coinSymbol, fiatMoney, duration, function(ma, hour){
  console.log("The " + coinSymbol + " " + hour + " Hour Moving Average is now: " + ma)
})

indicators.minuteMovingAverage(coinSymbol, fiatMoney, duration, function(ma, minutes){
  console.log("The " + coinSymbol + " " + minutes + " Minute Moving Average is now: " + ma)
})

indicators.js -->

    CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency, {limit:"none"}).then(data => {
    

@Boki @Ouzo69
Having problems with indicators.hourMovingAverage is not a function
Is there anyone have issue with this?

Hey Ivan & Filip,

I think I’ve completed this assignment correctly.

my indicators.js:

const CCAPIKey = "3840cb378b205f861e7299bcbe54babf1a322f55802e76584b8b7491df475d9b"
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);

module.exports = {

  dailymovingAverage:function(cryptoAsset,fiatCurrency,hours,callback){

       if(hours>31){
          console.error("Only up to 31 Days allowed!")
          return
        }

        // 1 get data from CC
        CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
        .then(data => {

          //2 calculate daily MA from 31 days or less
          data = data.reverse()
          var sum = 0;
          for(var i = 0;i<hours;i++){
            sum+=data[i].close;
          }

          var movingAverage = sum/hours;
          callback(movingAverage);

        })

        .catch(console.error)
      }
,
  hourlymovingAverage:function(cryptoAsset,fiatCurrency,hours,callback){

    if(hours>169){
      console.error("Only up to 169 hours allowed!")
      return
    }

    // 1 get data from CC
    CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
    .then(data => {

      //2 calculate hourly MA from 100 past hours
      data = data.reverse()
      var sum = 0;
      for(var i = 0;i<hours;i++){
        sum+=data[i].close;
      }

      var movingAverage = sum/hours;
      callback(movingAverage);

    })

    .catch(console.error)
  }
,
  minutelymovingAverage:function(cryptoAsset,fiatCurrency,hours,callback){

        if(hours>1441){
          console.error("Only up to 1441 hours allowed!")
          return
        }

        // 1 get data from CC
        CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
        .then(data => {

          //2 calculate minutely MA from 1441 max minutes
          data = data.reverse()
          var sum = 0;
          for(var i = 0;i<hours;i++){
            sum+=data[i].close;
          }

          var movingAverage = sum/hours;
          callback(movingAverage);

        })

        .catch(console.error)
    }
}

And my index.js

global.fetch = require("node-fetch");
const GeminiAPI = require("gemini-api").default;
const secret = "47BJYkLAbHBN2dArhguctRkigeF3";
const key = "account-55Z4i8MWhoM3GLaGNdqH";
const restClient = new GeminiAPI({key, secret, sandbox:true});
const indicators = require("./indicators.js");



indicators.dailymovingAverage("BTC","USD",31,function(result){
  console.log("Daily MA: ",result)
})
indicators.hourlymovingAverage("BTC","USD",100,function(result){
  console.log("Hourly MA: ",result)
})
indicators.minutelymovingAverage("BTC","USD",1441,function(result){
    console.log("Minutely MA: ",result)
})

I’ve checked all max length of the input values that could be given for histoDay and histoMinute. I’ve changed the comments accordingly per function. I haven’t changed the code that does the calculation part. Could anyone confirm if this code is 100%. In school i coded js too in notepad, atom is so much better. Cool to see what other people have coded. This was my first succesful JS code and it feels great!

Kr,
KryptoKoala

1 Like

So, I got a little ahead of myself trying to build my own bot.
I have daily first, and I went ahead and used the same moving averages from earlier in the course when I was messing around with Pine Editor on trading view. I still haven’t figured out how to implement the RSI indicator set to my specifications, but I will soon and any help is appreciated.

const CCAPIKey = "87e875dbe9a945f6a491f1af89c373b823f26155f1cdca8ed6dd414b37f33596"
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);

module.exports = {

  dailyMovingAverage:function (cryptoAsset,fiatCurrency,days,callback){
  if(days>31){
    console.log("Only up to 31 days allowed motherfucker!")
    return
  }

    //1 get data from CryptoCompare
      CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
      .then(data => {

      //2 calculate MA 
        data = data.reverse()
        var sum = 0;
        for(var i = 0; i<days; i++){
          sum+=data[i].close;
          }
          var dailyMovingAverage = sum/days;
          callback(dailyMovingAverage);
      })
      .catch(console.error)
    },

      hourlyMovingAverage:function (cryptoAsset,fiatCurrency,hours,callback){
      if(hours>169){
        console.log("Only up to 169 hours allowed motherfucker!")
        return
      }

        //1 get data from CryptoCompare
          CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
          .then(data => {

          //2 calculate MA 
            data = data.reverse()
            var sum = 0;
            for(var i = 0; i<hours; i++){
              sum+=data[i].close;
              }
              var hourlyMovingAverage = sum/hours;
              callback(hourlyMovingAverage);
          })
          .catch(console.error)
        },


      minuteMovingAverage:function (cryptoAsset,fiatCurrency,days,callback){
      if(minutes>1440){
        console.log("Only up to 1440 minutes allowed motherfucker!")
        return
      }

        //1 get data from CryptoCompare
          CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
          .then(data => {

          //2 calculate MA 
            data = data.reverse()
            var sum = 0;
            for(var i = 0; i<minutes; i++){
              sum+=data[i].close;
              }
              var minuteMovingAverage = sum/minutes;
              callback(minuteMovingAverage);
          })
          .catch(console.error)
        }


    }

const CCAPIKey = "3b7a0095c38aa471715e03ee0905a8547174822908de986b5b8c1104eece17c7";
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);


module.exports = {
  hourlyMovingAverage:function (cryptoAsset, fiatCurrency, hours, callback){

  if (hours>169){
    console.error("Max 169 hours!")
    return
  }
  // 1 get data from CC
  CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
  .then(data => {

  // 2 calculate MA from 100 past hours
    data = data.reverse()
    var sum = 0;
    for(var i = 0; i<hours; i++){
      sum += data[i].close;
    }
    var movingAverage = sum/hours;
    callback(movingAverage);
  })
  .catch(console.error)
},

 dailyMovingAverage:function (cryptoAsset, fiatCurrency, days, callback){

if (days>30){
  console.error("Max 30 days!")
  return
}
// 1 get data from CC
CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
.then(data => {

// 2 calculate MA from 100 past hours
  data = data.reverse()
  var sum = 0;
  for(var i = 0; i<days; i++){
    sum += data[i].close;
  }
  var movingAverage = sum/days;
  callback(movingAverage);
})
.catch(console.error)
},

minuteMovingAverage:function (cryptoAsset, fiatCurrency, minute, callback){

if (minute>1440){
 console.error("Max 1440 minutes!")
 return
}
// 1 get data from CC
CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
.then(data => {

// 2 calculate MA from 100 past hours
 data = data.reverse()
 var sum = 0;
 for(var i = 0; i<minute; i++){
   sum += data[i].close;
 }
 var movingAverage = sum/minute;
 callback(movingAverage);
})
.catch(console.error)
}

}

1 Like

Daily and Minute Moving Averages using histoDay() and histoMinute();

1 Like

my index.js file looks like this:
global.fetch = require (“node-fetch”);
const GeminiAPI = require (“gemini-api”).default;
const secret = “2GxkM3sHHFWY48EpdxVLrSzjTq8F”;
const key = “account-NSzewmv9B4b0i2kzQPza”;
const indicators = require ("./indicators.js");
/* const restClient = new GeminiAPI ({key, secret, sandbox:true});
restClient.newOrder ({amount:10, price:1100, side:“buy”, symbol:“ethusd”})
.then (response => console.log (response))
.catch (error => console.log (error)); */

indicators.minuteMovingAverage(“BTC” , “USD” , 1440 , function(result){
console.log(“Minute Moving Average is:”, result)
})
indicators.hourlyMovingAverage (“BTC”, “USD”, 100, function(result){
console.log (“Hourly Moving Average is:”, result)
})
indicators.dailyMovingAverage(“BTC”, “USD”, 31, function(result){
console.log(“Daily Moving Average is:”, result)
});
// 3 check continuously if price is crossing 100 MA => BUY/SELL/HODL

my indicators.js looks like this:
const CCAPIKEY = “9587d57e52f021966c44742c2e36c09c9b48049c3b7e966badef0edb408bdcff”
const CryptoCompareAPI = require (“cryptocompare”);
CryptoCompareAPI.setApiKey (CCAPIKEY);

module.exports = {

hourlyMovingAverage : function(cryptoAsset, fiatCurrency, hours, callback){
if(hours > 169){
console.error(“Only up to 169 hours allowed!”)
return
}
// Print MA of the last 100 hours close price going backwards from now
CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
.then(data => {
data = data.reverse()
var sum = 0;
for(var i = 0; i < hours; i++){
sum += data[i].close;
}
var movingAverage = sum/hours;
callback(movingAverage);
})
.catch(console.error)
},

dailyMovingAverage: function(cryptoAsset, fiatCurrency, days, callback){
if(days > 365){
console.error(“Only up to 365 days allowed!”)
return
}
// Print MA of the last 100 hours close price going backwards from now
CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency , {limit:“none”})
.then(data => {
data = data.reverse()
var sum = 0;
for(var i = 0; i < days; i++){
sum += data[i].close;
}
var movingAverage = sum / days;
callback(movingAverage);
})
.catch(console.error)
},

minuteMovingAverage : function(cryptoAsset , fiatCurrency , minutes , callback){
if(minutes > 1440){
console.error(“Only up to 1440 minutes allowed!”)
return
}
// Print MA of the last 100 hours close price going backwards from now
CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
.then(data => {
data = data.reverse()
var sum = 0;
for(var i = 0; i < minutes; i++){
sum += data[i].close;
}
var movingAverage = sum/minutes;
callback(movingAverage);
})
.catch(console.error)
}
}
and it works :slight_smile:

2 Likes

Here’s my module.exports in indicators.js:

module.exports = {
  dailyMovingAverage: function (cryptoAsset, fiatCurrency, days, callback){
    if(days > 365){
      console.error("Max days is 365");
    }
    CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency, {limit:"none"})
      .then(data => {
        data = data.reverse();
        var sum = 0;
        for(var i = 0; i < days; i ++){
          sum += data[i].close;
        }
        var movingAverage = sum/days;
        callback(movingAverage)
      })
      .catch(console.error);
  },
  hourlyMovingAverage: function (cryptoAsset, fiatCurrency, hours, callback){
    if(hours > 169){
      console.error("Max hours is 169");
    }
    CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
      .then(data => {
        data = data.reverse();
        var sum = 0;
        for(var i = 0; i < hours; i ++){
          sum += data[i].close;
        }
        var movingAverage = sum/hours;
        callback(movingAverage)
      })
      .catch(console.error);
  },
  minuteMovingAverage: function (cryptoAsset, fiatCurrency, minutes, callback){
    if(minutes > 1440){
      console.error("Max minutes is 1440");
    }
    CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
      .then(data => {
        data = data.reverse();
        var sum = 0;
        for(var i = 0; i < minutes; i ++){
          sum += data[i].close;
        }
        var movingAverage = sum/minutes;
        callback(movingAverage)
      })
      .catch(console.error);
  }
}

Strangely, the {limit:“none”} is needed only when calling daily data. Hourly and minute work without it.

2 Likes

Here is my module indicators

const cryptoCompareAPI=require("cryptocompare");
const CCAPIkey="e482474df69abeaddfb25de087fded403773a0b04a8ef0b5c81977df4b5ac2cc";
cryptoCompareAPI.setApiKey(CCAPIkey);

/* need module to be exporte */

module.exports={
hourlyMovingAvg:
function (cryptoAsset,fiatCurrency,hours, callback){

  if(hours > 169){
    console.log("Only upto 169 hours");
    return
  }

  cryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
  .then(data => {
    data=data.reverse();
    var sum =0;
    for(var i=0; i <hours;i++){
    sum +=data[i].close;
    }
    var movingAvg=sum/hours;
    callback(movingAvg);
  })
  .catch(console.error)
}

,
dailyMovingAvg:
function (cryptoAsset,fiatCurrency, days, callback){
if(days>30){
  console.log("Only less than 30 days")
}

  cryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)
  .then(data => {

    data=data.reverse();

    var sum =0;
    for(var i=0; i <days;i++){
    sum +=data[i].close;
    }
    var movingAvg=sum/days;
    callback(movingAvg);
  }
)
  .catch(console.error)
}
,
minutelyMovingAvg:
function (cryptoAsset,fiatCurrency,minutes, callback){

  
  cryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
  .then(data => {
    data=data.reverse();
    var sum =0;
    for(var i=0; i <minutes;i++){
    sum +=data[i].close;
    }
    var movingAvg=sum/minutes;
    callback(movingAvg);
  })
  .catch(console.error)
}
}

For some reason cryptocompare throws an error if I try a big number of days in Daily average. I thinks it is restricted to the beginning of year so i put a restriction of 30 days.

1 Like

why is the MA price, (the bottom one) say that the MA is 153k???

1 Like

i did it:

1 Like
/*Index.js*/
global.fetch = require("node-fetch");
const GeminiAPI = require("gemini-api").default;
const key = "account-k6rqU6YkieDC9OUDNwV8";
const secret = "498FX4LFq7WhoEXiFDUnHCnrzG0";
const geminiClient = new GeminiAPI ({key, secret, sandbox:true});
const indicators = require("./indicators.js");

indicators.minuteMA("BTC", "USD", 1440, function(result){
    console.log("Minute MA price is: ", result)
  })

  indicators.hourlyMA("BTC", "USD", 100, function(result){
    console.log("Hourly MA Price is: ", result)
  })

  indicators.dailyMA("BTC", "USD", 30, function(result){
    console.log("Daily MA Price is: ", result)
  });


/*Indicators.js*/

const CCAPIKEY = "b4a6ade9366e115f022d215b1d38b5c6a2c0c0d60fc32eb4b0c0762b1945032f"
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKEY);


module.exports = {
  minuteMA:function(cryptoAsset, fiatCurrency, minutes, callback){

  if(minutes>1440){
    console.error("Only up to 1440 minutes allowed!");
    return
}

CryptoCompareAPI.histoMinute(cryptoAsset,fiatCurrency)
.then(data => {
  data = data.reverse()
  var sum = 0;
  for(var i = 0; i<minutes;i++){
    sum+=data[i].close;
}

var minuteMA = sum/minutes;
callback(minuteMA);

})
.catch(console.error)


},


  hourlyMA:function(cryptoAsset, fiatCurrency, hours, callback){

  if(hours>169){
    console.error("Only up to 169 hours allowed!");
    return
}


CryptoCompareAPI.histoHour(cryptoAsset,fiatCurrency)
.then(data => {
  data = data.reverse()
  var sum = 0;
  for(var i = 0; i<hours;i++){
    sum+=data[i].close;
}

var hourlyMA = sum/hours;
callback(hourlyMA)

})
.catch(console.error);

},


  dailyMA:function(cryptoAsset, fiatCurrency, days, callback){

  if(days>365){
    console.error("Only up to 365 days allowed!")
    return
}


CryptoCompareAPI.histoDay(cryptoAsset,fiatCurrency, {limit:"none"})
.then(data => {
  data = data.reverse()
  var sum = 0;
  for(var i = 0; i<days;i++){
    sum+=data[i].close;

}

var dailyMA = sum/days;
callback(dailyMA);

})
.catch(console.error);
}
}

1 Like

Hi guys im struggling with the exercise…

error screen:::
Screen Shot 2021-03-21 at 2.35.02 PM

CODE::

index.js

Screen Shot 2021-03-21 at 2.35.31 PM

indicators.js

Screen Shot 2021-03-21 at 2.36.04 PM

appreciate your help!!

1 Like

Hey @Dexda, hope you are great.

Please provide the code properly so i can test it properly.

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

preformatted text

Carlos Z.