Giter Club home page Giter Club logo

finance.js's Introduction

Build Status

Finance.js

Introduction

Finance.js makes it easy to incorporate common financial calculations into your application. The library is built on pure JavaScript without any dependencies.

This project is hosted on GitHub. You can report bugs and discuss features on the GitHub issues page. Finance.js is available for use under the MIT software license.

Getting Started

npm install financejs --save

or

  • Download or fork the repository from GitHub.
  • Extract the file finance.js from the project and include it in your application on the client side.

Example Usage

var Finance = require('financejs');
var finance = new Finance();
// To calculate Amortization
finance.AM(20000, 7.5, 5, 0);
// => 400.76

Typescript

import { Finance } from 'financejs'
let finance = new Finance();
// To calculate Amortization
finance.AM(20000, 7.5, 5, 0);
// => 400.76

Tests

npm test

Available Methods

Amortization

finance.AM(principal, rate, total number of payments, [type]);

Amortization is the paying off of debt with a fixed repayment schedule in regular installments over a period of time.1

For total number of payments which are entered as years, [type] takes a 0, whereas for months [type] takes a 1.

#Total Number of Payments Type = Years
 // e.g., If principal is $20,000, rate is 7.5%, total number of payments is 5, and payment type is 0 (years), monthly payment is $400.76.

 finance.AM(20000, 7.5, 5, 0);
 => 400.76

 #Total Number of Payments Type = Months
 // e.g.,If principal is $20,000, rate is 7.5%, total number of payments is 60, and payment type is 1 (months), monthly payment is $400.76.

 finance.AM(20000, 7.5, 60, 1);
 => 400.76

Compound Annual Growth Rate (CAGR)

finance.CAGR(beginning value, ending value, number of periods);

Compound Annual Growth Rate (CAGR) is the year-over-year growth rate of an investment over a specified period of time.2

// e.g., If the beginning value is $10,000, the ending value is $19,500, and the number of periods is 3, the CAGR is 24.93%.

 finance.CAGR(10000, 19500, 3);
 => 24.93

Compound Interest (CI)

finance.CI(rate, compoundings per period, principal, number of periods);

Compound Interest is the interest calculated on the initial principal and also on the accumulated interest of previous periods of a deposit or loan.3

// e.g., If rate is 4.3%, the compoundings per period is 4, the principal is $1,500, and the number of periods is 6, the compound interest is $1,938.84.

 finance.CI(4.3, 4, 1500, 6 );
 => 1938.84

Discount Factor (DF)

finance.DF(rate, number of periods);

The Discount Factor (DF) is the factor by which a future cash flow must be multiplied in order to obtain the present value.4

// e.g., If rate is 10% and the number of periods is 6, the result is an array of discount factors: [1, 0.91, 0.827, 0.752, 0.684].

 finance.DF(10, 6);
 => [1, 0.91, 0.827, 0.752, 0.684]

Future Value (FV)

finance.FV(rate, cash flow, number of periods);

Future Value (FV) is the value of an asset or cash at a specified date in the future that is equivalent in value to a specified sum today5

// e.g., If rate is 0.5%, cash flow is $1,000, and the number of periods is 12, the FV is $1,061.68.

 finance.FV(0.5, 1000, 12);
 => 1061.68

Internal Rate of Return (IRR)

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

Internal Rate of Return (IRR) is the discount rate often used in capital budgeting that makes the net present value of all cash flows from a particular project equal to zero.6

// e.g., If initial investment is -$500,000 and the cash flows are $200,000, $300,000, and $200,000, IRR is 18.82%.

 finance.IRR(-500000, 200000, 300000, 200000);
 => 18.82

XIRR

finance.XIRR([cash flows], [dates], guess);

XIRR is used to determine the Internal Rate of Return when the cash flows are at Irregular intervals.15

// e.g., If the cash flows are -$1,000 on 1st Nov 2015, -$100 on 01 Jul 2016 and $1,200 on 19 Jul 2016, the XIRR is 14.11%.

 finance.XIRR([-1000, -100, 1200],[new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )],0 );
 => 14.11

