# Examples

### Introduction to PortfolioAnalytics

This is a guest post by Ross Bennett. Ross is currently enrolled in the University of Washington Master of Science in Computational Finance & Risk Management program with an expected graduation date of December 2014. He worked on the PortfolioAnalytics package as part of the Google Summer of Code 2013 project and continues to work on the package as a Research Assistant at the University of Washington. His work on the package focused on implementing a portfolio specification to separate and modularize assets, constraints, and objectives.

### Tactical asset allocation using quantstrat

As promised in the introduction to quantstrat, here is an example strategy. I thought I’d start with the obligatory tactical asset allocation (TAA) strategy. This post will replicate the strategy in the post, tactical asset allocation using blotter. The “faber” demo in the quanstrat package contains a TAA strategy but it uses a slightly different approach than the code we’re trying to replicate. There are two major differences: The blotter TAA code initiates a position at the first observation where the close is above the SMA.

### Timely Portfolio: LSPM Examples

Timely Portfolio has been doing some interesting work with Ralph Vince’s Leverage Space Model via the LSPM R package. Here’s a short list of his most recent LSPM-related posts: The Leverage Space Trading Model Bond Market as a Casino Game Part 1 Bond Market as a Casino Game Part 2 Slightly Different Use of Ralph Vince’s Leverage Space Trading Model Another Use of LSPM in Tactical Portfolio Allocation I encourage those of you who are interested in LSPM and/or R to check out his blog.

### How to backtest a strategy in R

This is the third post in the Backtesting in Excel and R series and it will show how to backtest a simple strategy in R. It will follow the 4 steps Damian outlined in his post on how to backtest a simple strategy in Excel. Step 1: Get the data The getSymbols function in quantmod makes this step easy if you can use daily data from Yahoo Finance. There are also “methods” (not in the strict sense) to pull data from other sources (FRED, Google, Oanda, R save files, databases, etc.

### How to backtest a strategy in Excel

(This is a guest post by Damian from Skill Analytics and ETF Prophet) Let me start by saying that I’m not an expert in backtesting in Excel – there are a load of very smart bloggers out there that have, as I would say, “mad skillz” at working with Excel including (but not limited to) Michael Stokes over at marketsci.com, Jeff Pietch over at etfprophet.com and the folks (David and Corey) over at cssanalytics.

### Backtesting in Excel and R

This post is the introduction to a series that will illustrate how to backtest the same strategy in Excel and R. The impetus for this series started with this tweet by Jared Woodard at Condor Options. After Soren Macbeth introduced us, Jared suggested backtesting a simple DVI strategy in Excel and R. The three-post series will show you: Resources that make it easier to move from Excel to R How to test DVI in Excel How to test DVI in R Since I know next to nothing about testing strategies in Excel, I will be writing posts 1 and 3.

### Margin Constraints with LSPM

When optimizing leverage space portfolios, I frequently run into the issue of one or more f\$ ([Max Loss]/f) being less than the margin of its respective instrument. For example, assume the required margin for an instrument is \$500, f\$ is \$100, and \$100,000 in equity. The optimal amount to trade is 1,000 shares (\$100,000/\$100). However, that would require \$500,000 in equity, while you only have \$100,000. What do you do?

### LSPM Joint Probability Tables

I’ve received several requests for methods to create joint probability tables for use in LSPM’s portfolio optimization functions. Rather than continue to email this example to individuals who ask, I post it here in hopes they find it via a Google search. ;-) I’m certain there are more robust ways to estimate this table, but the code below is a start… # `x` is a matrix of market system returns # `n` is the number of bins to create for each system # `FUN` is the function to use to calculate the value for each bin # `.

### Maximum Probability of Profit

To continue with the LSPM examples, this post shows how to optimize a Leverage Space Portfolio for the maximum probability of profit. The data and example are again taken from The Leverage Space Trading Model by Ralph Vince. These optimizations take a very long time. 100 iterations on a 10-core Amazon EC2 cluster took 21 hours. Again, the results will not necessarily match the book because of differences between DEoptim and Ralph’s genetic algorithm and because there are multiple possible paths one can take through leverage space that will achieve similar results.

### LSPM with snow

My last post provided examples of how to use the LSPM package. Those who experimented with the code have probably found that constrained optimizations with horizon > 6 have long run-times (when calc.max >= horizon). This post will illustrate how the snow package can increase the speed of the probDrawdown() and probRuin() functions on computers with multiple cores. This yields nearly linear improvements in run-times relative to the number of cores.