2

So I have an account that provides a 2.02% annual return plus rf%, which is just the annual deposit rate given in this list below. I first made the principle deposit of $2500 in July 2018 at 11:59pm. On the last day of every month I make an additional $800 deposit, ex: on August 31 '18 at 11:59pm I deposit $800 and so on. The list starts from August' 18 till Jan'22.

rate = [1.91, 1.95, 2.19, 2.20, 2.27, 2.40, 2.40, 2.41, 2.42, 2.39, 2.38, 2.40, 2.13, 2.04, 1.83, 1.55, 1.55, 1.55, 1.58, 0.65, 0.05, 0.05, 0.08, 0.09, 0.10, 0.09, 0.09, 0.09, 0.09, 0.09, 0.08, 0.07, 0.07, 0.06, 0.08, 0.10, 0.09, 0.08, 0.08, 0.08, 0.08, 0.08]

The code I have created is very simplistic in nature and when i run it doesn't give me the exact number up to two decimal points as I want. I also want to implement the list of rates I have to correspond to the months I want to know the amount. In this case it's Ocotober '18 which it's rate is 2.19, and for Feb'22 it's 0.08.

P =int(2500)
r =1.91
r2=0.08
FV1= P*(1+(r+2.02)//(100//12))+800
FV2= P*(1+(r2+2.02)//(100//12))+800
print("The value of my account on September 1st 2018, 00:00 am is",FV1)
print("The value of my account on February 1st 2022, 00:00 am is",FV2)

For example, if all goes well for september '18 the value should be; 2500*(1+(1.91+2.02)/100/12)+800 = 3308.19.

1 Answers1

0

I'm not sure your equation is right. It's been a while since I looked at the compounding interest formula, but if I were you that's where I'd start.

Alternatively, I'd iteratively calculate the interest for each month and add it to the principal for the next month, since your rate of interest is changing

# These two imports just to add one month to print nice output
import datetime 
from dateutil.relativedelta import relativedelta

bonus_rates = [1.91, 1.95, 2.19, 2.20, 2.27, 2.40, 2.40, 2.41, 2.42, 2.39, 2.38, 2.40, 2.13, 2.04, 1.83, 1.55, 1.55, 1.55, 1.58, 0.65, 0.05, 0.05, 0.08, 0.09, 0.10, 0.09, 0.09, 0.09, 0.09, 0.09, 0.08, 0.07, 0.07, 0.06, 0.08, 0.10, 0.09, 0.08, 0.08, 0.08, 0.08, 0.08]

base_rate = 2.02

monthly_deposit = 800

principal = 2500
current_date = datetime.date(2018, 7, 31) 

acct_values = []

for month_bonus_rate in bonus_rates:
    current_date += relativedelta(months=1) # Increment the month
    r = (base_rate + month_bonus_rate) / 12 # The rate for this month
    i = principal * r / 100                 # The interest for this month

    principal += (i + monthly_deposit)      # Add interest and monthly deposit to new principal
    acct_values.append(principal)
    print(f"At the end of {current_date.strftime('%Y-%m')}, account value = {principal:.2f}")

Which gives the output:

At the end of 2018-08, account value = 3308.19
At the end of 2018-09, account value = 4119.13
At the end of 2018-10, account value = 4933.58
At the end of 2018-11, account value = 5750.93

...

At the end of 2021-09, account value = 34401.70
At the end of 2021-10, account value = 35261.90
At the end of 2021-11, account value = 36123.61
At the end of 2021-12, account value = 36986.82
At the end of 2022-01, account value = 37851.55

The acct_values list is:

[3308.1875,
 4119.132086979167,
 4933.583375384319,
 5750.933143587754,
 6571.49272957608,
...
 35261.898333886056,
 36123.606655970354,
 36986.822967618304,
 37851.54990781163]

As you can see, the value of your account on September 1 is calculated correctly as 3308.1875 (or .19 when rounded to two places)

You can access the elements of the list to get the values for any of the months.

Note that using floating-point numbers for counting money has some inherent problems that you should be aware of.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Hi thank you! Would there be a way for this code to be written without "current_date += relativedelta(months=1) " code. – anthonyceaser2021 Feb 23 '22 at 00:36
  • Well that's just there to print out the message without having to add months ourselves and has nothing to do with the actual compound interest code. You can get rid of it if you don't care about printing the month in the loop – Pranav Hosangadi Feb 23 '22 at 14:07
  • so if i want the value of october '18 and feb '22 exactly what would i put to get the value and to print the result – anthonyceaser2021 Feb 23 '22 at 15:02
  • Instead of printing it, append `principal` to a list and select the correct index from the list after the loop – Pranav Hosangadi Feb 23 '22 at 15:09
  • 1
    sorry for asking you for more clarification but how would i be able to do that per say? would it be like bonus_rates.append(principal) – anthonyceaser2021 Feb 23 '22 at 15:20
  • No, you'd create a _new_ list before the loop, e.g. `acct_values = []`. Then append to that list. Think about this - If you want all the account values, why would you append them to a list that already contains interest rates? See the edited answer. @anthonyceaser2021 – Pranav Hosangadi Feb 23 '22 at 16:03
  • So I added the two values I want for oct 1 '18 and feb 1 '22 using this code print("The value of my account on October 1st 2018 ",[i[0] for x in acct_values] ) but it gives me an error saying "float object is not sucestible" – anthonyceaser2021 Feb 23 '22 at 16:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242335/discussion-between-pranav-hosangadi-and-anthonyceaser2021). – Pranav Hosangadi Feb 23 '22 at 16:36