Creating Daily and Minutely MA - Practical Assignment

#1

Feel free to ask questions here if you have issue with this assignment.

#2

First to blog!!! Done it and loving the whole training course!!!

#3

Hi Guys,

Everything seems to work well when executing only one Moving Averages at a time.

On the other hand, when trying to request results from the three MA’s together at the same time the terminal always gives following error on the first module.exports in the indicators file.

So when I put minutely MA first in the file:

TypeError: indicators.minutelyMovingAverage is not a function
at Object.<anonymous> (/Users/jebeljohm/Documents/Crypto/Ivan Programming/Tradingbots/index.js:14:12)

When I put hourly MA first in the file:

TypeError: indicators.hourlyMovingAverage is not a function
at Object.<anonymous> (/Users/jebeljohm/Documents/Crypto/Ivan Programming/Tradingbots/index.js:18:12)

#4

How do you get more than 31 days of historical data using the histoDay function.

I am unable to figure that out.

#5

I think it might be the fact that there has to be a comma between the functions under module.exports in the indicators.js file.

1 Like
#6

Hi Inus, correct! I did three seperate exports modules, that’s why it didn’t work.
Thanks a lot :grinning:!!

#7

Hello Ivan - loving the course!

I need some help with syntax for the limit option using histoHour - I want to change the default of 30 days to ‘none’ but can’t get it right. Everything is working correctly otherwise.

PS C:\Users\Ken\Desktop\Code> node index.js
HourlyMA: 142.80959999999993
DailyMA: 136.92
MinutleyMA: 141.9838

//with days > 31
PS C:\Users\Ken\Desktop\Code> node index.js
HourlyMA: 142.81319999999994
TypeError: Cannot read property ‘close’ of undefined
at CryptoCompareAPI.histoDay.then.data (C:\Users\Ken\Desktop\Code\indicators.js:69:18)
at process._tickCallback (internal/process/next_tick.js:68:7)
MinutleyMA: 141.984

#9

Please post your code here, thanks

#10

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

#11

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 );
#12

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

#13
  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

#14

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)
#15

image

try with:

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

1 Like
#16

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

#17

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