Leverage Ratio (LR)

finance.LR(total liabilities, total debts, total income);

Leverage Ratio (LR) is used to calculate the financial leverage of a company or individual to get an idea of the methods of financing or to measure ability to meet financial obligations.7

// e.g., If total liabilities are $25, total debts are $10, and total income is $20, the leverage ratio is 1.75.

 finance.LR(25, 10, 20);
 => 1.75

Net Present Value (NPV)

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

Net Present Value (NPV) compares the money received in the future to an amount of money received today, while accounting for time and interest [through the discount rate]. It's based on the principal of time value of money (TVM), which explains how time affects monetary value.8

[cash flows] takes any number of projected cash flows.

// e.g., 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

Payback Period (PP)

finance.PP(number of periods, [cash flows]);

Payback Period (PP) is the length of time required to recover the cost of an investment.9

number of periods takes a 0 value for even cash flows;
for uneven cash flows, number of periods takes any number of projected periods.

[cash flows] takes any number of projected cash flows.

#Even Cash Flows
 // e.g., Because even cash flows have the same inflow during each period, we set 'number of periods' to '0.' If initial investment is -$105 and the annual cash flow is $25, the payback period is 4.2 years.

 finance.PP(0, -105, 25);
 => 4.2

 #Uneven Cash Flows
 // e.g., If number of periods is 5, initial investment is -$50, and the cash flows are $10, $13, $16, $19, and $22 for each year, the payback period is 3.42 years.

 finance.PP(5, -50, 10, 13, 16, 19, 22);
 => 3.42

Present Value (PV)

finance.PV(rate, cash flow, number of periods);

Present Value (PV) is the current worth of a future sum of money or stream of cash flows given a specified rate of return.10

number of periods is optional and defaults to 1.

// e.g., If rate is 5% and cash flow is $100, the PV is $95.24.

 finance.PV(5, 100);
 => 95.24

// e.g., If rate is 5%, cash flow is $100, and number of periods is 5, the PV is $78.35.

 finance.PV(5, 100);
 => 95.24

Profitability Index (PI)

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

Profitability Index (PI) is an index that attempts to identify the relationship between the costs and benefits of a proposed project through the use of a ratio calculated.11

[cash flows] takes any number of projected cash flows.

// e.g., If rate is 10%, initial investment is -$40,000, cash flows are $18,000, $12,000, $10,000, $9,000, and $6,000, PI is 1.09.

 finance.PI(10, -40000, 18000, 12000, 10000, 9000, 6000);
 => 1.09

Return on Investment (ROI)

finance.ROI(initial investment, earnings);

Return on Investment (ROI) is a simple calculation that tells you the bottom line return of any investment.12

// e.g., If initial investment is -$55,000 and the earnings are $60,000, the return on investment is 9.09%.

 finance.ROI(-55000, 60000);
 => 9.09

Rule of 72 (R72)

finance.R72(rate);

Rule of 72 (R72) is a rule stating that in order to find the number of years required to double your money at a given interest rate, you divide the compound return into 72.13

// e.g., If annual rate is 10%, rule of 72 is 7.2 years.

 finance.R72(10);
 => 7.2

Weighted Average Cost of Capital (WACC)

finance.WACC(market value of equity, market value of debt, cost of equity, cost of debt, tax rate);

Weighted Average Cost of Capital (WACC) is the rate that a company is expected to pay on average to all its security holders to finance its assets.14

// e.g., If market value of equity is $600,000, market value of debt is $400,000, cost of equity is 6%, cost of debt is 5%, and tax rate is 35%, WACC is 4.9%.

 finance.WACC(600000, 400000, 6, 5, 35);
 => 4.9

Loan Payment Per Period (PMT)

finance.PMT(fractional interest rate, number of payments, principal);

Payment for a loan based on constant payments and a constant interest rate


   finance.PMT(0.02,36,-1000000);
 => 39232.8526

Inflation-adjusted Return

finance.IAR(investment Return, inflation Rate);

Measure the return taking into account the time period's inflation rate


   finance.IAR(0.08, 0.03)
 => 4.85

Contributing

Contributions are welcome to aid in the expansion of the library. In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, and please lint and test your code.

To Do

  • Expand library with more financial calculations
  • Include edge cases in testing, if any

Contributing

Contributions are welcome to aid in the expansion of the library. In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, and please lint and test your code.

To Do

  • Expand library with more financial calculations
  • Include edge cases in testing, if any

finance.js's People

Contributors

alhassanebarry avatar creaoticx avatar ebradyjobory avatar editter avatar gpedro avatar igorsantos07 avatar mmcdermott avatar olamilekan000 avatar seanpat09 avatar sharddith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

finance.js's Issues

Finance.js IRR function problem.

Hi, Essam.
I am Jacky from Taiwan.

Few day ago, my friend has a project use Finance.js, IRR function.
But, function was not enough precision than Excel IRR function.
So, my friend ask for my help

I look your program, and find 2 issues.

  1. dynamic guess variable isn't exist, and decimal is not enough.
  2. recursive call will happen stack error.

First, i tell to my friend to modify your program at some const variable.
That will fix issues 1, but at same time it will happen issues 2.
Because i use more decimal and more precision guess, it mean time complexity will grow, in recursive call it also mean more stack.

So, i write a do-while version to calculate, but performance still limit by browser.
Then, i write a burst-guess to speed-up, when VPN too huge.
e.g : Math.log(VPN).

My code is over here.
https://github.com/eastmoon/Tutorial-JavaScript/blob/master/Issues-FinanceIRR/Index.html

Hope it would be helpful for you

New maintainer of this project

Hi,

I use this library very often and for my own DCF calculator.

It seems as though this package has been abandoned. Could I request to take it over @ebradyjobory

I'd like to refactor it to normal functions, add testing, add more functions etc.

Thanks

Time-weighted Rate of Return?

Love this library! :)

Would it be possible to implement a Time-Weighted Rate of Return function? I'm working on a project that needs it. If I end up creating my own function, I'll just PR it here, but I'm sort of hoping that there's an existing function that can just be incorporated here.

XIRR incorrect values for very large datasets

XIRR value is being calculated incorrectly for transactions of order 40k-50k, the incorrectness increases when the number of transactions increase say 80k-90k

This I had checked with other xirr packages which were returning the same value as well as Excel, which confirmed the calculations. The value calculated by this package was incorrect

PV not working the same as Excel

Gives totally different values compared to other PV calculations. Not sure if this is different version of PV but it doesn't seem correct.
For reference, I use this function which behaves the same as Excel's version:

PV(rate, numOfPayments, pmt) {
		return pmt / rate * (1 - Math.pow(1 + rate, -numOfPayments));
	}

[IRR-Question] Excel can generate result and finance.js can't

Hi @essamjoubori nice effort to write this package!
Pretty awesome!

I'm a bit new to this world and I'm having some difficult to understand why excel can do IRR with some values and finance.js can't. I'm guessing it's because javascript itself can't deal with imaginary numbers.

finance.js

screen shot 2017-04-23 at 22 41 33

excel

screen shot 2017-04-23 at 22 41 13

Please let me know your thoughts on how to overcome that.
Thank you!

Is there a need for a real object in this library?

It would be much more friendly if the library was just a pack of functions, just like lodash or many others.

Why do we need to instantiate an empty object so we use the functions?
That also blocks us to rename functions when importing (i.e. futureValue makes more sense than a FV acronym, for laymen looking at the code).

NPV not working

I tried your example:

finance.NPV(10, -500000, 200000, 300000, 200000);

I get 80015.03

But when I try it in Excel: =NPV(0.1, -500000, 200000, 300000, 200000)

I get 72740.93

Am I missing something here?

I need to replicate the NPV functionality in Jscript but I can't do this when I am getting 2 different results.

Any help would be appreciated.

[XIRR] The return of the follow instruction is not a % is an amount

I execute with the version "financejs": "^4.1.0",

the follow instruction

