Convert Stock Prices Into Returns in JavaScript

The following Javascript code is for converting an array of stock price into an array of returns. The length of the array of returns will have a length that is 1 unit smaller than the one of prices. This is because we need 2 prices for a return.

The code is following:

function getReturn(array){
     var returnarray = [];
     var l = array.length;
     for(let i = 0; i < l-1; i++){
     	returnarray.push(array[i+1]/array[i]-1)
} return returnarray; };

Type in the textarea of some numbers seperated by commas.


  • Prices: 100,101,102
  • Return:

We can also get the mean return and return standard deviation, too.

 function getReturnObj(array){
     var objecttoreturn = {};
     var returnarray = [];
     var l = array.length;
     for(let i = 0; i < l-1; i++){
     	returnarray.push(array[i+1]/array[i]-1)
     };

     var mean = returnarray.reduce((a,b,c,d)=>{
        if(c==d.length-1){
		return (a+b)/d.length
	}
	return a+b
     });
	
     var std = returnarray.reduce((a,b,c,d)=>{
	var l = d.length;
        if(c== l-1){	
		var mean = (a+b)/d.length;
		var variance = 0;
		for(let i = 0; i < l; i++){
			variance += Math.pow(d[i]-mean,2);
		}
		return Math.sqrt(variance/(l-1));
	}
	return a+b
	})

     objecttoreturn.returns = returnarray;
     objecttoreturn.mean = mean;
     objecttoreturn.std = std;
     return objecttoreturn;
 };

Type in the textarea of some numbers seperated by commas.


  • Prices: 100,101,102
  • Return:
  • Mean Returns:
  • Return Standard Deviation:

Lastly the following code creats an the object with the mean and the standard deviation of stock prices.

function getReturnObjWithPrices(array){
     var objecttoreturn = {};
     var returnarray = [];
     var l = array.length;
     for(let i = 0; i < l-1; i++){
     	returnarray.push(array[i+1]/array[i]-1)
     };

     var mean = returnarray.reduce((a,b,c,d)=>{
        if(c==d.length-1){
		return (a+b)/d.length
	}
	return a+b
     });
	
     var std = returnarray.reduce((a,b,c,d)=>{
	var l = d.length;
        if(c== l-1){	
		var mean = (a+b)/d.length;
		var variance = 0;
		for(let i = 0; i < l; i++){
			variance += Math.pow(d[i]-mean,2);
		}
		return Math.sqrt(variance/(l-1));
	}
	return a+b
	});

    var priceMean = array.reduce((a,b,c,d)=>{
        if(c==d.length-1){
		return (a+b)/d.length
	}
	return a+b
     });

     var priceStd = array.reduce((a,b,c,d)=>{
	var l = d.length;
        if(c== l-1){	
		var mean = (a+b)/d.length;
		var variance = 0;
		for(let i = 0; i < l; i++){
			variance += Math.pow(d[i]-mean,2);
		}
		return Math.sqrt(variance/(l-1));
	}
	return a+b
	});

     objecttoreturn.returns = returnarray;
     objecttoreturn.mean = mean;
     objecttoreturn.std = std;
     objecttoreturn.priceMean = priceMean;	
     objecttoreturn.priceStd = priceStd;

     return objecttoreturn;
 };

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser