<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/" data-counturl="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/" data-count="horizontal" data-text="How To Compute Turnover With Return.Portfolio in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://quantstrattrader.wordpress.com/2016/05/11/how-to-compute-turnover-with-return-portfolio-in-r/"> R – QuantStrat TradeR</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post will demonstrate how to take into account turnover when dealing with returns-based data using PerformanceAnalytics and the Return.Portfolio function in R. It will demonstrate this on a basic strategy on the nine sector SPDRs.</p>
<p>So, first off, this is in response to a question posed by one Robert Wages on the R-SIG-Finance mailing list. While there are many individuals out there with a plethora of questions (many of which can be found to be demonstrated on this blog already), occasionally, there will be an industry veteran, a PhD statistics student from Stanford, or other very intelligent individual that will ask a question on a topic that I haven’t yet touched on this blog, which will prompt a post to demonstrate another technical aspect found in R. This is one of those times.</p>
<p>So, this demonstration will be about computing turnover in returns space using the PerformanceAnalytics package. Simply, outside of the PortfolioAnalytics package, PerformanceAnalytics with its Return.Portfolio function is the go-to R package for portfolio management simulations, as it can take a set of weights, a set of returns, and generate a set of portfolio returns for analysis with the rest of PerformanceAnalytics’s functions.</p>
<p>Again, the strategy is this: take the 9 three-letter sector SPDRs (since there are four-letter ETFs now), and at the end of every month, if the adjusted price is above its 200-day moving average, invest into it. Normalize across all invested sectors (that is, 1/9th if invested into all 9, 100% into 1 if only 1 invested into, 100% cash, denoted with a zero return vector, if no sectors are invested into). It’s a simple, toy strategy, as the strategy isn’t the point of the demonstration.</p>
<p>Here’s the basic setup code:</p>
<pre>
require(TTR)
require(PerformanceAnalytics)
require(quantmod)

symbols &lt;- c(&quot;XLF&quot;, &quot;XLK&quot;, &quot;XLU&quot;, &quot;XLE&quot;, &quot;XLP&quot;, &quot;XLF&quot;, &quot;XLB&quot;, &quot;XLV&quot;, &quot;XLY&quot;)
getSymbols(symbols, src='yahoo', from = '1990-01-01-01')
prices &lt;- list()
for(i in 1:length(symbols)) {
  tmp &lt;- Ad(get(symbols[[i]]))
  prices[[i]] &lt;- tmp
}
prices &lt;- do.call(cbind, prices)

# Our signal is a simple adjusted price over 200 day SMA
signal &lt;- prices > xts(apply(prices, 2, SMA, n = 200), order.by=index(prices))

# equal weight all assets with price above SMA200
returns &lt;- Return.calculate(prices)
weights &lt;- signal/(rowSums(signal)+1e-16)

# With Return.portfolio, need all weights to sum to 1
weights$zeroes &lt;- 1 - rowSums(weights)
returns$zeroes &lt;- 0

monthlyWeights &lt;- na.omit(weights[endpoints(weights, on = 'months'),])
weights &lt;- na.omit(weights)
returns &lt;- na.omit(returns)
</pre>
<p>So, get the SPDRs, put them together, compute their returns, generate the signal, and create the zero vector, since Return.Portfolio treats weights less than 1 as a withdrawal, and weights above 1 as the addition of more capital (big FYI here).</p>
<p>Now, here’s how to compute turnover:</p>
<pre>
out &lt;- Return.portfolio(R = returns, weights = monthlyWeights, verbose = TRUE)
beginWeights &lt;- out$BOP.Weight
endWeights &lt;- out$EOP.Weight
txns &lt;- beginWeights - lag(endWeights)
monthlyTO &lt;- xts(rowSums(abs(txns[,1:9])), order.by=index(txns))
plot(monthlyTO)
</pre>
<p>So, the trick is this: when you call Return.portfolio, use the verbose = TRUE option. This creates several objects, among them returns, BOP.Weight, and EOP.Weight. These stand for Beginning Of Period Weight, and End Of Period Weight.</p>
<p>The way that turnover is computed is simply the difference between how the day’s return moves the allocated portfolio from its previous ending point to where that portfolio actually stands at the beginning of next period. That is, the end of period weight is the beginning of period drift after taking into account the day’s drift/return for that asset. The new beginning of period weight is the end of period weight plus any transacting that would have been done. Thus, in order to find the actual transactions (or turnover), one subtracts the previous end of period weight from the beginning of period weight.</p>
<p>This is what such transactions look like for this strategy.</p>
<p><img src="https://i2.wp.com/imgur.com/Z1Docgf.png"></p>
<p>Something we can do with such data is take a one-year rolling turnover, accomplished with the following code:</p>
<pre>
yearlyTO &lt;- runSum(monthlyTO, 252)
plot(yearlyTO, main = &quot;running one year turnover&quot;)
</pre>
<p>It looks like this:</p>
<p><img src="https://i0.wp.com/imgur.com/h2DVRbZ.png"></p>
<p>This essentially means that one year’s worth of two-way turnover (that is, if selling an entirely invested portfolio is 100% turnover, and buying an entirely new set of assets is another 100%, then two-way turnover is 200%) is around 800% at maximum. That may be pretty high for some people.</p>
<p>Now, here’s the application when you penalize transaction costs at 20 basis points per percentage point traded (that is, it costs 20 cents to transact $100).</p>
<pre>
txnCosts &lt;- monthlyTO * -.0020
retsWithTxnCosts &lt;- out$returns + txnCosts
compare &lt;- na.omit(cbind(out$returns, retsWithTxnCosts))
colnames(compare) &lt;- c(&quot;NoTxnCosts&quot;, &quot;TxnCosts20BPs&quot;)
charts.PerformanceSummary(compare)
table.AnnualizedReturns(compare)
</pre>
<p>And the result:</p>
<p><img src="https://i1.wp.com/imgur.com/HuKlqYh.png"></p>
<pre>

                          NoTxnCosts TxnCosts20BPs
Annualized Return             0.0587        0.0489
Annualized Std Dev            0.1554        0.1553
Annualized Sharpe (Rf=0%)     0.3781        0.3149
</pre>
<p>So, at 20 basis points on transaction costs, that takes about one percent in returns per year out of this (admittedly, terrible) strategy. This is far from negligible.</p>
<p>So, that is how you actually compute turnover and transaction costs. In this case, the transaction cost model was very simple. However, given that Return.portfolio returns transactions at the individual asset level, one could get as complex as they would like with modeling the transaction costs.</p>
<p>Thanks for reading.</p>
<p>NOTE: I will be giving a lightning talk at R/Finance, so for those attending, you’ll be able to find me there.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quantstrattrader.wordpress.com/470/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/quantstrattrader.wordpress.com/470/" border="0"></a> <img height="1" alt="" width="1" src="https://pixel.wp.com/b.gif?host=quantstrattrader.wordpress.com&blog=66155974&%23038;post=470&%23038;subd=quantstrattrader&%23038;ref=&%23038;feed=1" border="0"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/" data-counturl="http://www.r-bloggers.com/how-to-compute-turnover-with-return-portfolio-in-r/" data-count="vertical" data-text="How To Compute Turnover With Return.Portfolio in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://quantstrattrader.wordpress.com/2016/05/11/how-to-compute-turnover-with-return-portfolio-in-r/"> R – QuantStrat TradeR</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qW_NFutbWFE"></a></div>