Get startedGet started for free

The time series of asset returns

Calculating the returns for one period is pretty straightforward to do in R. When the returns need to be calculated for different dates the functions Return.calculate() and Return.portfolio(), in the R package PerformanceAnalytics are extremely helpful. They require the input data to be of the xts-time series class, which is pre-loaded. You will explore the functionality of the PerformanceAnalytics package in this exercise.

The object prices which contains the Apple (aapl) and Microsoft (msft) stocks is available in the workspace.

This exercise is part of the course

Introduction to Portfolio Analysis in R

View Course

Exercise instructions

  • Load the package PerformanceAnalytics in your R session.
  • Have a look at the first and last six rows of prices, using head() and tail() respectively.
  • Use the function Return.calculate() with the only argument prices to compute for each date the return as the percentage change in the price compared to the previous date, call this returns.
  • Print the first six rows of returns.
  • Remove the first row of returns.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load package PerformanceAnalytics 


# Print the first six rows and last six rows of prices
 


# Create the variable returns using Return.calculate()  
 
  
# Print the first six rows of returns. Note that the first observation is NA because there is no prior price.

  
# Remove the first row of returns
returns <- returns[-1, ]
Edit and Run Code