by Jonathan Regenstein

Financial applications were an early driving force behind the adoption of the R language, but as data science becomes increasingly critical to banks, hedge funds, investment managers, data providers, exchanges, etc., R is becoming even more important to Finance. We are excited and inspired by what the future holds in the brave new world of data-driven financial institutions. In this first post of what we hope will be a regular series about Finance, R and RStudio, we present a classic use case: import stock data, build a portfolio, and calculate the Sharpe Ratio. We use the new RStudio IDE Notebooks tool to emphasize reproducibility.

By way of a brief roadmap, we’ll start with a function that grabs monthly stock returns and saves those monthly returns as an xts object in the global environment. With that function, we will create three xts objects of monthly returns, and merge those three xts objects into one object, before passing that merged object to dygraphs to peek at the individual stocks. Then, we move on to build a portfolio by selecting asset weights and calculating the portfolio monthly returns. Next, we will calculate the growth of a dollar invested in that portfolio (which is what matters to us) over time, and save the results to an xts object. Dygraphs will come in handy again for the portfolio visualizations. Finally, we will calculate the Sharpe Ratio.

Before we get to the fun stuff – the code itself! – I will note that these posts aim to be useful to R coders who either work in the financial industry or want to work in the financial industry. At times the posts will contain ideas or paradigms that are good practice in the institutional context but perhaps not directly relevant to an R user on the lookout for one-off coding tricks. That is a good segue into the structure and content of this Notebook, which could be fit comfortably into one setup chunk and one substantive code chunk, instead of my five chunks.

I chose to use five chunks to demonstrate how this document might fit into the workflow of a team of analysts with varying levels of R knowledge. Team workflow + reproducibility is going to be a recurrent theme in this series, as it is a recurrent theme at financial institutions using the RStudio toolchain.

Five chunks breaks this code into simple steps: build an import function, choose/visualize the stocks, choose portfolio weights/visualize returns, and calculate the Sharpe Ratio. From a team workflow perspective, this structure could be an introductory template for analysts that are just learning R, or it could serve as the starting point for advanced users who want to use the data import and return functions before they add complexity. For example, calculating the Sharpe Ratio is just the beginning of risk-to-volatility analysis, and most shops have their own proprietary algorithms/models to advance the analysis. Beginners or advanced users could start with a common Notebook template like this one, to standardize the data import and basic visualization workflow.

Now, onwards to the setup code chunk, where we load three packages: quantmod to download the data, PerformanceAnalytics to run portfolio calculations, and dygraphs to graph time series objects. We will also create a function to import stock data.

The monthly_stock_returns function above takes 2 parameters, a stock symbol and a year. Note that we could have included a third parameter called something like ‘period’ if we wanted the ability to grab periods other than monthly returns. For example, we can envision a desire to look at annual, weekly or daily returns. Here, I force monthly returns because I don’t want to allow different period options. It’s a choice driven by the purpose of this Notebook – which here is focused on monthly returns.

In the next chunk, we choose three stock tickers and a starting year argument for the monthly_stock_returns function. Then, we merge them into one xts object and graph their individual performances over time.

Nothing earth-shattering thus far: we have an xts object of three time series and have seen that one of them had weird behavior in April of 2014 (a Google stock split). We’ll ignore that behavior for this example and go on to constructing a portfolio, which means finding the monthly returns of a weighted combination of assets. Unsurprisingly, we start out by choosing those weights.

Now, instead of looking at monthly returns, let’s look at how $1 would have grown in this portfolio.

A dollar would have grown quite nicely in this portfolio – about 2.5x – fantastic. Now let’s look at the risk/reward of this portfolio by calculating the Sharpe Ratio. Briefly, the Sharpe Ratio is the mean of the excess monthly returns above the risk-free rate, divided by the standard deviation of the excess monthly returns above the risk-free rate. This is the formulation of the Sharpe Ratio as of 1994; if we wished to use the original formulation from 1966 the denominator would be the standard deviation of portfolio monthly returns. Learn more here.