console.log(finance.XIRR([-91000.00, 75000.00, -11500.00, -3500.00, 75000.00, -86720.00, -2500.00, -8000.00, 75000.00, -2500.00, -480.00, 75000.00, -1040.00],[ new Date(2018, 10, 29 ), new Date(2018, 10, 29 ), new Date(2018, 11, 12 ), new Date(2018, 11, 22 ), new Date(2018, 11, 22 ), new Date(2018, 11, 29 ), new Date(2018, 12, 3 ), new Date(2018, 12, 10 ), new Date(2018, 12, 10 ), new Date(2018, 12, 20 ), new Date(2018, 12, 22 ), new Date(2018, 12, 27 ), new Date(2019, 1, 10 )],0));

and gives me the follow amount as a result

83206612.79

The idea as I understan os this function is return e % not an amount .

Someone knows, what could be the problem?

seekZero IRR bug

the seekZero function gets into an infinite loop for some inputs to the IRR function.
Have patched it to abort the calculation after a second

//seekZero seeks the zero point of the function fn(x), accurate to within x \pm 0.01. fn(x) must be decreasing with x.
function seekZero(fn) {
var x = 1;
var ind = new Date().getTime();
while (fn(x) > 0) {
x += 1;
if(new Date().getTime()-ind>1000)
throw("seekZero failed");
}
while (fn(x) < 0) {
x -= 0.01
if(new Date().getTime()-ind>1000)
throw("seekZero failed");
}
return x + 0.01;
}

Bower package

It'd be great if you could create a bower package for this so people don't have to manually copy it over into their browser projects.

finance.IRR causes "Cannot convert undefined or null to object" error

The example:

finance.IRR(-500000, 200000, 300000, 200000);

Result:

    Uncaught TypeError: Cannot convert undefined or null to object
    at slice (<anonymous>)
    at Finance.IRR (finance.self-e3a7ba8fd778d256a438ea068bd511733e341d18b95ce0cda6e89071e7a0edb2.js?body=1:51)
    at <anonymous>:1:9

Here:

    Finance.prototype.IRR = function(cfs) {
    var depth = cfs.depth;
    var args = cfs.cashFlow;
    var numberOfTries = 1;
    // Cash flow values must contain at least one positive value and one negative value
    var positive, negative;
    Array.prototype.slice.call(args).forEach(function (value) { // <<<<< Right here
        if (value > 0) positive = true;
        if (value < 0) negative = true;
    })

image

IRR precision

Hello!

I'm seeing a discrepancy calculating IRR compared to Excel. It isn't huge, but when annualising it is enough for it to be a problem.

Consider the following code.

cashFlow = [-206136.99, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 8993.21, 18993.21];
// Calculate monthly IRR
IRR = finance.IRR( { depth: 1500, cashFlow: cashFlow } );
// Annualise IRR
IRR = (Math.pow(1 + IRR / 100, 12) - 1) * 100;

This returns 0.72% (exactly) which annually becomes 8.99% (8.990490026994502).
Compared to Excel which gives me 0.71414% monthly and 8.91% annually (0.0891443135).

I would appreciate any clues as to why this is happening. I find the exact 0.72 a bit suspicious, is it possible that some rounding is happening there?

Cheers!

new calcs

Hello are you currently open to adding new calculators? I am happy to open a pull request for one some I use sometimes including Inflation-adjusted returns.

Not an issue

Just wanted to say thank you for the work you have put towards this. It's working fantastic with version 4. I failed to reply to a previous issue I put forward. I can't find it to reply now, but everything is working great. Thanks again!

Website is outdated

Some functions are missing from the said .org, such as PMT. I spent some time writing that function into the library only to notice later it's already there, but not in the "official" docs.

Please update it :(

Suggestion: make it a simple GHPage based on the README. Much better than leaving both out of sync (and making a markdown file seem like a HTML thing)

Incorrect formula for FV calculation

The FV formula asks for the number of "periods". However, the wording should be changed to "years".

For example, a year might have 12 periods which means that the cumulative interest is calculated once a month. As of right now, the calculator would assume 12 periods means 1 cumulative interest period each year for 12 years.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.