# how to calculate YTD return including the paid dividends

I am looking for a way to compute YTD return and I found this question (calculate YTD return / find first available datapoint of a year in python), however, it seems that it does not include the paid dividend in the year. How can I compute YTD return including the paid dividends?

My goal is to add this into my Python code for more accurate backtesting, so I would appreciate it if your answer considers Python implementation.

I've recently created a Python package called pyTAA as a side hobby to analyse low-frequency strategies. This is very much in alpha and can change at any point in time. To answer your question, I've added some tools to do precisely what you're asking. You can find the implementation here, all that is needed is yfinance really.

And a small code snippit to compare price vs total returns:

from pytaa.backtest.returns import get_historical_total_return
from pytaa.tools.data import get_historical_price_data

start, end = "2013-01-01", "2023-05-01"
assets = ["SPY", "AGG", "IEF", "GLD", "EEM"]
prices = get_historical_price_data(assets, start, end).loc[:, "Close"]
total_returns = get_historical_total_return(prices, "USD", "total")
price_returns = get_historical_total_return(prices, "USD", "price")

# plot price returns
cpr.plot(figsize=(8,5), title="Cumulative Price Returns")
print(cpr.tail(1).rank(axis=1, ascending=False))

# plot total returns
ctr.plot(figsize=(8,5), title="Cumulative Total Returns")
ctr.tail(1).rank(axis=1, ascending=False)

# show the last period cum returns
comp = pd.concat([
cpr.tail(1).rename({cpr.index[-1]: "Price Return"}),
ctr.tail(1).rename({ctr.index[-1]: "Total Return"})
])
comp.index.name = ""
comp.mul(100).round(2)


Assuming you aren't reinvesting the dividend in the stock, your YTD return would just be the price on date nplus the dividends received up to that date divided by the initial price:

ytd_return = (price_n + div_ytd) / price_0

• thank you for your response. I guess that my question was not so clear and I edited it to be more accurate. I want this for more accurate backtesting, Do you know any python library that gives div_ytd? Oct 5, 2021 at 21:40