Creating Daily and Minutely MA - Practical Assignment

I think that I have it working right. Now dailyMovingAverage is available for up to 365 days.

PS C:\Users\Ken\Desktop\Code> node index.js
HourlyMA: 142.83179999999996
MinutleyMA: 142.0304
DailyMA: 136.26

from node.js
indicators.minutelyMovingAverage(“ETH”,“USD”,50,function(result){
console.log("MinutleyMA: ", result)}),
indicators.hourlyMovingAverage(“ETH”,“USD”,50,function(result){
console.log("HourlyMA: ", result)}),
indicators.dailyMovingAverage(“ETH”,“USD”,50,function(result){
console.log("DailyMA: ", result)});

from indicators.js:
const CCAPIKey = “b5c65d6a8e240b7faec31b7243234c445eda6fa6a1c65382520c0ccdc7c95f5e”
const CryptoCompareAPI = require(“cryptocompare”);
CryptoCompareAPI.setApiKey(CCAPIKey);

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

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

// 1 get data from CryptoCompare
CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)
.then(data => {
  // 2 calculate MA from past minutes
  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

}

// 1 get data from CryptoCompare
CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
.then(data => {
// 2 calculate MA from 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>365){
  console.error("Only up to 365 days allowed!")
  return

}

// 1 get data from CryptoCompare
CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency,{‘limit’:‘none’})
.then(data => {
// 2 calculate MA from past 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)
}
}

1 Like

My solution :

// indicators.js

const Logger = require("./logger.js");
const CryptoCompareApiKey = "12b23e55426ae7d2aa024ce96a131e2026e9ae3ef2131ba60474e2bd89eba46d";
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CryptoCompareApiKey);

module.exports = 
{
	DailyMovingAverageAsync: 
		async function DailyMovingAverageAsync(
			cryptoAsset,
			currency,
			hours,
			currentElementIndex)
		{
			const priceHistory =
			 	await CryptoCompareAPI.histoDay(
			 			cryptoAsset, 
			 			currency
			 	   ,{
			 			limit : hours + currentElementIndex + 1
			 		}
			 	);

			const priceHistoryToPast = priceHistory.reverse();

			const nowMovingAverage =
				CalculateMovingAverageByClosingPriceForItem(
					priceHistoryToPast,
					/*currentElementIndex:*/ currentElementIndex,
					/*windowSize:*/ hours
				);

			return nowMovingAverage;
		}



	, HourlyMovingAverageAsync: 
		async function HourlyMovingAverageAsync(
			cryptoAsset,
			currency,
			hours,
			currentElementIndex)
		{
			const priceHistory =
			 	await CryptoCompareAPI.histoHour(
			 			cryptoAsset, 
			 			currency
			 	   ,{
			 			limit : hours + currentElementIndex + 1
			 		}
			 	);

			const priceHistoryToPast = priceHistory.reverse();

			const nowMovingAverage =
				CalculateMovingAverageByClosingPriceForItem(
					priceHistoryToPast,
					/*currentElementIndex:*/ currentElementIndex,
					/*windowSize:*/ hours
				);

			return nowMovingAverage;
		}


	, MinutelyMovingAverageAsync: 
		async function MinutelyMovingAverageAsync(
			cryptoAsset,
			currency,
			hours,
			currentElementIndex)
		{
			const priceHistory =
			 	await CryptoCompareAPI.histoMinute(
			 			cryptoAsset, 
			 			currency
			 	   ,{
			 			limit : hours + currentElementIndex + 1
			 		}
			 	);

			const priceHistoryToPast = priceHistory.reverse();

			const nowMovingAverage =
				CalculateMovingAverageByClosingPriceForItem(
					priceHistoryToPast,
					/*currentElementIndex:*/ currentElementIndex,
					/*windowSize:*/ hours
				);

			return nowMovingAverage;
		}

}


function JsonBeautifyDefault(json)
{
	const result = 
		JsonBeautify(
			/*value:*/ json,
			/*replacer:*/ null,
			/*space:*/ 2,
			/*limit:*/ 80
		);

	return result;
}




function CalculateMovingAverageByClosingPriceForItem(
	priceHistoryToPast, 
	currentElementIndex, 
	windowSize)
{
	var result = 0;
	var accumulatedSum = 0;

	for (i = 0; i < windowSize; i++)
	{
		accumulatedSum += priceHistoryToPast[currentElementIndex + i].close;
	}

	result = accumulatedSum / windowSize;

	return result;
}


// usage

const DEFAULT_WINDOW_SIZE = 100;


async function MovingAverageIndicatorsLogicAsync()
{
	var nowMovingAverageDay =
	await Indicators.DailyMovingAverageAsync(
		/*cryptoAsset:*/ "BTC",
		/*currency:*/ "USD",
		/*hours:*/ DEFAULT_WINDOW_SIZE,
		/*currentElementIndex:*/ 0
	);

	console.log('daily 100 nowMovingAverage : ' + nowMovingAverageDay);

	var nowMovingAverageHour =
	await Indicators.HourlyMovingAverageAsync(
		/*cryptoAsset:*/ "BTC",
		/*currency:*/ "USD",
		/*hours:*/ DEFAULT_WINDOW_SIZE,
		/*currentElementIndex:*/ 0
	);

	console.log('hourly 100 nowMovingAverage : ' + nowMovingAverageHour);

	var nowMovingAverageMinute =
	await Indicators.MinutelyMovingAverageAsync(
		/*cryptoAsset:*/ "BTC",
		/*currency:*/ "USD",
		/*hours:*/ DEFAULT_WINDOW_SIZE,
		/*currentElementIndex:*/ 0
	);

	console.log('minutely 100 nowMovingAverage : ' + nowMovingAverageMinute);

}


