Consider current month in FIRE calculation (#1349)

pull/1353/head
Yash Solanki 2 years ago committed by GitHub
parent a323313c71
commit 246de7aa86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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…
Cancel
Save