Calculating returns on a price series is one of the most basic calculations in finance, but it can become a headache when we want to do aggregations for weeks, months, years, etc. In Python, the Pandas library makes this aggregation very easy to do, but if we don’t pay attention we could still make mistakes. Assuming that we want the return of the whole month, and we are not interested, for example, in the returns accumulated so far. These latter returns require that they be normalized to be comparable with the other returns. Show
Simple motivationGiven some prices on business days, you can get the trailing returns per month for the situation that we want to calculate. With this in mind, I’d like to describe how to avoid miscalculating monthly returns. NoteBefore you start, you may need some dependencies to do this: Let’s get to workWe’re going to work with the shares of “Banco do Brazil SA.” Our first step is to download yahoo finance data using pandas_datareader: Let’s see what the data looks like: If we plot the closing prices, we’ll see this: Now we’ll work with closing prices. We’re going to calculate the monthly returns, so we can do the following*: * At the end of this post you will find the auxiliary functions used in the code, such as “total_return” The problem of this approximation is that it leaves out one day in the calculation of each monthly return, as it only takes into account the prices that belong to the month in question and completely omits all other information. For the calculation to be correct, you must include the closing price on the day before the first day of the month, i. e. the last day of the previous month. We can see it with an example: if we select month 8 of 2017, and see the prices that have been used to calculate returns, we will see that the series starts on August 1st and ends on August 31st. We have left out July 31st!, this happens every month. It should also be mentioned that the last month (the current month) is not comparable with the rest of the months since it has not yet finished. A second approachOkay, as a second approach to incorporate the previous data, I could calculate the returns first, then group and calculate the total return with that series:
However, all that glitters is not gold; this approximation has a problem in the first value. We still don’t have the price before the one needed to make the calculation. The remaining months would be correctly calculated with the exception of the last return (current month), which is again not comparable with the rest. The third approachSo, what can we do? The most correct result would come from first decimating the price series by taking only the last working day of the month, then grouping by year and month, and with the resulting series calculate, finally, the returns. The good thing about this approximation is that it doesn’t return values for the first or last month with prices available, which can save us some calculation errors. ConclusionIt’s unfortunately very easy to make mistakes with these kinds of calculations. Pandas makes things much simpler, but sometimes can also be a double-edged sword. Nothing like a quick reading to avoid those potential mistakes. As we can see on the plot, we can underestimate or overestimate the returns obtained.
Appendix: useful functionsBonusI have prepared a notebook with the complete code so that it can be executed and played with: Check it out here!
related postsAll How earnings reports affect stocks?J. GonzálezAll Building a sector rotation strategy based on Fed’s interest rate policyGonzalo Sainz PonceAll ESG 2022: “greenwashing” or alpha source?jsanchezalmarazAll Doubling Down: Double Deep Q-networks for tradingAlfredo TimermansLabel {} [+] Name* Email Label {} [+] Name* Email 2 Comments Inline Feedbacks View all comments Ettore Errazuriz 4 years ago Why not Df.resample(“M”).last().pct_change(1) ? Reply Author mgreco 4 years ago Reply to Hello Ettore Errazuriz, sorry for the late reply. Yes, It seems to be a good point, although it seems to work well it fails in the same way as “approach 2”, at the end it gives us a return that shouldn’t be there. I’ve updated the notebook picking up your proposal, you can have a look at it here, and/or run it online here. Thank you very much for you question!! 😉 |