ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.73k stars 827 forks source link

Sharpe ratio formula should use compound returns #267

Closed boylez closed 1 year ago

boylez commented 1 year ago

I wanted to provide some feedback on the sharpe() function in QuantStats. Currently, it calculates the Sharpe ratio by simply dividing the mean return by the standard deviation. This approach does not account for the compounding of returns over time and will understate the total return, resulting in an incorrect Sharpe ratio. The proper way to calculate the Sharpe ratio is to use the compound annual growth rate (CAGR) formula: Sharpe Ratio = (CAGR - Risk Free Rate) / Annualized Standard Deviation Where: CAGR = (End Value / Start Value)^(1/n) - 1 n = Number of periods Annualized Standard Deviation = Periodic Std Dev sqrt(n) For example: Mean monthly return: 0.5% Monthly standard deviation: 3% Over 12 months: Incorrect (mean return): Sharpe Ratio = 0.5% / 3% = 0.16 Correct (uses CAGR): CAGR = (1.005 ^ 12) - 1 = 0.629 = 62.9% Annualized Std Dev = 3% sqrt(12) = 9.49% Sharpe Ratio = (62.9% - 6%) / 9.49% = 5.16 The CAGR approach properly compounds the returns over the full year, whereas the mean return approach does not and significantly understates the return (and hence the Sharpe ratio). I would suggest updating the sharpe() function to use the CAGR formula to calculate the total return relative to risk. This would make the QuantStats library more accurate and robust. Please let me know if I can provide any other details or suggestions on improving the library. Thank you for developing this useful library! I hope this feedback is helpful. Please let me know if you have any questions or want any clarification. I'm happy to discuss further to strengthen the QuantStats library.

ranaroussi commented 1 year ago

In order to account for compounded returns, you should use:

comp_returns = qs.stats.compsum(returns)
qs.stats.sharpe(comp_returns)