Consider current month in FIRE calculation (#1349)
This commit is contained in:
parent
a323313c71
commit
246de7aa86
@ -25,6 +25,7 @@ import {
|
||||
Tooltip
|
||||
} from 'chart.js';
|
||||
import * as Color from 'color';
|
||||
import { getMonth } from 'date-fns';
|
||||
import { isNumber } from 'lodash';
|
||||
import { Subject, takeUntil } from 'rxjs';
|
||||
|
||||
@ -50,6 +51,8 @@ export class FireCalculatorComponent
|
||||
|
||||
@ViewChild('chartCanvas') chartCanvas;
|
||||
|
||||
private readonly CONTRIBUTION_PERIOD = 12;
|
||||
|
||||
public calculatorForm = this.formBuilder.group({
|
||||
annualInterestRate: new FormControl<number>(undefined),
|
||||
paymentPerPeriod: new FormControl<number>(undefined),
|
||||
@ -296,11 +299,15 @@ export class FireCalculatorComponent
|
||||
label: $localize`Savings`
|
||||
};
|
||||
|
||||
const monthsPassedInCurrentYear = getMonth(new Date());
|
||||
|
||||
for (let period = 1; period <= t; period++) {
|
||||
const periodInMonths =
|
||||
period * this.CONTRIBUTION_PERIOD - monthsPassedInCurrentYear;
|
||||
const { interest, principal, totalAmount } =
|
||||
this.fireCalculatorService.calculateCompoundInterest({
|
||||
P,
|
||||
period,
|
||||
periodInMonths,
|
||||
PMT,
|
||||
r
|
||||
});
|
||||
|
@ -4,35 +4,32 @@ import Big from 'big.js';
|
||||
@Injectable()
|
||||
export class FireCalculatorService {
|
||||
private readonly COMPOUND_PERIOD = 12;
|
||||
private readonly CONTRIBUTION_PERIOD = 12;
|
||||
|
||||
public constructor() {}
|
||||
|
||||
public calculateCompoundInterest({
|
||||
P,
|
||||
period,
|
||||
periodInMonths,
|
||||
PMT,
|
||||
r
|
||||
}: {
|
||||
P: number;
|
||||
period: number;
|
||||
periodInMonths: number;
|
||||
PMT: number;
|
||||
r: number;
|
||||
}) {
|
||||
let interest = new Big(0);
|
||||
const principal = new Big(P).plus(
|
||||
new Big(PMT).mul(this.CONTRIBUTION_PERIOD).mul(period)
|
||||
);
|
||||
const principal = new Big(P).plus(new Big(PMT).mul(periodInMonths));
|
||||
let totalAmount = principal;
|
||||
|
||||
if (r) {
|
||||
const compoundInterestForPrincipal = new Big(1)
|
||||
.plus(new Big(r).div(this.COMPOUND_PERIOD))
|
||||
.pow(new Big(this.COMPOUND_PERIOD).mul(period).toNumber());
|
||||
.pow(periodInMonths);
|
||||
const compoundInterest = new Big(P).mul(compoundInterestForPrincipal);
|
||||
const contributionInterest = new Big(
|
||||
new Big(PMT).mul(compoundInterestForPrincipal.minus(1))
|
||||
).div(new Big(r).div(this.CONTRIBUTION_PERIOD));
|
||||
).div(new Big(r).div(this.COMPOUND_PERIOD));
|
||||
interest = compoundInterest.plus(contributionInterest).minus(principal);
|
||||
totalAmount = compoundInterest.plus(contributionInterest);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user