Skip to content

Instantly share code, notes, and snippets.

@petrbrzek
Created April 11, 2022 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrbrzek/e17e8e97e1bca66ef57f12521988e4b1 to your computer and use it in GitHub Desktop.
Save petrbrzek/e17e8e97e1bca66ef57f12521988e4b1 to your computer and use it in GitHub Desktop.
Calculate compound interest with regular deposits and daily compounding period
const calculateCompoundInterest = ({
initialDeposit,
regularDeposit,
investmentInYears = 1,
depositFrequency = 12,
interestRate,
compoundPeriodsPerYear = 365
}) => {
// https://www.vertex42.com/Calculators/compound-interest-calculator.html#rate-per-period
interestRate = interestRate / 100;
const totalCompoundingPeriods = depositFrequency * investmentInYears;
const rate =
Math.pow(
1 + interestRate / compoundPeriodsPerYear,
compoundPeriodsPerYear / depositFrequency
) - 1;
const futureValue =
initialDeposit * Math.pow(1 + rate, totalCompoundingPeriods) +
regularDeposit * ((Math.pow(1 + rate, totalCompoundingPeriods) - 1) / rate);
return {
futureValue,
interest:
futureValue -
initialDeposit -
regularDeposit * depositFrequency * investmentInYears,
totalDeposit:
initialDeposit + regularDeposit * depositFrequency * investmentInYears
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment