Solved

I need a javascript formula that calculates daily, weekly, monthly compound interest accurately

Posted on 2009-04-09
5
703 Views
Last Modified: 2012-05-06
I need a very accurate calculator formula, right now my formula works for monthly contributions with monthly compounding, but when you use weekly contributions with monthly compounding, I don't know how to adjust the formula. You can see my calculator in action at by downloading and unzipping the attached files.

So far, this is the most accurate calculator I've found on the web (I'd be happy if you could get a javascript formula to match the values generated by this calculator):
http://tiny.cc/kmhGP

Here is the problem with weekly contributions and monthly compounding:
1. there are 52 weeks in a year, but some months have 5 weeks while others have 4, however I don't think their calc takes this into account, I think they simply base their calculation on 52 weeks and every 4 weeks they compound the interest.

Anyway, here is the formula I'm using:

var P = startingAmount;
    var Y = yearsOfInvesting;
    var c = getAdditionalContributionsPerPeriod;
    var t = getTermsPerPeriod;
    var n = t * Y;
    var r = interestRate / 100 / t; // interestRate [%]
    var z = 1 + r;
 total = P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;

For a scenario:
1. $1000 principal
2. $50 per week contribution
3. 10 years
4. monthly compounding
The total according to: http://tiny.cc/kmhGP should be $30,007 rounding the decimals up.

The closest I've come to this is by this formula is by weekly contributions & weekly compounding (but I want to change it to monthly compounding):
<script>
var P = 1000;//startingAmount;
    var Y = 0;//yearsOfInvesting;
    var c = 50;//
    var n = 520;//t * Y;
    var r = .02/52;
    var z = 1 + r;

mz=  P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;
document.write(mz);
</script>
How can I make my formula work for weekly and daily contributions?
 
// Savings calculator formulas
 

// Parameters:

//startingAmount

//years

//additionalContributions

//interestRate

//aditionalContributionsPeriod: 1 - per week; 2 - bi-weekly; 3 - per month; 4 - per quarter; 5 - per year

//interestRatePeriod, Compound: 1 - Daily; 2 - monthly; 3 - quarterly; 4 - annually
 
 

function getDataForChart(startingAmount, yearsOfInvesting, additionalContributions, interestRate, 

    aditionalContributionsPeriod, interestRatePeriod) {

   // Map to one - letter variables

   //var P = startingAmount;

   var Y = yearsOfInvesting;

  // var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting, 

      // additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

  // var t = getTermsPerPeriod(startingAmount, yearsOfInvesting, 

      // additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

  // var n = t * Y;

  // var r = interestRate / 100 / t; // interestRate [%]

  // var z = 1 + r;

   

   var data1 = new Array();

   var data2 = new Array();

   for (var i = 1; i <= Y; i++) {

       // Base amount:

       var B = calculatePrincipal(startingAmount, i, additionalContributions, interestRate, 

                 aditionalContributionsPeriod, interestRatePeriod);

       data1[i - 1] = [i, B];

       // Compound contributions:

       var Cc = calculateTotal(startingAmount, i, additionalContributions, interestRate, 

                aditionalContributionsPeriod, interestRatePeriod);

       // var Cc = P * Math.pow(z, i) + c * (Math.pow(z, i + 1) - z) / r;

       data2[i - 1] = [i, Cc];

   } 

   

   var series = new Array();

   series[0] = data1;

   series[1] = data2;

   return series;

}
 

