Using Javascript to Calculate Actual Rate of Return

When looking at deposits through the year, most companies do no accurately report earnings for individuals. One thing that I want to change is requiring institutions to be more transparent about individual profit. In other words, who cares how the markets are doing, what the fees are. Let’s focus on profit (after fees and other charges).

If we are not satisfied with profit, lets dig in deeper to see what the income or expenses are.

Here is code to calculate growth based on deposits:


<html>
<body>
<pre>
<script>
function logWrite(arg) {
  document.body.innerHTML = document.body.innerHTML + arg + '\n<br>';
}
function logClear() {
  document.body.innerHTML = '';
}

function parseDate(dateString) {
  var mdy = dateString.split('/')
  return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function yearsInBetweenDates(date1, date2) {
  return (date2-date1)/(1000*60*60*24*365);
}

function yearsInBetweenAssign(paymentArray, currentNetWorth) {
  for (var i = 0; i<paymentArray.length; i++) {
    paymentArray[i].years = yearsInBetweenDates(paymentArray[i].date, currentNetWorth.date);
  }
}

function findTotalPayments(paymentArray) {
  var total = 0;
  for (var i = 0; i<paymentArray.length; i++) {
    total = total + paymentArray[i].amount;
  }
  return total;
}

function findRateOfReturn(paymentArray, currentNetWorth, lowRate, highRate){
  yearsInBetweenAssign(paymentArray, currentNetWorth);
  while(highRate-lowRate > .0001) {
    var testRate = (lowRate + highRate)/2;
    var calculatedNetWorth = calculateNetWorth(paymentArray, testRate);
    if(calculatedNetWorth < currentNetWorth.amount) {
      lowRate = testRate;
    } else {
      highRate = testRate;
    }
  }
  return lowRate;
}

function calculateNetWorth(paymentArray, interestRate) {
  var total = 0;
  for (var i=0; i<paymentArray.length; i++) {
    var P = paymentArray[i].amount;
    var r = interestRate;
    var y = paymentArray[i].years;
    total = total + P*Math.pow((1+r), y);
  }
  return total;
}

function createTransaction(date, amount) {
  var object = new Object();
  object.date = parseDate(date);
  object.amount = amount;
  return object;
}

var currentNetWorth = createTransaction("10/25/2014", 111197.30);
var paymentArray = [
createTransaction("06/21/2013", 25000.00),
createTransaction("07/26/2013", 15101.06),
createTransaction("10/30/2013", 42084.34),
createTransaction("11/12/2013", 8541.71),
createTransaction("12/13/2013", 11000.00),
createTransaction("03/21/2014", 995.00),
createTransaction("04/21/2014", 995.00),
createTransaction("05/21/2014", 995.00),
createTransaction("06/20/2014", 995.00),
createTransaction("07/22/2014", 995.00),
createTransaction("08/19/2014", 995.00),
createTransaction("09/19/2014", 995.00),
createTransaction("10/21/2014", 995.00)
];
for (var i = 0; i<paymentArray.length; i++) {
  logWrite('DepositDate:'+paymentArray[i].date+' DespositAmount:'+paymentArray[i].amount);
}
var totalDeposits = findTotalPayments(paymentArray);
logWrite('Deposits:'+totalDeposits);
logWrite('Current:'+currentNetWorth.amount);
logWrite('Rate:'+findRateOfReturn(paymentArray, currentNetWorth, -1, 1));
logWrite('Growth:'+(currentNetWorth.amount-totalDeposits));

</script>
</pre>
</body>
</html>

Output:
DepositDate:Fri Jun 21 2013 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:25000
DepositDate:Fri Jul 26 2013 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:15101.06
DepositDate:Wed Oct 30 2013 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:42084.34
DepositDate:Tue Nov 12 2013 00:00:00 GMT-0800 (Pacific Standard Time) DespositAmount:8541.71
DepositDate:Fri Dec 13 2013 00:00:00 GMT-0800 (Pacific Standard Time) DespositAmount:11000
DepositDate:Fri Mar 21 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Mon Apr 21 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Wed May 21 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Fri Jun 20 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Tue Jul 22 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Tue Aug 19 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Fri Sep 19 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
DepositDate:Tue Oct 21 2014 00:00:00 GMT-0700 (Pacific Daylight Time) DespositAmount:995
Deposits:109687.10999999999
Current:111197.3
Rate:0.01318359375
Growth:1510.1900000000169

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s