Introducing Finance.js


Introduction

While I was studying for an MBA, I had to calculate complicated financial equations either by hand or with Excel. Like everyone else I'm sure, it was not a pleasant experience for me.

In this blog post I discuss one of my latest projects, Finance.js, an open source library that makes it easy to incorporate common financial calculations into your application. The library is built on pure JavaScript without any dependencies.

The Algorithms

While I was developing algorithms for each financial calculation function, I took the following into consideration:

  • The algorithm should be able to compute any number of arguments
  • Solve the algorithm in a linear time complexity even in worst cases
  • Be as concise as possible with the code

The code below is the algorithm for calculating the Internal Rate of Return (IRR). The challenge was to write an algorithm that finds the IRR that returns a Net Present Value (NPV) of 0 (or as close to 0 as possible) while also accepting any number of arguments. In order to accomplish this, I used recursion. The base case of the recursion occurs when the calculated NPV returns a value equal to or below zero.

// Internal Rate of Return (IRR)
Finance.prototype.IRR = function(cfs) { 
var bestGuess, currentNPV;
var checkNPV = function(rate, arguments){
  var npv = arguments[0];
  for (var i = 1; i < arguments.length; i++) {
    npv +=(arguments[i] / Math.pow((1 + rate/100), i));
  }
  currentNPV = Math.round(npv * 100) / 100;
  // base case
  if (currentNPV <= 0) {
    bestGuess = rate;
    console.log(bestGuess)
    return;
  } 
  checkNPV(rate + 0.01, arguments);
}; 
checkNPV(0.01, arguments);
return Math.round(bestGuess * 100) / 100;
};
Working with Finance.js

Working with the library is fairly easy. To include it in your application, you will need to download or fork the repository from GitHub. Extract the file finance.js from the project and include it in your application.

In order to use the library, you will need to first create a new instance of the class Finance.

var finance = new Finance();

Let's say you want to calculate the Net Present Value (NPV) for a given set of cash flows:

finance.NPV(rate, initial investment, [cash flows]);

[cash flows] is any number of projected cash flows. Let's try it out with some sample data:

// If discount rate is 10%, initial investment is -$1,000, cash flow in year 1 is $200,000, year 2 is $300,000, and year 3 is $200,000, the NPV is $80,015.03.

finance.NPV(10, -500000, 200000, 300000, 200000);
=> 80015.03
Testing

In this project I took the test driven development (TDD) approach, using the Chai testing framework. The code below is a snippet of testing for the Internal Rate of Return (IRR) algorithm that I mentioned above:

var assert = chai.assert;
var expect = chai.expect;

var cal = new Finance();

describe('FinanceJS', function() {
  it('should compute IRR', function() {
    var irr = cal.IRR(-500000, 200000, 300000, 200000);
    expect(irr).to.be.within(18, 19); 
  });
});
Looking Ahead

v1.0.0 of the library contains 14 functions. I hope to expand this in the future with the help of contributors. If you would like to contribute, please fork the repo on GitHub and submit a pull request. You can also raise any issues or submit questions here.