function calculatePrincipal(startingAmount, yearsOfInvesting, additionalContributions, interestRate, 

    aditionalContributionsPeriod, interestRatePeriod) {

   

    var P = startingAmount;

    var Y = yearsOfInvesting;

    var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting, 

       additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

    var t = getTermsPerPeriod(startingAmount, yearsOfInvesting, 

       additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

    var n = t * Y;

    var r = interestRate / 100 / t; // interestRate [%]

    var z = 1 + r;

    var coeff = 1;

    if (aditionalContributionsPeriod == 1) {

	coeff = 52;

    }

    else if (aditionalContributionsPeriod == 2) {

	coeff = 26;

    }

    else if (aditionalContributionsPeriod == 3) {

	coeff = 12;

    }

    else if (aditionalContributionsPeriod == 4) {

	coeff = 4;

    }

    return startingAmount + coeff * additionalContributions * yearsOfInvesting;

}
 

function calculateTotal(startingAmount, yearsOfInvesting, additionalContributions, interestRate, 

    aditionalContributionsPeriod, interestRatePeriod) {

    

    var P = startingAmount;

    var Y = yearsOfInvesting;

    var c = getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting, 

       additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

    var t = getTermsPerPeriod(startingAmount, yearsOfInvesting, 

       additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod);

    var n = t * Y;

    var r = interestRate / 100 / t; // interestRate [%]

    var z = 1 + r;

    return P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;

}
 

function getTermsPerPeriod(startingAmount, yearsOfInvesting, additionalContributions, interestRate, 

      aditionalContributionsPeriod, interestRatePeriod) {	

  

      if (interestRatePeriod == 1) {

	  return 365;

      }

      else if (interestRatePeriod == 2) {

	  return 12;

      }

      else if (interestRatePeriod == 3) {

	  return 4;

      }

      else {

	  return 1;

      }

      return 1;

}
 

function getAdditionalContributionsPerPeriod(startingAmount, yearsOfInvesting, 

    additionalContributions, interestRate, aditionalContributionsPeriod, interestRatePeriod) {

  

  var divideFactor = 1;

  if (interestRatePeriod == 1) {

      // Additional contributions per day

      if (aditionalContributionsPeriod == 1) {

	  // Week => factor = 7

          divideFactor = 7;

      }

      else if (aditionalContributionsPeriod == 2) {

	  // Biweekly => factor = 14

          divideFactor = 7;

      }

      else if (aditionalContributionsPeriod == 3) {

	  // Monthly => factor = 30

          divideFactor = 30;

      }

      else if (aditionalContributionsPeriod == 4) {

	  // Per quarter => factor = 91

          divideFactor = 91;

      }

      else if (aditionalContributionsPeriod == 5) {

	  // Per year => factor = 365

          divideFactor = 365;

      }

  } 

  else if (interestRatePeriod == 2) {

      // Additional contributions per month

      if (aditionalContributionsPeriod == 1) {

	  // Week

          divideFactor = 1 / 4;

      }

      else if (aditionalContributionsPeriod == 2) {

	  // Biweekly

          divideFactor = 1 / 2;

      }

      else if (aditionalContributionsPeriod == 3) {

	  // Monthly

          divideFactor = 1;

      }

      else if (aditionalContributionsPeriod == 4) {

	  // Per quarter

          divideFactor = 4;

      }

      else if (aditionalContributionsPeriod == 5) {

	  // Per year

          divideFactor = 12;

      }

  }   

  else if (interestRatePeriod == 3) {

      // Additional contributions per quarter

      if (aditionalContributionsPeriod == 1) {

	  // Week

          divideFactor = 1 / 13;

      }

      else if (aditionalContributionsPeriod == 2) {

	  // Biweekly

          divideFactor = 1 / 6.5;

      }

      else if (aditionalContributionsPeriod == 3) {

	  // Monthly

          divideFactor = 1 / 4;

      }

      else if (aditionalContributionsPeriod == 4) {

	  // Per quarter

          divideFactor = 1;

      }

      else if (aditionalContributionsPeriod == 5) {

	  // Per year

          divideFactor = 4;

      }

  }   

  else if (interestRatePeriod == 4) {

      // Additional contributions per year

      if (aditionalContributionsPeriod == 1) {

	  // Week

          divideFactor = 1 / 52;

      }

      else if (aditionalContributionsPeriod == 2) {

	  // Biweekly

          divideFactor = 1 / 26;

      }

      else if (aditionalContributionsPeriod == 3) {

	  // Monthly

          divideFactor = 1 / 12;

      }

      else if (aditionalContributionsPeriod == 4) {

	  // Per quarter 

          divideFactor = 1 / 4;

      }

      else if (aditionalContributionsPeriod == 5) {

	  // Per year

          divideFactor = 1;

      }

  }   

  return additionalContributions / divideFactor;

}

Open in new window

0
Comment
Question by:selpmet
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
mudbuggle earned 500 total points
ID: 24114910
To determine whether or not the month has 4 weeks or 5 weeks we need to determine the last day of the month. Here is an article on how to do that.
http://javascript.about.com/library/bllday.htm

Then you'll want to divide the last day of month by 7, then round up. Then multiply your total per week by the resulting number of weeks. If you want to calculate only on weeks that have weekendings in that month then you'll want to insert an if statement to verify that the last day of the month is on the last day of the week. I have an exampe below.
var dim = days in month;

var eomform = month + '/' + dim + '/'+ year

var eomdate = new Date(eomform);

var eomeow = eomdate.getDay();

if(eommeow == 6)

{

   var multiplier = whater var you set the calculated # of weeks to;

}

else

{

   var multiplier = whater var you set the calculated # of weeks to -1;

}

Open in new window

0
 

Author Comment

by:selpmet
ID: 24116246
I don't understand the var multiplier - for instance 10 years would be 520 weeks and you're saying to subtract 1 from it?

Also, can you help me figure out the formula for calculating daily/monthly/quarterly/annually compound interest for weekly/daily/bi-weekly/monthly/annually made contributions? There has to be a financial math javascript library that already can do this like excel has.
0
 
LVL 6

Expert Comment

by:mudbuggle
ID: 24116522
the multiplier is for on a monthly basis it will calculate per month... as I wrote it it would work for 1 month. Turns out I misread what you were asking, that was my fault. I can come up with something it will take a little though.
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 37169587
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now