To calculate the total return on an investment with dividends reinvested, you need to simulate the process of reinvesting dividends into buying more shares of the stock at the price available when the dividend is paid. Below is a Python function that calculates the total return of an investment with dividends reinvested.
First, you'll want to have two lists:
prices: a list of tuples with (date, price) where date is the time when the price is valid, and price is the price of the stock at that time.
dividends: a list of tuples with (date, dividend) where date is the payment date of the dividend, and dividend is the amount paid per share.
Here’s a simple Python algorithm to do this:
def reinvested_dividends(prices, dividends, initial_investment):
current_shares = initial_investment / prices[0][1] # Assuming prices are sorted by date
prices_index = 0
price_lookup = {date: price for date, price in prices}
for dividend_date, dividend_amount in dividends:
# Find the stock price at dividend payment date
while prices_index + 1 < len(prices) and prices[prices_index + 1][0] <= dividend_date:
prices_index += 1
dividend_price = price_lookup[prices[prices_index][0]]
# Calculate dividends earned and reinvest to buy more shares
dividends_earned = current_shares * dividend_amount
additional_shares = dividends_earned / dividend_price
current_shares += additional_shares
# Calculate the final value of the investment with reinvested dividends
final_value = current_shares * prices[-1][1]
total_return = final_value - initial_investment
return total_return
# Example data
prices = [
("2021-01-01", 100),
("2021-02-01", 105),
("2021-03-01", 110),
# ... add more price data points
]
dividends = [
("2021-01-15", 1),
("2021-02-15", 1),
# ... add more dividend data points
]
initial_investment = 10000 # Example initial investment amount
# Calculate return with reinvested dividends
total_return = reinvested_dividends(prices, dividends, initial_investment)
print(f"Total return with dividends reinvested: {total_return}")
Notes:
- Ensure your
prices and dividends lists are both sorted by date before you pass them to the function.
- This example assumes that you reinvest the dividends on the day they're paid out and that you can buy fractional shares with the dividends. In practice, there might be a delay, and you might only be able to buy whole shares.
- You can adjust the date handling if your data has different date formats or if dividends are reinvested at a different time or in a different process.
- This example does not account for tax considerations, transaction costs, or other potential factors influencing the return.