main( MovingAverageIndicatorsLogicAsync );

Ok this is doing my life in…

@ivan i tried doing my own code for it copying what we learnt in the hourly MA… Which is:

const CCAPIKey = "7cf6ac539f5b54f35c90798aee4b4957800026fac6de2eb1971d6879b8d12cc9";
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
  }



  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




  CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency)

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

  }

}
}

I run that and i get PS C:\Users\Russ1\Desktop\Coding Stuffs\Trading bot> node index.js
Hourly Moving Average is: 5777.275600000001

It wont show the daily, its like its not getting the data??

I tried using @kjames code for the daily and i get the error:

TypeError: Cannot read property ‘close’ of undefined
at CryptoCompareAPI.histoDay.then.data (C:\Users\Russ1\Desktop\Coding Stuffs\Trading bot\indicators.js:77:14)
at process._tickCallback (internal/process/next_tick.js:68:7)
Hourly Moving Average is: 5777.2773

  indicators.hourlyMovingAverage("BTC","USD",100,function(result){
    console.log("Hourly Moving Average is: ", result)
  }),

  indicators.dailyMovingAverage("BTC","USD",365,function(result){
    console.log("Daily Moving Average is: ", result)
  });

index.js file indicator calls look like this

Ok so i worked out the difference in code, i had a } in the wrong spot.

However now i get the :

TypeError: Cannot read property 'close' of undefined
   at CryptoCompareAPI.histoDay.then.data (C:\Users\Russ1\Desktop\Coding Stuffs\Trading bot\indicators.js:43:21)
   at process._tickCallback (internal/process/next_tick.js:68:7)

try with:

CryptoCompareAPI.histoDay(cryptoAsset, fiatCurrency, {limit:“none”})

1 Like

I noticed that in the comments above and finally got it to work haha :hugs::hugs:

Does anyone know how to do a setup to go 100% each trade?

return restClient.newOrder({amount:0.5,
price:50000,
side:“buy”,
symbol:“btcusd”,
options:[“immediate-or-cancel”]})

Now each buy trade goes 0.5 BTC but I want to do it 100%…

@ivan

My results:
PS C:\Users\power\Desktop\Ivan Course Programming\Gemini Trading Bot> node index.js
Hourly MA: 9307.641
Minute MA: 9357.5772
Daily MA: 9593.805300000002

Here are from index.js:

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

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

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


And from indicators.js:

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

}

Last exercise. … My try is to check more shorter time periods and as Ivan said in the end - find a way to buy more when the price starts to go up. At the moment, since I could not advance for technical reasons, I am reading other peoples solutions and trying to learn form them.

Let’s build daily and minutely indicators!

In principle this task was not too difficult since you only had to change certain expressions. I will explain how to get from the hourlyMovingAverage to the minutelyMovingAverage function:

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

to

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

(hourlyMovingAverage to minutelyMovingAverage and hours to minutes)

2.)
CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)

to

CryptoCompareAPI.histoMinute(cryptoAsset, fiatCurrency)

(histoHour to histoMinute)

3.)
if (hours>169) {
console.error(“Only up to 169 hours allowed!”);
return

to

if (minutes>1441) {
console.error(“Only up to 1441 minutes allowed!”);
return

(hours to minutes and 169 to 1441)

4.)

for (var i=0; i<hours; i++){

to

for (var i=0; i<minutes; i++){

(hours to minutes)

5.)
var movingAverage = sum / hours;

to

var movingAverage = sum / minutes;

(hours to minutes)

Another thing that should not be forgotten is the comma behind the first and the second function after the last curly bracket of these functions. Since the third function is the last one at the moment there must not be a comma after the last curly bracket of this function.

Finding out about the index boundaries

I used the following temporary code in each function before the callback to find out the index boundaries:

console.log(data.length);

This gave me the following results:

169
daily MA:  7112.343250000001
169
hourly MA:  7118.710700000001
1441
minutely MA:  6843.309819819822

This method is more reliable than to look for this information in the API documentation, because for the API call histoDay a default value of 30 is documented, which is not updated to the current actual value of 169 yet. Increasing the index just by one each function will run into an error!

In the following the full code of the module indicators.js is shown:

const CryptoCompareAPI = require("cryptocompare");
const CCAPIKey="6f48b0b8ec36e174dff6a4cc1550ab55afc12208c50784300d515d6e5f2dc7d5"
CryptoCompareAPI.SetApiKey = CCAPIKey;

module.exports = {

  minutelyMovingAverage:function(cryptoAsset, fiatCurrency, minutes, callback){
    if (minutes>1441) {
      console.error("Only up to 1441 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 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(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>169) {
      console.error("Only up to 169 days allowed!");
      return
    }
    CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
    .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)
  }

}

Please note that the code will not work with the API provided above since I have deleted this API key. Please create and use your own Cryptocompare API Key.

I used the following calls to test the three functions:

// 100 minutes Moving Average
indicators.minutelyMovingAverage("BTC", "USD", 100, function(result){
  console.log("minutely MA: ", result);
});

// 100 hour Moving Average
indicators.hourlyMovingAverage("BTC", "USD", 100, function(result){
  console.log("hourly MA: ", result);
});

// 100 day Moving Average
indicators.dailyMovingAverage("BTC", "USD", 100, function(result){
  console.log("daily MA: ", result);
});

This resulted in the following reply:

hourly MA:  7117.136800000002
daily MA:  7117.206200000002
minutely MA:  6862.9628999999985

The construction of the function dailyMovingAverage works in an analogous way. Please always try to do the assignments on your own. Running into problems and figuring out why it is not working at the moment is a very important task learning and getting used to a new programming language.

5 Likes

As I earlier said I have to go through this course again, have to learn Java programming. Reading the other peoples solutions and trying to learn from them.

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?