• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Calculating Stock Returns and Portfolio Returns in R

Data Science, Portfolio Management

Portfolio Analysis in R 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

Previous Lesson
Back to Course
Next Lesson

Primary Sidebar

In this Course

Course Home
R Financial Packages for Portfolio Analysis
Downloading Stock Data in R Using QuantMod
Calculating Stock Returns and Portfolio Returns in R
Modern Portfolio Theory
Portfolio Optimisation in R
Return to Portfolio Analysis in R

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us