The Average Investor's Blog

A software developer view on the markets

Summarizing Returns with R

Posted by The Average Investor on Aug 2, 2011

Often I like to see the performance of a trading strategy summarized annually, quarterly or by month. In R, we start off with the summary function:

aggregateReturns = function( xx, leverage=1 )
{
   return( tail( cumprod( 1 + xx*leverage ), 1 ) )
}

Given a series xx, usually a chunk of the original, this function returns the accumulative returns for the period. The leverage is useful to somewhat simulate leveraged ETFs.

The rest is to call this function for various periods:

summarizeDailyReturns = function(
      ss,
      indicator,
      returns="closeToClose",
      period="annually",
      leverage=1 )
{
   stopifnot( length( index( ss ) ) == length( index( indicator ) ) )
   stopifnot( is.xts( ss ) )
   stopifnot( is.xts( indicator ) )

   if( tolower( returns ) == "opentoclose" )
   {
      stopifnot( has.Op( ss ) && has.Cl( ss ) )
      rets = Cl( ss ) / Op( ss ) - 1
   }
   else 
   {
      stopifnot( has.Ad( ss ) || has.Cl( ss ) )
      if( has.Ad( ss ) )
      {    
         rets = Ad( ss ) / lag( Ad( ss ) ) - 1
      }    
      else 
      {    
         rets = Cl( ss ) / lag( Cl( ss ) ) - 1
      }    
   }

   rets[as.character(head( index( ss ), 1 ))] = 0
   rets = as.xts( indicator * coredata( rets ) )

   if( tolower( period ) == "annually" )
   {
      yy = as.numeric( format( index( rets ), "%Y" ) )
      rets = aggregate( rets, yy, aggregateReturns, leverage )
   }
   else if( tolower( period ) == "quarterly" )
   {
      rets = aggregate( rets, as.yearqtr, aggregateReturns, leverage )
   }
   else if( tolower( period ) == "monthly" )
   {
      rets = aggregate( rets, as.yearmon, aggregateReturns, leverage )
   }

   return( round( rets, 4 ) )
}

I will finish the post with an illustration how to use this function (assuming the two functions reside in returns.R from the current directory):

library(quantmod)

source("returns.R")

getSymbols("^GSPC", from="1900-01-01")

# The indicator - buy and hold: 1 every day
ind = xts(rep(1, length(index(GSPC))), order.by=index(GSPC))

summarizeDailyReturns(GSPC, ind, returns="OpenToClose")
summarizeDailyReturns(GSPC, ind, leverage=2, period="quarterly")
summarizeDailyReturns(GSPC, ind, leverage=2, period="daily")

The last call computes the compound growth on a daily basis. :)

The final version of the function is available from the quntscript project.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: