ebradyjobory / finance.js Goto Github PK
View Code? Open in Web Editor NEWA JavaScript library for common financial calculations
License: MIT License
A JavaScript library for common financial calculations
License: MIT License
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;
}
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.
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?
@essamjoubori
The typescript definition for this method says it returns a number, but actual library code returns a number array.
Line 84 in 6d571ea
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
excel
Please let me know your thoughts on how to overcome that.
Thank you!
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.
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
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.
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!
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.
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!
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).
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));
}
The example in the documentation page for FV is wrong
finance.FV(0.5, 1000, 12);
=> 1068.68
It should say 1061.68
The unit test is correct though cal.FV(0.5, 1000, 12).should.equal(1061.68);
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.
Hello everyone,
For amortization class, I need to calculate a growing annuity. Any ideas on how to do it? Thanks everyone.
https://en.wikipedia.org/wiki/Time_value_of_money#Present_value_of_a_growing_annuity
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
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;
})
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)
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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.