Chris Mendez in Javascript, Finance

Finance: Calculating Present and Future Value using ES6

I'm currently enrolled as a USC Marshall EMBA student aiming to graduate in 2020. One of the first classes I'm taking is Finance and this week we're learning about "Present Value", "Net Present Value", "Discount Rate", "Future Value" and a few other money fundamentals. While working through a few practice problems using Microsoft Excel, I started to think about what a few of these concepts would look like if I were to write them in ES6.

Below are a few key concepts written using Node and ES6. They are simple by design so that everyone can quickly grasp the algebraic expressions.

If anyone has any helpful hints or suggestions, please share.

const util = require("util");

class Currency {
    static get formatter() {
        return new Intl.NumberFormat('en-US', {
            style: "currency",
            currency: "USD",
            minimumFractionDigits: 2
        })
    }
    static USD(number) {
        let f = this.formatter;
        return f.format(number);
    }
}

class MyFinance {
    //
    // Present Value = Tommorrow's Cash / (1 + Rate of Return)
    // PMT = the sum after investing over a period of times
    // IR = Rate of Return / Interest Rate
    // NP = Number of Periods (usually years)
    PV(PMT, IR, NP = 1) {
        var PV = PMT * (1 - Math.pow(1 + IR, -NP)) / IR
        return PV;
    }
    // Net Present Value
    NPV(Cost, PMT, IR, NP = 1) {
        return Math.abs(Cost - this.PV(PMT, IR, NP));
    }
    //
    // Future Value = Todays Cash x (1 + Rate of Return)^ Number of Periods
    // PMT = the sum after investing over a period of times
    // IR = Rate of Return / Interest Rate
    // NP = Number of Periods (usually years)
    FV(PMT, IR, NP = 1) {
        var FV = PMT * (Math.pow(1 + IR, NP))
        return FV;
    }
    Perpetuity(payment) {
        let payments = [];
        let nper = 999;
        var i = 0;
        while (i < nper) {
            payments.push(payment);
            i = i + 1;
        }
        return payments;
    }
    // Internal Rate of Return
    // Used to calculate investments
    // Its looking for an array of payments
    IRR(CArray = []) {
        var min = 0.0;
        var max = 1.0;
        do {
            var guest = (min + max) / 2;
            var NPV = 0;
            for (var j = 0; j < CArray.length; j++) {
                NPV += CArray[j] / Math.pow((1 + guest), j);
            }
            if (NPV > 0) {
                min = guest;
            }
            else {
                max = guest;
            }
        } while (Math.abs(NPV) > 0.000001);
        return guest * 100;
    }
}

// // // // // // // // // // // // // // // // // // 
// // // // // // // // // // // // // // // // // // 

// A. Create a new Finance Object
var myFinance = new MyFinance();

// // // // // // // // // // // // 
// Present Value 
// @ Future Cash Payment Amount
// @ Discount Rate
var futureCash = 11000;
var rate = 0.10;
var presentValue = myFinance.PV(futureCash, rate, 1);
console.log("Present Value: ", Currency.USD(presentValue));


// // // // // // // // // // // // 
// Future Value 
// @ Current Cash Payment Amount
// @ Discount Rate
var currentCash = 10000;
var futureValue = myFinance.FV(currentCash, rate);
console.log("Future Value: ", Currency.USD(futureValue));


// // // // // // // // // // // // 
// Internal Rate of Return 
// @ An Array of Payments

// Year one and two, a company loses money
var losses = [-200000, -350000];
// Then by year three onward, a company earns $50k
var wins = myFinance.Perpetuity(50000)
// Merge the two arrays
var mergedArr = [...losses, ...wins]
// Calculate the Percentage of Return
var irr = myFinance.IRR(mergedArr);
console.log("IRR: ", irr);

Why Does This Matter?

Suppose you had $10,000 and you wanted to make an investment. Your two options are:

  1. Invest in a low-cost index fund such as Vanguard 500 Index Fund Admiral Class (NASDAQMUTFUND:VFIAX) which requires a $10k minimum investment but provides an expected return (after taxes of) 10%.
  2. Invest in your friend who promises to give you $10,800 next year.

Which investment should you make?

You should invest in the stock market because you will earn $11k instad of $10,800. A $200 difference.

const finance = new MyFinance();
var currentCash = 10000;
var rate = 0.10;
var periods = 1;

// Get the Present Value based on your future cash promise.
var friendPromise = 10800
var friendROI = finance.PV(friendPromise, rate, 1) - currentCash;
// Get the Future Value based on your current cash.
var stockROI = finance.FV(currentCash, rate) - currentCash;

console.log("One year from now, my results will look like this:")
console.log("Friend ROI: ", Format.toCash(friendROI), " | ", "Stock ROI:", Format.toCash(stockROI))

Resources