forked from theupscale/financeFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirr.js
More file actions
73 lines (50 loc) · 1.34 KB
/
irr.js
File metadata and controls
73 lines (50 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function IRR(loan,tenor,advanceEmi,flatRate){
//set all the values 0 first
//loan = 0;
//tenor = 0;
//emi = 0;
//advanceEmi = 0;
//flatRate = 0;
//finding the interest first on the loan which has to be flat
interest = loan * flatRate / 100 / 12 * tenor;
//debug(interest);
//finding the monthly emi
emi = (interest + loan) / tenor ;
//debug(emi)
// intialising few loop variables
I = 0;
npv = null;
// Deriving Outstanding Loan Amount
loan_outstanding = loan - (advanceEmi * emi);
//debug(loan_outstanding);
//Deriving Tenor
tenor = tenor - advanceEmi;
//debug(tenor);
//Documentation on IRR
/*
/*
The below codes do the following
first it needs to give the guessing range where it has to guess the IRR and then it has to loop through every period given
*/
loan = loan -(advanceEmi * emi);
for (irr = 1; irr < 100; irr = irr + 0.01){
npv = 0.00;
for (I = 1; I <= tenor; I++){
//npv = npv + (emi /Math.pow(1+ irr/12,I));
npv = npv + (emi/Math.pow((1+(irr/100/12)),I))
// debug(irr);
// debug(npv);
// debug(emi);
// debug(I);
}
if (npv <= loan){
found = true;
//debug(found);
//debug(loan);
//debug(npv);
//break;
return irr;
}
//debug("this is " + irr);
}
}