In other words, the Sharpe Ratio measures excess returns per unit of volatility, where we take the standard deviation to represent portfolio volatility. The Sharpe Ratio was brought to us by Bill Sharpe – arguably the most important economist for modern investment management as the creator of the Sharpe Ratio, CAPM and Financial Engines, a forerunner of today’s robo-advisor movement.

In the code chunk below, we’ll calculate the Sharpe Ratio in two ways.

First, we’ll use the Return.excess function from PerformanceAnalytics to calculate a time series of monthly excess returns. Two arguments need to be supplied: the time series of returns and the risk-free rate. The function will return a time series of excess returns, and we’ll take the mean of that time series to get the numerator of the Sharpe Ratio. Then we’ll divide by the standard deviation of the that time series to get the Sharpe Ratio.

Our second method is a bit easier. We’ll use the SharpeRatio function in PerformanceAnalytics, for which we’ll supply two arguments: a time series of monthly returns and risk-free rate.

For both methods I use a risk-free rate of .03% as the approximate mean of the 1-month Treasury bill rate since 2010. I’ll cover a quick way to grab this and other data via Quandl in a future post.

Using the Return.excess function and then dividing by the standard deviation of excess returns, the Sharpe Ratio is `sharpe_ratio_manual[,1]`

= 0.2018.

Using the built in SharpeRatio function, the Sharpe Ratio is `sharpe_ratio[1,]`

= 0.2018.

Alright, we have built a portfolio and calculated the Sharpe Ratio – and also set up some nice reusable chunks for data import, portfolio construction and visualization. We haven’t done anything terribly complex but this can serve as a useful paradigm to any collaborators, including our future selves, who want to reproduce this work, learn from this work, or expand upon this work.

In the next post, we will create a Shiny app to let users enter their own stock tickers, starting year, portfolio weights and risk-free rate, and then calculate a portfolio Sharpe Ratio. See you next time!

RichNovember 10, 2016 at 9:30 amWell done!

Jonathan HillNovember 10, 2016 at 9:57 amThank you Jonathan. Great post! I really enjoyed this.

BHMNovember 11, 2016 at 4:08 amInteresting. It was new for me to set/assign the name of a variable in global env, instead of returning the value from the function.

May I request to extend the future posts to other domains such as Clickstream analytics, NLP, etc

And may be not the best forum to say, but the new version of R Studio is mind blowing. Great Work.

Thanks again.

@patternproject

burtNovember 11, 2016 at 10:50 amIf the GOOG split was a problem in the first graph, you could have used Ad(GOOG) which adjusts.

Jonathan RegensteinNovember 11, 2016 at 10:53 amThanks, Burt – absolutely a good way to take account of the split!

Lecturas para el fin de semana [2016S45] – Infinitos ContrastesNovember 13, 2016 at 1:01 am[…] Reproducible Finance with R: The Sharpe Ratio: primer post de una serie de artículos sobre análisis de datos en finanzas, utilizando R como lenguaje de programación. Uno de los principales focos de atención reside en la reproducibilidad del procedimiento seguido. […]

Reproducible Finance with R: A Sharpe Ratio Shiny App – RStudioNovember 18, 2016 at 12:00 pm[…] this previous post, we used an R Notebook to grab the monthly return data on three stocks, build a portfolio, […]

Rex MaceyNovember 21, 2016 at 9:39 amNice example. Burt’s comment about using adjusted prices is important. Ignoring splits will cause stocks with splits to have dramatically higher standard deviations and thus lower Sharpe Ratios. By the way, what are the differences (and pros and cons) of using a “R Notebook” v “R Markdown” file?

Jonathan RegensteinNovember 21, 2016 at 12:12 pmHi Rex, thanks for the comment and you are absolutely correct about ignoring stock splits – we normally would not want to do that! As to your question, an R Notebook is an output of an Rmd file. Have a look here: http://rmarkdown.rstudio.com/r_notebooks.html. I prefer the Notebook because it is easier to view the chunk outputs and share the html output.