-
Notifications
You must be signed in to change notification settings - Fork 0
/
cashflow.js
359 lines (288 loc) · 9.74 KB
/
cashflow.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// TODO: add unit tests
// TODO: clear code
// TODO: translation
// TODO: documentation
"use strict";
export class FlowsBuilder {
constructor(){
this.periodicity =
this.periods =
this.amount =
this.startDate = null;
this.periodUnit = 1;
}
of(periodUnit){
this.periodUnit = periodUnit;
return this;
}
startFrom(date){
this.startDate = date;
return this;
}
repeat(periods){
this.periods = periods;
return this;
}
ofAmount(amount){
this.amount = amount;
return this;
}
days(){
this.periodicity = Periods.DAYS;
return this;
}
weeks(){
this.periodicity = Periods.WEEKS;
return this;
}
months(){
this.periodicity = Periods.MONTHS;
return this;
}
years(){
this.periodicity = Periods.YEARS;
return this;
};
build(){
return Flow.createFlows(this.periods,
new Periodicity(this.periodUnit,this.periodicity),
this.amount, this.startDate);
}
}
export class Flow{
constructor(date = new Date(), amount = 0.0, description = ""){
this.date = date;
this.amount = amount;
this.description = description;
}
static createFlows(n, periodicity, amount, startDate = new Date()){
let flows = [];
for (let i = 0; i < n; i++){
//let date = new Date(startDate);
//date.setDate(date.getDate() + i * periodicity.periods*periodicity.toDays());
let date = periodicity.nextDate(startDate);
startDate = date;
date = new Date(date.setHours(0,0,0,0));
flows.push(new this(date, amount, `Test flow ${i}`));
}
return flows;
}
}
export class Cashflow {
constructor(){
this.flows = []
}
clear(){
this.flows = []
}
addFlow(flow){
this.flows.push(flow);
}
popFlow(){
this.flows.pop();
}
removeFlow(i=0){
this.flows.splice(i,1);
}
print(){
const table = [];
for (let f of this.flows){
//console.log(` ${f.date.toLocaleDateString()} ${f.amount} ${f.description}`);
table.push([f.date.toLocaleDateString(), f.amount, f.description]);
}
console.table(table);
}
}
export class Finance{
static discountAmount(amount,rate,periods){
const dr = rate / 100;
const discountFactor = 1 / (Math.pow(1 + dr, periods));
const pvf = amount * discountFactor;
return pvf;
}
// Returns the discounted value of an amount at the specified
// rate during the specificed number of periods
static PV(periods, rate, amount) {
if ( typeof periods === "undefined" || periods === null)
throw new Error("No periods specified");
if ( typeof rate === "undefined" || isNaN(rate) || rate === null)
throw new Error ("Rate not valid");
if ( typeof amount === "undefined" || isNaN(amount) || amount === null)
throw new Error ("Amount not valid");
const discountFactor = 1 / Math.pow( 1 + rate, periods);
return amount * discountFactor;
}
// Calculates present value of flows in the cashflow on a key date
// Discount rate should be expressed yearly
// Key date should be before all flow dates
// TODO: esta funcion solo debería descontar un flujo?
static discountFlows(cashflow, rate, keyDate) {
if ( typeof rate === "undefined" || isNaN(rate) || rate === null)
throw new Error ("No discount rate specified");
if ( typeof keyDate === "undefined" || keyDate === null)
throw new Error ("No key date specified");
if ( typeof cashflow === "undefined" || cashflow === null)
throw new Error ("No flows specified");
// Validate if any flow exists
if ( cashflow.length === 0)
throw new Error("No flows in Cashflow");
// Validate if keyDate is before flows dates
if ( cashflow.flows.some( f => { return (f.date < keyDate) }) )
throw new Error("Key date should be before all flow dates.");
// Present date
const presentDate = keyDate;
// Present Value calculated as aggregation of discounted flows
// until present date using passed discount rate
// assuming it represents the investment payment at present date
const flows = cashflow.flows;
const pv = flows.reduce( (acum, f) => {
const periods = daysBetween(presentDate, f.date) / 365;
const pvf = this.PV(periods, rate, f.amount);
return acum + pvf;
},0);
return pv;
}
// Calculates net present value for a cashflow
// Present date is assumed to be the date of firs flow
// Discount rate should be expressed yearly
static XNPV(cashflow, rate){
try {
return this.discountFlows(cashflow,rate,cashflow.flows[0].date);
} catch (e) {
throw e;
}
}
// Calculates internal return rate (IRR) for the cashflow.
// Flows can be non periodical.Year is are considered to be 365 days
// Newton Raphson iteration is used to calculate the result
static XIRR(cashflow, initialGuess = 0.10){
//this.validateFlows();
if (! (cashflow.flows.some( f => { return f.amount > 0 } ) &&
cashflow.flows.some( f => { return f.amount < 0 })))
throw new
Error("Cashflow must contain positive and negative flows");
// Validate if any flow exists
if (cashflow.flows.length === 0)
throw new Error("No flows in Cashflow");
// At least one flow amount must exist with amount < 0
if (cashflow.flows[0].amount > 0)
throw new Error("No investment Flow found");
let rate = initialGuess;
const maxRelError = 0.00000001/100;
const maxIterations = 10000;
let error = Number.MAX_VALUE;
let iterations = 0;
const presentDate = cashflow.flows[0].date;
let f = r => {
const pv = this.XNPV(cashflow, r);
return pv;
}
let fprime = r => {
let flows = cashflow.flows.slice(1,cashflow.flows.length);
return flows.reduce(
(acum,flow) => {
let period = daysBetween(flow.date, presentDate)/365;
return acum - 1*flow.amount*period*Math.pow(1+r,-period-1);
},0);
}
console.time("IRR calculation");
while ((iterations < maxIterations) && ( error > maxRelError)){
let oldRate = rate;
rate = rate - f(rate)/fprime(rate);
error = Math.abs((rate-oldRate)/rate);
iterations++;
}
console.timeEnd("IRR calculation");
console.debug("Iterations",iterations);
console.debug(`Relative Error: ${(error*100).toFixed(15)}%`);
console.debug("Rate: ",rate);
if (iterations === maxIterations)
throw new Error("Could not determine IRR");
return Math.fround(rate);
}
}
export const Periods = {
DAYS:1,
WEEKS:2,
MONTHS:3,
YEARS:4
}
export class Periodicity {
constructor(periods, periodType){
this.periods = periods,
this.periodType = periodType
}
static days(n){
return new this(n,Periods.DAYS);
}
static weeks(n){
return new this(n,Periods.WEEKS);
}
static months(n){
return new this(n,Periods.MONTHS);
}
static years(n){
return new this(n,Periods.YEARS);
}
nextDate(dateFrom){
let newDate = dateFrom;
switch(this.periodType){
case Periods.MONTHS:
newDate = new Date(dateFrom.setMonth(dateFrom.getMonth() + 1 * this.periods));
break;
case Periods.DAYS:
newDate = new Date(dateFrom.setDate(dateFrom.getDate() + 1 * this.periods));
break;
case Periods.YEARS:
newDate = new Date(dateFrom.setFullYear(dateFrom.getFullYear() + 1 * this.periods));
break;
case Periods.WEEKS:
newDate = new Date(dateFrom.setDate(dateFrom.getDate() + 7 * this.periods));
break;
default:
throw new Error("unrecognized period type");
}
return newDate;
}
toDays(){
switch (this.periodType){
case Periods.DAYS:
return 1;
break;
case Periods.WEEKS:
return 7;
break;
case Periods.MONTHS:
return 30;
break;
case Periods.YEARS:
return 365;
break;
}
}
}
Object.freeze(Periods);
export function daysBetween(firstDate, secondDate){
const oneDay = 24 * 60 * 60 * 1000;
const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
return diffDays;
}
export function addDays(startDate,days){
const newDate = new Date(startDate);
newDate.setDate(newDate.getDate() + days);
return newDate;
}
/* Sample Usage */
/*
const cashflow = new Cashflow();
// investment flow
cashflow.addFlow( new Flow(new Date(), -10000, "Disbursement"));
// repayment flows
new FlowsBuilder().startFrom(addDays(new Date(),30))
.repeat(12).of(1).months().ofAmount(1000).build()
.forEach(f => cashflow.addFlow(f));
// IRR calculation
Finance.XIRR(cashflow);
// NPV calculationi using 15% discount rate
Finance.XNPV(cashflow, 0.15);
*/