In this quick tutorial I will introduce the PerformanceAnalytics library, the library lets us easily analyse the performance of our strategies. In this tutorial you will learn how to plot cumulative returns and draw downs compared to an index, output a table of monthly performance metrics, use boxplots to investigate strategy outliers and finally plot histograms of returns and overlay with different statistical measures.

You will need some returns data for this tutorial, I have created a file with some returns in to get you started: strategyperfomance.csv

The three image outputs are:

The text output is:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Strategy1 Strategy2 Index 2005 1.8 0.0 -0.3 1.9 1.4 1.4 0.7 1.3 0.7 0.0 1.2 1.2 12.0 9.2 9.6 2006 0.1 0.9 0.9 1.4 1.0 1.8 -1.7 0.4 1.0 0.2 0.1 0.5 6.9 4.0 6.3 2007 1.5 0.5 0.4 -0.9 1.3 1.8 -2.9 -0.3 -0.2 -1.1 -1.3 0.5 -0.9 -4.5 -3.7 2008 1.8 -1.0 3.5 1.5 0.2 -2.4 -1.0 -1.3 1.3 2.1 -5.3 2.9 1.9 0.4 -0.9 2009 -2.0 -4.4 0.5 -0.5 2.4 3.1 2.0 -0.6 -1.9 1.6 2.2 1.7 3.8 -0.5 0.8 2010 1.4 1.1 -0.8 -3.0 -0.1 -3.3 2.6 4.1 1.4 0.2 2.8 1.5 8.0 8.3 8.6 2011 0.9 -0.2 0.7 1.0 -1.3 2.5 0.6 0.5 -0.5 -2.0 -0.5 -0.6 1.0 -1.2 -1.5

Onto the code:

^{?}View Code RSPLUS

install.packages("PerformanceAnalytics") #Install the PerformanceAnalytics library library("PerformanceAnalytics") #Load the PerformanceAnalytics library #Load in our strategy performance spreadsheet #Structure is: Date,Strategy1,Strategy2,Index #Each row contains the period returns (% terms) strategies <- read.zoo("strategyperfomance.csv", sep = ",", header = TRUE, format="%Y-%m-%d") head(strategies) #List all the column names to check data loaded in correctly #Can access colums with strategies["columnname"] colnames(strategies) #Lets see how all the strategies faired against the index charts.PerformanceSummary(strategies,main="Performance of all Strategies") #Lets see how just strategy one faired against the index charts.PerformanceSummary(strategies[,c("Strategy1","Index")],main="Performance of Strategy 1") #Lets calculate a table of montly returns by year and strategy table.CalendarReturns(strategies) #Lets make a boxplot of the returns chart.Boxplot(strategies) #Set the plotting area to a 2 by 2 grid layout(rbind(c(1,2),c(3,4))) #Plot various histograms with different overlays added chart.Histogram(strategies[,"Strategy1"], main = "Plain", methods = NULL) chart.Histogram(strategies[,"Strategy1"], main = "Density", breaks=40, methods = c("add.density", "add.normal")) chart.Histogram(strategies[,"Strategy1"], main = "Skew and Kurt", methods = c("add.centered", "add.rug")) chart.Histogram(strategies[,"Strategy1"], main = "Risk Measures", methods = c("add.risk")) #Note: The above histogram plots is taken from the example documentation #The documentation is excellent #http://cran.r-project.org/web/packages/PerformanceAnalytics/vignettes/PA-charts.pdf |

The performance analytics documentation (including more examples) can be found at:

http://cran.r-project.org/web/packages/PerformanceAnalytics/vignettes/PA-charts.pdf

Gekko; interesting post. I use R too for all my analysis.

Q: on the histogram of risk mesaure plot; what are you measuring there? i can’t see the text.

Hi Alpha, I liked your blog especially the data snooping article. What strategies are you hoping to develop?

In the image two labels have gotten overlapped so it is hard to read, the code that produced that plot is:

chart.Histogram(strategies[,”Strategy1″], main = “Risk Measures”, methods = c(“add.risk”))

The labels should say 95% ModVaR and 95% VaR (as seen on page 14 of PA-charts.pdf). I didn’t really explain what these measures mean in the tutorial (I might do a post on this in the future). VaR is basically the maximum amount you stand to loose over a period of time with a certain amount of confidence. So in the above image we could say that “with 95% confidence our maximum 1 day loss will be 2.5% or less”. See http://en.wikipedia.org/wiki/Value_at_risk for more info. It’s basically a plot of returns.