Beta

Calculate daily accurate interest for a bank account

20Odd

Description:

Calculate the interest paid per year with daily accuracy and the actual number of days per year (act/act).

It is the end of the year and the bank has to calculate how much interest to pay you. It pays the interest with a daily accuracy and over the actual days per year. This method of calculating interest is called act/act. Some other methods assume 30 days per month and 360 days per year (30/360), but we want to calculate the interest more accurately.

To achieve this you need a couple of things:

  • A list of transactions of which each transaction is a tuple of an ISO time string and the value of the transaction.
    ('2015-04-20 10:12:13', 123.45)
    
  • The initial balance, e.g. 1000, meaning the balance at the start of the year
  • The interest rate per year, e.g. 0.02 for 2% interest per year.

Let's get into an example to make things a little clearer:
Let's say we want to calculate the total interest paid in the year 2019 for a bank account with the following transactions

[('2019-04-20 10:12:13', 100.00), ('2019-10-11 16:45:00', 25.50)]

and an initial balance of 1000 and an interest rate per year of 0.025 or 2.5%

The interest rate per day is 0.025 / 365 = 0.000068493150685 because the year 2019 had 365 days (no leap year)
Our first interest period is 2019-01-01 - 2019-04-20 which is 109 days long and the balance for that period was 1000
The second interest period is 2019-04-20 - 2019-10-11 which is 174 days long and the balance for that period was 1100
The last interest period is 2019-10-11 - 2020-01-01 which is 82 days long and the balance for that period was 1125.50

The total interest paid can then be calculated using the interest per day for these periods:
First period: 1000 * 0.000068493150685 * 109 = 7.465753424657534
Second period: 1100 * 0.000068493150685 * 174 = 13.10958904109589
Third period: 1125.50 * 0.000068493150685 * 82 = 6.321301369863014

Resulting in a total interest paid of 26.89664383561644 or 26.90 when rounded to two decimal places.
The balance at the end of the year would therefore be 1152.40

Notes:

  • The transactions are all in the same year;
  • If the balance becomes negative over a period of the year, the same interest rate is still used, resulting in negative interest for that period;
  • Interest is paid starting from the day of the transaction (a transaction on December 31st would still get interest for one day);
  • The final balance has to be rounded to two decimal places;
  • Transactions are not necessarily ordered by date;
  • No rounding takes place during the calculation.
Algorithms

Stats:

CreatedFeb 7, 2020
PublishedFeb 7, 2020
Warriors Trained129
Total Skips48
Total Code Submissions194
Total Times Completed20
Python Completions20
Total Stars3
% of votes with a positive feedback rating69% of 8
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes2
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Odd Avatar
  • 4500zenja1 Avatar
Ad