// Calculate the interest rate for an amortization schedule
// with a loan of 2000 dollars on January 1, 2005, and 5
// monthly payments of 500 dollars starting on Febuary 28th,
// payments made on the last day of each month.
// Get a new amortization calculation.
var c = plugins.amortization.newCalculation();
// Set the rate to -1 for unknown.
c.addRateChange(-1, new Date(2005, 0, 1));
// Set the compounding period to monthly.
c.addCompoundPeriodChange(12, new Date(2005, 0, 1));
// Add the loan and the payments to the schedule.
c.addLoan(2000, new Date(2005, 0, 1));
var lastDate = null;
var period = 12;
//valid periods are:
//PERIOD_ANNUALY : 1
//PERIOD_BI_ANNUALLY : 2
//PERIOD_DAILY : 365
//PERIOD_FOUR_MONTHLY : 3
//PERIOD_FOUR_WEEKLY : 13
//PERIOD_MONTHLY : 12
//PERIOD_QUARTERLY : 4
//PERIOD_TWO_MONTHLY : 6
//PERIOD_TWO_WEEKLY : 26
//PERIOD_WEEKLY : 52
var number_count = 5;
var startday = 31;
c.addPayment(500, new Date(2005, 1, 28), lastDate, period, number_count, startday);
// Solve for the interest rate.
c.solveForUnknown();
// Get the interest rate and the error in the calculation.
// which should be small (otherwise the calculation did
// not converge for some reason.
var r = c.getUnknown();
var e = c.getError();
// When there are no unknowns you can calculate the
// actual amortization schedule.
// Same as before, use the calculated interest rate.
var c = plugins.amortization.newCalculation();
c.addRateChange(r, new Date(2005, 0, 1));
c.addCompoundPeriodChange(12, new Date(2005, 0, 1));
c.addLoan(2000, new Date(2005, 0, 1));
c.addPayment(500, new Date(2005, 1, 28), null, 12, 5, 31);
// Calculate the actual amortization schedule.
c.calculateAmortizationSchedule();
// Get the amortization schedule (which is a JSDataSet) and
// convert it to html. This way you can put it on a label.
// As a JSDataSet you can just get the values stored in
// the rows and columns to use in your script.
var s = "<html>" + c.getAmortizationSchedule().getAsHTML();
// Get the rest balance, which is the amount left over after
// the amortization schedule. In our case (since we calculated
// the rate to have nothing left, it should amount to no more
// than 1 or 2 cents due to rounding).
var rb = c.getRestBalance(); |