Calculating Stock Returns and Portfolio Returns in R
To calculate the returns of AAPL & GOOG over the time period, you can use the Return.calculate
function.
stockReturns <- Return.calculate(adjustedPrices)
head(stockReturns)
## AAPL.Adjusted GOOG.Adjusted
## 2017-01-03 NA NA
## 2017-01-04 -0.001119178 0.0009667604
## 2017-01-05 0.005085153 0.0090481583
## 2017-01-06 0.011148442 0.0152766979
## 2017-01-09 0.009159465 0.0006202319
## 2017-01-10 0.001008411 -0.0023058897
This will then calculate the daily returns of AAPL and GOOG over the time period. The first day, as there is nothing to divide it by, will be NA. It generally makes sense to use this code instead to diverge from that problem.
stockReturns <- Return.calculate(adjustedPrices)[-1]
head(stockReturns)
## AAPL.Adjusted GOOG.Adjusted
## 2017-01-04 -0.001119178 0.0009667604
## 2017-01-05 0.005085153 0.0090481583
## 2017-01-06 0.011148442 0.0152766979
## 2017-01-09 0.009159465 0.0006202319
## 2017-01-10 0.001008411 -0.0023058897
## 2017-01-11 0.005373393 0.0038767816
If one wants to calculate the returns of a portfolio, one can use the Return.portfolio
function with the returns of the stocks as one argument and the weights of the stocks as another. For this example, it will be an equal weight between AAPL and GOOG.
portReturns <- Return.portfolio(stockReturns, c(0.5, 0.5))
head(portReturns)
## portfolio.returns
## 2017-01-04 -7.620906e-05
## 2017-01-05 7.068722e-03
## 2017-01-06 1.321878e-02
## 2017-01-09 4.868296e-03
## 2017-01-10 -6.500634e-04
## 2017-01-11 4.625730e-03
This does not rebalance the portfolio. To do this, just add the rebalance_on
argument. In this case, it is going to be done monthly.
portReturnsRebalanced <- Return.portfolio(stockReturns, c(0.5, 0.5), rebalance_on = "months")
head(portReturnsRebalanced)
## portfolio.returns
## 2017-01-04 -7.620906e-05
## 2017-01-05 7.068722e-03
## 2017-01-06 1.321878e-02
## 2017-01-09 4.868296e-03
## 2017-01-10 -6.500634e-04
## 2017-01-11 4.625730e-03
You can also get an annual table of performance using the table.AnnualizedReturns
function. The arguments are an xts object of portfolio returns (can be multiple) and the risk-free rate, used to calculate the Sharpe ratio. In this example, the non-rebalanced portfolio will be compared to the monthly rebalanced portfolio.
allPortReturns <- cbind(portReturns, portReturnsRebalanced)
colnames(allPortReturns) <- c("Non-Rebalanced", "Monthly Rebalanced")
table.AnnualizedReturns(allPortReturns, Rf = 0.1/252)
## Non-Rebalanced Monthly Rebalanced
## Annualized Return 0.1647 0.1769
## Annualized Std Dev 0.2114 0.2104
## Annualized Sharpe (Rf=10%) 0.2549 0.3085
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $39 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.