-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinancecalculator.html
269 lines (239 loc) · 7.02 KB
/
financecalculator.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<HTML>
<meta name="viewport" content="width=device-width, initial-scale=1">
<HEAD>
<script>
function cal_rate() {
n = parseFloat(document.forms[0].number.value);
pmt = parseFloat(document.forms[0].pmt.value);
pv = parseFloat(document.forms[0].pv.value);
fv = parseFloat(document.forms[0].fv.value);
ioy = parseInt(document.forms[0].ioy.value);
var iteration_max = 1000;
var iteration = 0;
var adjustment = 0.5;
var solver = 0;
var sum_all = 0;
var findTheAnswer = false;
var inverse = false;
if(pv > 0 && fv < 0) {
inverse = true;
} else if(pv > 0 && fv > 0) {
alert('Either PV or FV should be negative');
return;
} else if(pv < 0 && fv < 0) {
alert('Either PV or FV should be positive');
return;
}
while(iteration < iteration_max) {
sum_all = 0;
rate = solver + 1;
for(i=1;i<=n;i++) {
sum_all = sum_all + pmt / Math.pow(rate,i);
}
sum_all = sum_all + pv;
sum_all = sum_all + fv/Math.pow(rate,n);
//console.log(sum_all);
if(Math.abs(sum_all) < 0.000001) {
//I got it!
findTheAnswer = true;
break;
} else if(sum_all > 0) {
//discountmore.
if(inverse == false) solver = solver + adjustment;
else solver = solver - adjustment;
//To-do: I need to take into account reverse case.
} else {
//subtract discount.
if(inverse == false) solver = solver - adjustment;
else solver = solver + adjustment;
}
adjustment = adjustment / 1.5;
iteration = iteration + 1;
}
if(findTheAnswer == true) {
rate = rate -1;
rate = rate * 100000;
rate = Math.round(rate);
rate = rate / 100000;
rate = rate * ioy;
document.forms[0].rate.value = rate;
} else {
alert('Unable to find YTM!');
}
}
function cal_fv() {
n = parseFloat(document.forms[0].number.value);
pmt = parseFloat(document.forms[0].pmt.value);
pv = parseFloat(document.forms[0].pv.value);
rate = parseFloat(document.forms[0].rate.value);
ioy = parseInt(document.forms[0].ioy.value);
rate = rate / ioy;
rate = rate + 1;
sum_all = 0;
sum_all = sum_all + pv * Math.pow(rate,n);
for(i=1;i<=n;i++) {
sum_all = sum_all + pmt * Math.pow(rate,i);
}
document.forms[0].fv.value = sum_all;
}
function cal_pmt() {
//Annuity
n = parseFloat(document.forms[0].number.value);
pv = parseFloat(document.forms[0].pv.value);
rate = parseFloat(document.forms[0].rate.value);
fv = parseFloat(document.forms[0].fv.value);
ioy = parseInt(document.forms[0].ioy.value);
rate = rate / ioy;
rate = rate + 1;
total = pv + fv;
total = total / ((1 - Math.pow(rate, -n))/(rate-1))
direction = pv / pv;
total = - direction * total;
document.forms[0].pmt.value = total;
}
function cal_pv() {
n = parseFloat(document.forms[0].number.value);
pv = parseFloat(document.forms[0].pv.value);
rate = parseFloat(document.forms[0].rate.value);
fv = parseFloat(document.forms[0].fv.value);
pmt = parseFloat(document.forms[0].pmt.value);
ioy = parseInt(document.forms[0].ioy.value);
rate = rate / ioy;
sum_all = 0;
rate = rate + 1;
for(i=1;i<=n;i++) {
sum_all = sum_all + (pmt / Math.pow(rate,i));
}
sum_all = sum_all + (fv / Math.pow(rate,n));
sum_all = -sum_all;
document.forms[0].pv.value = sum_all;
}
function showhelp(obj) {
if(obj.value == 'Show Help') {
document.getElementById("help_table").style.display = "";
obj.value = 'Hide Help';
} else {
document.getElementById("help_table").style.display = "none";
obj.value = 'Show Help';
}
}
</script>
<STYLE>
.inputbox {
width: 100px;
border: 1px solid #CCC;
height: 30px;
font-size: 12pt;
}
</STYLE>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-57854391-1', 'auto');
ga('send', 'pageview');
</script></head>
<BODY>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- responsive -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-7620137999744720"
data-ad-slot="5778750897"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<BR>
<BR>
<Script src="menu.js"></Script>
<BR>
<Table width="300"><TR><TD>
<Center>Finance Calculator</Center><BR>
<Center>(Mobile Supported)</Center><BR>
</TD></TR></Table>
<form name="calc">
<Table border="0" cellpadding="1" cellspacing="2" bgcolor="black" width="300">
<TR>
<TD bgcolor="#FFFFFF">N(Period)</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="number" value="10" class="inputbox"></TD>
</TR>
<TR>
<TD bgcolor="#FFFFFF">PMT</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="pmt" value="50" class="inputbox"><Input type="button" value="calculate PMT" onclick="cal_pmt()"></TD>
</TR>
<TR>
<TD bgcolor="#FFFFFF">PV</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="pv" value="-950" class="inputbox"><Input type="button" value="calculate PV" onclick="cal_pv()"></TD>
</TR>
<TR>
<TD bgcolor="#FFFFFF">FV</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="fv" value="1000" class="inputbox">
<Input type="button" value="calculate FV" onclick="cal_fv()"></TD>
</TR>
<TR>
<TD bgcolor="#FFFFFF">RATE</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="rate" value="0.05" class="inputbox"><Input type="button" value="calculate YTM" onclick="cal_rate()"></TD>
</TR>
<TR>
<TD bgcolor="#FFFFFF">IoY</TD>
<TD bgcolor="#FFFFFF"><input type="text" name="ioy" value="2" class="inputbox"></TD>
</TR>
</TABLE>
</form><BR>
<BR>
<input type="button" name="help" value="Show Help" onclick="showhelp(this);">
<TABLE width="300" id="help_table" style="display:none;">
<TR>
<TD>
How to use it and example.<BR>
<BR>
<B>1. Getting YTM</B><BR>
<BR>
The bond's face value is $100<BR>
The bond is sold at $95<BR>
The coupon rate is 5%, semiannual. for 10 yrs.<BR>
Get the YTM on this bond.<BR>
<BR>
IoY = 2 (Semiannual) <BR>
FV = 100<BR>
PV = -95 (Should be negative)<BR>
PMT = 2.5<BR>
N = 20 (Semiannual * 10 yrs)<BR>
Click "calculate YTM" button<BR>
<BR>
<B>2. Getting Mortgage Value</B><BR>
<BR>
Supposed that the house value is $1000<BR>
You need to pay the mortgage monthly<BR>
The APR rate is 12%, compounding monthly<BR>
It's 10 years contract<BR>
Get the monthly payment on this mortgage contract<BR>
<BR>
IoY = 12 (Monthly) <BR>
FV = 0<BR>
PV = 1000<BR>
N = 120 (monthly * 10 yrs)<BR>
RATE = 0.12<BR>
<BR>
Click "calculate pmt" button<BR>
<BR>
<B>3. Getting Present Value of the bond</B><BR>
<BR>
The bond's face value is $100<BR>
The coupon rate is 5%, semi-annual, for 10 yrs.<BR>
The discount rate is 7%.<BR>
Get the value on this bond<BR>
<BR>
IoY = 2 (Semiannual) <BR>
FV = 100<BR>
PMT = 2.5<BR>
N = 20 (Semiannual * 10 yrs)<BR>
RATE=0.07<BR>
Click "calculate PV" button<BR>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>