Skip to content

Instantly share code, notes, and snippets.

@igricart
Created May 2, 2023 10:33
Show Gist options
  • Save igricart/2e240fdcf5d338f624053f1120ecdb82 to your computer and use it in GitHub Desktop.
Save igricart/2e240fdcf5d338f624053f1120ecdb82 to your computer and use it in GitHub Desktop.
import numpy as np
def investment_value(principal, rate, time, n, dividends, reinvest_dividends, brokerage_fee, investment_fee):
if reinvest_dividends:
# Calculate dividend reinvestment
for t in range(time):
# Calculate interest with compound
principal = principal * (1 + rate / n) ** n
# Calculate and reinvest dividends
dividend_income = principal * dividends
dividend_income_minus_fees = dividend_income - brokerage_fee - investment_fee
if dividend_income_minus_fees > 0:
principal += dividend_income_minus_fees
else:
# Calculate interest with compound
principal = principal * (1 + rate / n) ** (n * time)
# Calculate and distribute dividends
total_dividends = principal * dividends * time
total_fees = (brokerage_fee + investment_fee) * time
principal += total_dividends - total_fees
return principal
# Parameters
principal = 10000 # Initial investment
rate = 0.07 # Annual interest rate (as a decimal)
time = 10 # Number of years
n = 1 # Number of times interest is compounded per year
dividends = 0.02 # Annual dividend yield (as a decimal)
reinvest_dividends = True # Set to True to reinvest dividends, False to distribute
brokerage_fee = 10 # Brokerage fee per dividend reinvestment or distribution
investment_fee = 5 # Investment fee per dividend reinvestment or distribution
final_value = investment_value(principal, rate, time, n, dividends, reinvest_dividends, brokerage_fee, investment_fee)
print(f"Final investment value: ${final_value:,.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment