npm install financial-fns
Use se import or require
ES6 Modules
import { xirr } from 'financial-fns'
const result = xirr(
[-10000, 2750, 4250, 3250, 2750],
[
new Date('2008-01-01'),
new Date('2008-03-01'),
new Date('2008-10-30'),
new Date('2009-02-15'),
new Date('2009-04-01')
]
)
console.log('RESULT', result) // RESULT 0.3733625335188315
CommonJS
const { xirr } require('financial-fns')
const result = xirr(
[-10000, 2750, 4250, 3250, 2750],
[
new Date('2008-01-01'),
new Date('2008-03-01'),
new Date('2008-10-30'),
new Date('2009-02-15'),
new Date('2009-04-01')
]
)
console.log('RESULT', result) // RESULT 0.3733625335188315
Calculates the internal rate of return (IRR) for a schedule of cash flows that is not necessarily periodic. Excel
- =XIRR (English)
- =TIR.NO.PER (Spanish)
xirr(cashFlow, dates, guess)
Example
const result = xirr(
[-10000, 2750, 4250, 3250, 2750],
[
new Date('2008-01-01'),
new Date('2008-03-01'),
new Date('2008-10-30'),
new Date('2009-02-15'),
new Date('2009-04-01')
]
)
console.log(result) // 0.3733625335188315
Calculates the internal rate of return (IRR) for a series of cash flows represented by the numbers in values. Excel:
- =IRR (English)
- =TIR (Spanish)
// guess default 0,1
irr(cashFlow, guess)
Example
const result = irr([-10000, 2750, 4250, 3250, 2750])
console.log(result) // 0.1154127831005586
Calculates the net present value of an investment based on a discount rate and a series of cash flows.
Excel:
- =NPV (English)
- =VNA (Spanish)
// rate default 0
npv(cashFlow, rate)
Example
const result = npv(
[
-1333.11,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46,
178.46
],
0.05
)
console.log(result) // 248.62588704065456
Calculates the net present value of an investment based on a discount rate and a series of cash flows.
Excel:
- =XNPV (English)
- =VNA.NO.PER (Spanish)
// rate default 0
xnpv(cashFlow, dates, rate)
Example
const result = xnpv(
[-10000, 2750, 4250, 3250, 2750],
[
new Date('2008-01-01'),
new Date('2008-03-01'),
new Date('2008-10-30'),
new Date('2009-02-15'),
new Date('2009-04-01')
],
0.1
)
console.log(result) // 1994.5100406532633
Monthly rate
Excel:
- =RATE (English)
- =TASA (Spanish)
rate(
nPeriods,
payment,
presentValue,
(futureValue = 0),
(dueDateType = 0),
(guess = 0.1)
)
Example
const monthlyRate = rate(4 * 12, -200, 8000)
console.log(monthlyRate) // 0.0077014724882104105
const annualRate = rate(4 * 12, -200, 8000) * 12
console.log(annualRate) // 0.09241766985852493
Calculate number of periods
Excel:
- =NPER (English)
- =NPER (Spanish)
nper(rate, payment, presentValue, (futureValue = 0), (dueDateType = 0))
Example
const result = nper(0, 500, -25000)
console.log(result) // 50
Calculate the future value of an investment based on a constant interest rate. You can use FV with either periodic, constant payments, or a single lump sum payment. Excel:
- =FV (English)
- =VF (Spanish)
fv(rate, totalPeriods, payment, (presentValue = 0), (dueDateType = 0))
Example
const result = fv(0.12 / 12, 12, -1000)
console.log(result) // 12682.503013196972
Future value (applying a series of compound interest rates) Excel:
- =FVSCHEDULE (English)
- =VF.PLAN (Spanish)
fvSchedule(principal, schedule)
Example
const result = fvSchedule(336, [0.02, 0.05, 0.03])
console.log(result) // 370.65168
Returns the effective annual interest rate, given the nominal annual interest rate and the number of compounding periods per year. Excel:
- =EFFECT (English)
- =INT.EFECTIVO (Spanish)
effect(rate, nPeriods)
Example
const result = effect(0.1, 12)
console.log(result) // 0.10471306744129724
Count days between two dates without time
dayCountByDate(dateFrom, dateTo, (abs = false))
Example
const dayCount = dayCountByDate(new Date('2022-06-10'), new Date('2022-02-18'))
console.log(dayCount) // -112
const dayCountAbs = dayCountByDate(
new Date('2022-06-10'),
new Date('2022-02-18'),
true
)
console.log(dayCountAbs) // 112
Count days between two dates without time
dayCountByDateTime(dateFrom, dateTo, (abs = false))
Example
const dayCount = dayCountByDateTime(
new Date('2022-03-04 22:00:00'),
new Date('2022-02-27 22:00:00')
)
console.log(dayCount) // -5
const dayCountAbs = dayCountByDateTime(
new Date('2022-03-04 22:00:00'),
new Date('2022-02-27 22:00:00'),
true
)
console.log(dayCountAbs) // 5
const dayCountAbs2 = dayCountByDateTime(
new Date('2022-03-04 23:00:00'),
new Date('2022-02-27 22:00:00'),
true
)
console.log(dayCountAbs2) // 5.04
Get total days in a frequency
getDaysByFrequency(frequency)
Example
/**
* weekly: 7,
* biWeekly: 14,
* monthly: 1,
* everyTwoMonths: 2,
* everyThreeMonths: 3,
* everyFourMonths: 4,
* everySixMonths: 6,
* yearly: 12,
*/
const monthlyDays = getDaysByFrequency(1)
console.log(monthlyDays) // 30
const biWeekly = getDaysByFrequency(14)
console.log(biWeekly) // 15
Get last date of month.
getLastDateOfMonth(month, year)
Example
/**
* JS Months from 0 to 11
*/
const lastDate1 = getLastDateOfMonth(1, 2022)
console.log(lastDate) // 28
const lastDate2 = getLastDateOfMonth(1, 2024)
console.log(lastDate) // 29
Round a number to a specified number of decimal places.
round(num, places)
Example
const result = round(135.15000004)
console.log(result) // 135.15
const result2 = round(135.15366007, 4)
console.log(result2) // 135.1537