Thursday, December 15, 2011

R/Finance 2012 Call for Papers

I'm excited to share the call for papers for the upcoming R/Finance conference.  Even if you don't submit a presentation, I hope to see you there!

Call for Papers:

R/Finance 2012: Applied Finance with R
May 11 and 12, 2012
University of Illinois, Chicago, IL, USA

The fourth annual R/Finance conference for applied finance using R will be held on May 11 and 12, 2012 in Chicago, IL, USA on the campus of the University of Illinois at Chicago. The two-day conference will cover topics including portfolio management, time series analysis, advanced risk tools, high-performance computing, market microstructure, and econometrics. All will be discussed within the context of using R as a primary tool for financial risk management, portfolio construction, and trading.

Over the past three years, R/Finance has included attendees from around the world and featured keynote presentations from prominent academics and practitioners. We anticipate another exciting line-up for 2012 -- including keynote presentations from Blair Hull, Paul Gilbert, Rob McCulloch, and Simon Urbanek.

We invite you to submit complete papers or one-page abstracts (in txt or pdf format) for consideration. Academic and practitioner proposals related to R are encouraged. We welcome submissions for full talks, abbreviated "lightning talks", and for a limited number of (longer) pre-conference seminar sessions.

Presenters are strongly encouraged to provide working R code to accompany the presentation/paper. Data sets should also be made public for the purposes of reproducibility (though we realize this may be limited due to contracts with data vendors). Preference may be given to presenters who have released R packages.

Travel and accommodation grants may be available for selected presenters at the discretion of the committee. In addition, the conference will award prizes for best papers. To be eligible for a best paper award, a submission must be a full paper. Extended abstracts, even if a full paper by conference time, are not eligible for a best paper award.

Please send submissions to: committee at

The submission deadline is January 31, 2012. Submitters will be notified of acceptance via email by February 28, 2012. Notification of whether a presentation will be a long presentation or a lightning talk will also be made at that time.

Additional details will be announced at this website as they become available. Information on previous year's presenters and their presentations are also at the conference website R/Finance 2009, 2010, and 2011.

For the program committee:
    Gib Bassett, Peter Carl, Dirk Eddelbuettel, Brian Peterson,
    Dale Rosenthal, Jeffrey Ryan, Joshua Ulrich

Tuesday, September 27, 2011

Denver 10/1-10/5

I will be traveling to Denver from 10/1-10/5.  Drop me a line if you're in the area and would like to meet for coffee / drinks.

Thursday, September 1, 2011

TTR_0.21-0 on CRAN

An updated version of TTR is now on CRAN.  It contains some much-needed bug fixes (most notably to stockSymbols), some small changes, and a few new functions.  Note that the change to wilderSum will affect functions that use it (e.g. ADX).

Here are the full contents of the CHANGES file:

TTR version 0.21-0
  Changes from version 0.20-2

  • Added wilder and ratio arguments to DEMA. Thanks to Matthew Fornari for the suggestion.
  • Changed wilderSum to seed initial value with raw sum. This matches Wilder's original calculations. Thanks to Mahesh Bp for the report.
  • The BBands sd calculation now uses the population instead of sample statistic. This is consistent with Bollinger Band literature. Thanks to Jeff Ryan for the patch.
  • Fixed stockSymbols for changes.
  • Fixed ZLEMA default ratio by changing it from 2/(n-1) to 2/(n+1). This makes it consistent with EMA. Thanks to Dirk Eddelbuettel.
  • Corrected close-to-close volatility. Thanks to James Toll for the report.
  • adjRatios failed (spectacularly) if there were missing close prices. Thanks to Garrett See for the report.

Tuesday, August 23, 2011

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 tactial 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:
  1. The blotter TAA code initiates a position at the first observation where the close is above the SMA.  The demo only initiates a position when the close crosses the SMA to the upside.

    For example, assume the close is above the SMA at the beginning of the sample.  The demo has to wait for the close to drop below the SMA and then cross above it before taking a position; the blotter TAA code initiates a position on the first observation.

  2. The blotter TAA code calculates order size based on total account equity (as stored in the UnitSize object).  The demo always uses an order size of 1,000 shares, regardless of total account value (in case you're wondering, yes, capital/equity-aware order sizing is on the to-do list).
We need to make a few changes to the demo(faber) code to make it work more like the blotter TAA post.  First, we change the add.signal calls to use sigCrossover instead of sigComparison.  This allows us to create an order on the first observation, rather than wait for a crossover.

Next, we need to change the first call to add.rule (the entry rule).  sigComparison will be TRUE for every period where the close is above the SMA and we don't want to buy 1000 shares every period, so we need to tell ruleSignal to use the max position order sizing function.  We do this by adding osFUN=osMaxPos to the list of arguments passed to ruleSignal.

Finally, we need to set the position limits for each instrument.  We do this via two calls to addPosLimit (one for each symbol) and we set the maximum position to 1000 shares and the minimum position to 0 shares.

The modified code is below and even includes some simple evaluation of the results at no extra charge.  The tradeStats function has a ton more columns; I've only selected a few to make the output more readable.  Feel free to tinker.

NOTE: I wrote this code using the latest quantmod, xts, and zoo from CRAN; and the latest blotter, FinancialInstrument, and quantstrat from R-Forge.

# This code is a slight modification of the quantstrat "faber" demo, intended to replicate 
# Uncomment the line below to install the latest packages on R-Forge:
#install.packages(c("quantstrat","blotter","FinancialInstrument"), repos="")

# Set initial values
initDate <- "2002-07-31"
endDate <- "2009-10-31"
initEq <- 100000

# Pull Yahoo Finance data
symbols <- c("IEF", "SPY")
getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct"))

# adjust for splits/dividends (comment to replicate blotter example)
#IEF <- adjustOHLC(IEF, use.Adjusted=TRUE)
#SPY <- adjustOHLC(SPY, use.Adjusted=TRUE)

# convert to monthly
IEF <- to.monthly(IEF, indexAt="endof")
SPY <- to.monthly(SPY, indexAt="endof")

# Set up instruments with FinancialInstruments package
for(symbol in symbols) {
  stock(symbol, currency="USD", multiplier=1)

# Delete portfolio, account, and order book if they already exist

# Initialize portfolio and account
initPortf("faber", symbols=symbols, initDate=initDate)
initAcct("faber", portfolios="faber", initDate=initDate, initEq=initEq)
initOrders(portfolio="faber", initDate=initDate)

# Initialize a strategy object
stratFaber <- strategy("faber")

# Add the 10-month SMA indicator
stratFaber <- add.indicator(strategy=stratFaber, name="SMA",
  arguments=list(x=quote(Cl(mktdata)), n=10), label="SMA10")

# There are two signals:
# The first is when monthly price crosses over the 10-month SMA
stratFaber <- add.signal(stratFaber, name="sigComparison",
  arguments=list(columns=c("Close","SMA10"),relationship="gte"), label="")
# The second is when the monthly price crosses under the 10-month SMA
stratFaber <- add.signal(stratFaber, name="sigComparison",
  arguments=list(columns=c("Close","SMA10"),relationship="lt"), label="")

# There are two rules:
# The first is to buy when the price crosses above the SMA
stratFaber <- add.rule(stratFaber, name="ruleSignal",
  arguments=list(sigcol="", sigval=TRUE, orderqty=1000, ordertype="market",
  orderside="long", pricemethod="market", TxnFees=-5, osFUN=osMaxPos), type="enter", path.dep=TRUE)
# The second is to sell when the price crosses below the SMA
stratFaber <- add.rule(stratFaber, name="ruleSignal",
  arguments=list(sigcol="", sigval=TRUE, orderqty="all", ordertype="market",
  orderside="long", pricemethod="market", TxnFees=-5), type="exit", path.dep=TRUE)

# Set position limits so we don't add to the position every month Close > SMA10
addPosLimit("faber", "SPY", timestamp=initDate, maxpos=1000, minpos=0)
addPosLimit("faber", "IEF", timestamp=initDate, maxpos=1000, minpos=0)

# Process the indicators and generate trades
out <- try(applyStrategy(strategy=stratFaber, portfolios="faber"))

# Evaluate results
portRet <- PortfReturns("faber")
portRet$Total <- rowSums(portRet, na.rm=TRUE)

Friday, August 12, 2011

Introduction to quantstrat

quantstrat provides a generic infrastructure to model and backtest signal-based quantitative strategies.  It is a high-level abstraction layer (built on xts, FinancialInstrument, blotter, etc.) that allows you to build and test strategies in very few lines of code.  quantstrat is still under heavy development but is being used every day on real portfolios.  We encourage you to send contributions and test cases to the project forums.

This post is a joint effort between me and Brian Peterson.  It will describe the underlying philosophy of quantstrat and how quantstrat implements that philosophy.  You may have seen some of this in Brian's Quantitative Strategy Development in R lightning talk at R/Finance 2011.

Generic Signal-Based Strategy Modeling

A signal-based strategy model first generates indicators.  Indicators are quantitative values derived from market data (e.g. moving averages, RSI, volatility bands, channels, momentum, etc.).  Indicators should be applied to market data in a vectorized (for fast backtesting) or streaming (for live execution) fashion, and are assumed to be path-independent (i.e. they do not depend on account / portfolio characteristics, current positions, or trades).

The interaction between indicators and market data are used to generate signals (e.g. crossovers, thresholds, multiples, etc.).  These signals are points in time at which you may want to take some action, even though you may not be able to.  Like indicators, signals may be applied in a vectorized or streaming fashion, and are assumed to be path-independent.

Rules use market data, indicators, signals, and current account / portfolio characteristics to generate orders.  Notice that rules about position sizing, fill simulation, order generation / management, etc. are separate from the indicator and signal generation process.  Unlike indicators and signals, rules are generally evaluated in a path-dependent fashion (path-independent rules are supported but are rare in real life) and are aware of all prior market data and current positions at the time of evaluation.  Rules may either generate new or modify existing orders (e.g. risk management, fill, rebalance, entry, exit).

How quantstrat Models Strategies

quantstrat uses FinancialInstrument to specify instruments (including their currencies) and uses blotter to keep track of transactions, valuations, and P&amp;amp;L across portfolios and accounts.

Indicators are often standard technical analysis functions like those found in TTR; and signals are often specified by the quantstrat sig* functions (i.e. sigComparison, sigCrossover, sigFormula, sigPeak, sigThreshold).  Rules are typically specified with the quantstrat ruleSignal function.

The functions used to specify indicators, signals, and rules are not limited to those mentioned previously.  The name parameter to add.indicator, add.signal, and add.rule can be any R function.  Because the supporting toolchain is built using xts objects, custom functions will integrate most easily if they return xts objects.

The strategy model is created in layers and makes use of delayed execution.  This means strategies can be applied--unmodified--to several different portfolios.  Before execution, quantstrat strategy objects do not know what instruments they will be applied to or what parameters will be passed to them.

For example, indicator parameters such as moving average periods or thresholds are likely to affect strategy performance.  Default values for parameters may (optionally) be set in the strategy object, or set at call-time via the parameters argument of applyStrategy (parameters is a named list, used like the arguments lists).

quantstrat models orders, which may or may not become transactions.  This provides a lot of extra ability to evaluate how the strategy is actually working, not working, or could be improved.  For example, performance strategies are often affected by how often resting limit orders are changed / replaced / canceled.  An order book allows the quantitative strategist to examine market conditions at the time these decisions are made. Also, the order history allows for easy computation of things that are important for many strategies, like order-to-fill ratios.

What's next?
  • Examples!  You can run some demos while you wait:
  • Strategy Evaluation
  • Parameter Evaluation

Wednesday, July 27, 2011

Creating Financial Instrument metadata in R

(This is a guest post by Ilya Kipnis)

When trading stocks in a single currency, instrument metadata can be safely ignored because the multiplier is 1 and the currencies are all the same.  When doing analysis on fixed income products, options, futures, or other complex derivative instruments, the data defining the properties of these instruments becomes critical to tasks like accounting for value of trades, or comparing notional value between more than one instrument. The FinancialInstrument package provides a construct for storing metadata for tradeable contracts (referred to as instruments, e.g. stocks, futures, options, etc.) and their root representations.  It can be used to create any asset class and complex derivatives, across multiple currencies. 

In tactical asset allocation using blotter, Joshua Ulrich used FinancialInstrument (blotter depends on it) to create a stock portfolio.  FinancialInstrument is also a required dependency of the quantstrat quantitative strategy framework in R (quantstrat will be covered in a later post).

Creating a list of historical symbols is a recurring challenge with historical data on derivative instruments.  These symbols tend to follow a deterministic pattern and FinancialInstrument provides utility functions to create the symbols traded over specific periods of time. These symbols could then be used to request historical data from a data vendor or to construct instrument objects in R.

The simplest function for generating a series of symbols is build_series_symbols.  Let's look at it with a small example using crude oil (CL) and STOXX (STXE) futures:

    # install.packages("FinancialInstrument", repos="")
    Data <- data.frame(primary_id="CL", month_cycle="F,G,H,J,K,M,N,Q,U,V,X,Z")
    Data <- rbind(Data, data.frame(primary_id="STXE", month_cycle="H,M,U,Z"))
    #   primary_id               month_cycle
    # 1         CL   F,G,H,J,K,M,N,Q,U,V,X,Z
    # 2       STXE                   H,M,U,Z

The Data object contains two columns.  The primary_id is the root contract that identifies the instrument, and the month_cycle defines the months the contracts trade in (e.g. “H,M,U,Z” for Mar/Jun/Sep/Dec).

build_series_symbols only needs Data and yearlist.   yearlist is the suffix for expiration years and 0, 1, 2 represent 2010, 2011, 2012 in this example.  build_series_symbols returns a vector of series symbols that we could use to request data or create future_series instruments.

    build_series_symbols(Data, yearlist=c(0,1,2))
     [1] "CLF0"   "CLG0"   "CLH0"   "CLJ0"   "CLK0"   "CLM0"   "CLN0"   "CLQ0" 
     [9] "CLU0"   "CLV0"   "CLX0"   "CLZ0"   "STXEH0" "STXEM0" "STXEU0" "STXEZ0"
    [17] "CLF1"   "CLG1"   "CLH1"   "CLJ1"   "CLK1"   "CLM1"   "CLN1"   "CLQ1" 
    [25] "CLU1"   "CLV1"   "CLX1"   "CLZ1"   "STXEH1" "STXEM1" "STXEU1" "STXEZ1"
    [33] "CLF2"   "CLG2"   "CLH2"   "CLJ2"   "CLK2"   "CLM2"   "CLN2"   "CLQ2" 
    [41] "CLU2"   "CLV2"   "CLX2"   "CLZ2"   "STXEH2" "STXEM2" "STXEU2" "STXEZ2"

A more complicated task is to create symbols for exchange guaranteed calendar spreads.  The build_spread_symbols function creates a vector of symbols for spreads of securities (currently coded for futures calendar spreads but can be extended and generalized) in a very shorthand notation.

build_spread_symbols accepts instrument specifications via either a file path or (preferably) a data frame (using the file or data arguments, respectively). Output can be assigned to an object or written to a file using the optional outputfile argument.  The default starting date is the current date, but the user can manually set a starting date (e.g. historical dates for backtesting, and future dates to create lists of instruments to be traded in the future).

    # read in data that would be suitable for load.instruments on root contracts
    Data <- read.csv("series_data.csv", stringsAsFactors=FALSE)
    # set the type to guaranteed_spread
    Data$type <- "guaranteed_spread"
    # call build_spread_symbols
    output <- build_spread_symbols(Data[6:7,], start_date="2010-01-01")

The critical fields in the data (CSV or data frame) are the primary_id, the type (e.g. future, calendar spread, intercommodity spread, etc.), the month_cycle, and the active_months (how many contracts to display).  For instance, an active_months value of 12 on a contract that trades quarterly (“H,M,U,Z”) would create front month contracts for the next 3 years.  An active_months value of 6 on this same quarterly contract would produce 1.5 years.  On a contract that trades 12 months a year (“F,G,H,J,K,M,N,Q,U,V,X,Z”), an active_months value of 12 would produce 1 year of contracts, and a value of 6 would only produce half a year.

Lastly, the contracts_ahead field specifies the month spread on calendar spread type securities.  Note that these are contracts ahead and not months ahead.  For example, if you consider a contract trading “H,M,U,Z” vs. a contract trading “F,G,H,J,K,M,N,Q,U,V,X,Z”, a value of 1 for the “H,M,U,Z” contract would create an H1-M1 spread or the like, while a value of 1 with the 12-month traded contract would create an F1-G1 spread.

The rest of the columns simply get carried over, for further use in other programs/scripts/procedures, such as the load.instruments function.

About the Author: Ilya Kipnis holds a Master's degree in Statistics from Rutgers, and uses and contributes to the R packages blotter, FinancialInstrument, and quantstrat. Ilya may be contacted for consulting and full-time opportunities in finance at

Thursday, June 23, 2011

The R Journal, Volume 3/1

The most recent issue of The R Journal was recently published.  If you're not a regular reader, you should at least check out the following three contributed articles (listed in order of appearance).

Sunday, May 29, 2011

R/Finance 2011 Presentations are online

For those of you who don't subscribe to the R-SIG-Finance mailing list:
  1. You really should subscribe ;-)
  2. Dirk Eddelbuettel announced the R/Finance 2011 presentations are now available.
I've included the entire announcement (with some hyperlinks) below.

The organizing committee for the R/Finance 2011 conference is pleased to announce the availability of presentation slides from the 3rd annual R/Finance conference.  This year's two-day conference once again attracted over 200 participants from across the globe. Academics, students and industry professionals enjoyed almost 30 talks covering trading, optimization, risk management and more --- all using R!

The majority of these presentations are now available for download at:

This year we began offering prizes for the best paper submissions.  The 2011 recipients are Robert Gramacy (University of Chicago) and David Matteson (Cornell University) who each won USD 1000.  Also new was a graduate student travel award: Mikko Niemenmaa (Aalto University) and Clément Dunand-Châtellet (École Polytechnique) each received USD 500.

With this, the organizing committee would like to thank our lead conference sponsors, the International Center for Futures and Derivatives at UIC and Revolution Analytics, as well as our conference sponsors OneMarketData, RStudio and lemnica for their continued support.

The organising committee would also like to thank all of the presenters and participants for making R/Finance 2011 so successful.  We look forward to seeing you in 2012, with the prospective dates of May 17 - 19 to be confirmed.

For the organizing committee,  

    Gib Bassett, Peter Carl, Dirk Eddelbuettel, Brian Peterson,
    Dale Rosenthal, Jeffrey Ryan, Joshua Ulrich

Sunday, May 1, 2011

Tuesday, April 26, 2011

Leverage Space Indexes Announced


The Leverage Space Portfolio (LSP) strategy seeks to maximize the probability of equity portfolio profitability by employing a risk-control process focused on capital preservation and drawdown management. Compared to a traditional buy-and-hold portfolio, an LSP-based portfolio aims for more consistent returns with lower risk.

The indexes, scheduled to be launched in the second half of 2011, can serve as the basis of both passive and active investment funds, including exchange-traded funds, mutual funds, and institutional accounts, around the world.

"We believe the marketplace will welcome our new family of indexes that applies this unique portfolio risk-management theory," said Michael A. Petronella, President, Dow Jones Indexes.  "These innovative techniques aspire to change the paradigm of the professional investment management process while providing Dow Jones Indexes with an opportunity to expand our roster of risk-based indexes."

Dow Jones Indexes has fully automated all elements of the LSP strategy, allowing for universal, systematic, and transparent application of a rules-based strategy to equity portfolios of any size or composition.

"The association with Dow Jones Indexes is, to us, a commercial validation on the highest order of our portfolio management ideas," LSP Partners’ Mr. Vince said. "We're extremely proud to collaborate with the world’s foremost index provider."

Journalists may e-mail questions regarding this press release to or contact Dow Jones Indexes press office:
New York: +1-212-597-5720
London: +44-20-7796-7247

About Dow Jones Indexes
Dow Jones Indexes is a leading full-service index provider that develops, maintains and licenses indexes for use as benchmarks and as the basis of investment products. Best-known for the Dow Jones Industrial Average, Dow Jones Indexes offers more than 130,000 equity indexes as well as fixed-income and alternative indexes, including measures of hedge funds, commodities and real estate. Dow Jones Indexes employs clear, unbiased and systematic methodologies that are fully integrated within index families. Dow Jones Indexes is part of a joint venture company owned 90 percent by CME Group Inc. and 10 percent by Dow Jones & Company, Inc., a News Corporation company (NASDAQ: NWS, NWSA; ASX: NWS, NWSLV).

"Dow Jones®," "Dow Jones Indexes," and all other index names listed above are service marks of Dow Jones Trademark Holdings LLC ("Dow Jones"), and have been licensed for use by CME Group Index Services LLC ("CME Indexes").

About LSP Partners
Ralph Vince and Richard Wilkie formed LSP Partners to evolve the concepts introduced in Mr.  Vince’s series of books on portfolio management following a breakthrough discovery in portfolio position sizing algorithms. A recognized authority on position sizing in trading, Mr. Vince has worked with institutional asset management companies, sovereign wealth funds and private traders for more than 30 years. He has written numerous books and professional papers on money management for trading, and introduced new statistical techniques that are in widespread use throughout the industry today. Mr. Vince also conducts portfolio riskmanagement workshops for institutional portfolio managers. For more information about LSP Partners, please see

Monday, April 4, 2011

RQuantLib Windows binary on CRAN

Dirk Eddelbuettel has recently released RQuantLib-0.3.7, which contains the necessary QuantLib builds to allow the CRAN servers to build the Windows binary.

This (thankfully) makes my post on how to build RQuantLib on 32-bit Windows unnecessary for casual users, but may be useful for those who want to develop RQuantLib on Windows.

Saturday, March 26, 2011

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.).  You could also use them as a template to write a custom function for a particular vendor you use.

# run the command below if quantmod isn't already installed
# install.packages("quantmod")
# use the quantmod package (loads TTR, xts, and zoo)
# pull SPX data from Yahoo (getSymbols returns an xts object)

Step 2: Create your indicator
The TTR package contains a multitude of indicators.  The indicators are written to make it easy to combine them in creative and unconventional ways.  Starting with revision 106 on R-forge, TTR has a DVI indicator.

# calculate DVI indicator
dvi <- DVI(Cl(GSPC))  # Cl() extracts the close price column

Step 3: Construct your trading rule
Since this trading rule is simple--we're long 100% if the DVI is below 0.5 and short 100% otherwise--it can be written in a single line.  More elaborate rules and/or position sizings can be done as well, but require more code (RSI(2) with Position Sizing is an example of more complex position sizing rules).  Also notice that the signal vector is lagged, which avoids look-ahead bias.

# create signal: (long (short) if DVI is below (above) 0.5)
# lag so yesterday's signal is applied to today's returns
sig <- Lag(ifelse(dvi$dvi < 0.5, 1, -1))

Step 4: The trading rules/equity curve
As in Damian's example, the code below is a simplified approach that is frictionless and does not account for slippage.  The code below takes today's percentage return and multiplies it by yesterday's signal / position size (always +/- 100% in this example).  I also subset the system returns to match the results in the Excel file.

# calculate signal-based returns
ret <- ROC(Cl(GSPC))*sig
# subset returns to match data in Excel file
ret <- ret['2009-06-02/2010-09-07']

Step 5: Evaluate strategy performance
Damian mentioned the importance of evaluating your strategy.  Fortunately for R users, the PerformanceAnalytics package makes this easy.  With a few lines of code we can view the drawdowns, downside risks, and a performance summary.

# use the PerformanceAnalytics package
# install.packages("PerformanceAnalytics")
# create table showing drawdown statistics
table.Drawdowns(ret, top=10)
# create table of downside risk estimates
# chart equity curve, daily performance, and drawdowns

That's all there is to backtesting a simple strategy in R.  It wasn't that intimidating, was it?  Please leave feedback if you're moving your backtesting from Excel to R and there's something you're hung up on or you have an awesome tip you'd like to share.

Here's a succinct version of the code in the above post if you want to be able to copy / paste it all in one block:


# Step 1: Get the data

# Step 2: Create your indicator
dvi <- DVI(Cl(GSPC))

# Step 3: Construct your trading rule
sig <- Lag(ifelse(dvi$dvi < 0.5, 1, -1))

# Step 4: The trading rules/equity curve
ret <- ROC(Cl(GSPC))*sig
ret <- ret['2009-06-02/2010-09-07']
eq <- exp(cumsum(ret))

# Step 5: Evaluate strategy performance
table.Drawdowns(ret, top=10)

Thursday, March 17, 2011

Risk-Opportunity Analysis: Houston

I will be attending Ralph Vince's risk-opportunity analysis workshop in Houston this weekend.  I'll be in town Friday-Monday.  Drop me a note if you're in the area and would like to meet for coffee / drinks.

Monday, March 14, 2011

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, Jeff Pietch over at and the folks (David and Corey) over at All of these guys have been gracious enough, over the years, to share with me how to do backtests – so I am indebted to them. And I want to thank Josh here at FOSS Trading as well – because he’s been kind enough to help me in learning how to use R for testing.

With all that in mind, I thought I’d walk through what I consider the four basic steps in producing a backtest in Excel. Note that the core Excel file wasn't created by me - it was created by Jared over at CondorOptions (another must read if you're not following him).

Step 1: Get the data
The first step is to get your market data into Excel. There are two basic approaches to this – the first involves going to Yahoo Finance and downloading historical data directly as CSV and then loading it into Excel. This is nice, but does require a manual update of that data as you go forward – meaning, you’ll need to re-download that historical data and then copy and paste either the entire dataset or a subset to update your strategy.

The second approach is to use code to go grab data automatically from Yahoo Finance. Plenty of people have written VBA for doing just this – I have not written it myself so I don’t feel comfortable republishing the code. A quick search on Google will provide some examples to work with. There are also 3rd party tools that make the job simple – I’d recommend AnalyzerXL as it provides the most flexibility and options.

How you store this data in Excel is up to you – most people I know have a single sheet where they keep all the data, and then have a separate worksheet for the rest of system. For systems with a single instrument (such as the SPY), it’s not a problem to integrate the data and the system, but as the number of instruments goes up, you’ll want to have them on a separate worksheet to minimize scrolling and make it easy to update.

Step 2: Create your indicator
Now that we’ve got the data, we can use that data to construct an indicator or indicators. In this example, Jared constructed the DVI indicator (originally created by David over as CSS Analytics). You’ll see that we used 5 different columns to create the indicator – each one taking part of the calculation. One nice thing about working with Excel is that it really makes you think about how an indicator is constructed. It can be far too simple, these days, to throw down and indicator without understanding how it actually works.

The final indicator column, DVI, is a weighted sum of the DVI magnitude and DVI stretch columns. I’d also note that AnalyzerXL also contains a large number of indicators predefined to make backtesting easier, and there are other add-ons for Excel that provide similar functionality.

Step 3: Construct your trading rule
Now that you have an indicator, you need to construct your trading rules. In this example (calculation is in the “Signal” column), our trading rule is simple – we’re long if DVI is below 0.5 and short if above. Obviously you could have more complex rules – a neutral state where you’re not long or short, or variable position sizing as opposed to just all-in long or short.

Step 4: The trading rules/equity curve
There are many different approaches here, but what you can see in this example is a simple way to do it. Assume a starting cash value of $10,000 and then increment or decrement that by whether or not we are long or short on the close of the prior day, and whether we were correct or not. In function form, we represent this by saying: if long, then multiple the prior day’s equity by the ratio of today’s close to yesterday’s close, otherwise multiple the prior day’s equity by ratio of yesterday’s close to today’s close. We can then, obviously, graph the results. Note also that we’re using cash here, but you could easily do raw percentages in place of a cash value.

What’s missing here can be important for deciding whether to trade or not trade a system. First of all, the results here are frictionless – they assume there is no cost/commission for the trade. In high frequency swing systems like this one, the commissions could have a major impact on the viability of a given strategy.

Second, we don’t have any statistics on the performance of the strategy – just a graph. Generally we want to know stats like CAGR and the Sharpe ratio to compare it with other strategies. We also don’t have monthly or yearly reporting. All of these things can be constructed in Excel with a bit of work – and again, AnalyzerXL provides a large number of reporting options as part of the package.

That's a basic overview of backtesting in Excel - hope that you all find it useful!

R/Finance 2011 Registration Open

The registration for R/Finance 2011--which will take place April 29 and 30 in Chicago--is NOW OPEN!

Building on the success of the two previous conferences in 2009 and 2010, we are expecting more than 250 attendees from around the world representing both industry and academia to join a record 30+ presentations covering all areas of finance with R.

This year we are excited to have longer tutorial sessions and an optional full-day workshop on the Thursday before the conference. In addition, we have worked hard to extend the great networking opportunities on both days with longer breaks and more hallway time between sessions.

New for 2011 is a special conference dinner that is held on Friday evening. Overlooking the river from the famed Chicago Mercantile Exchange, we have designed it to be a great way to continue the conversations from the first day, as well as offering a chance to dine and drink in Chicago style.

More details of the agenda are available at:

Registration can be directly accessed by going to

On behalf of the committee and sponsors, we look forward to seeing you in Chicago!

 Gib Bassett, Peter Carl, Dirk Eddelbuettel, Brian Peterson, Dale Rosenthal, Jeffrey Ryan, Joshua Ulrich

International Center for Futures and Derivatives at UIC
Revolution Analytics

One Market Data

Saturday, March 5, 2011

Moving from Excel to R

This first post of the Backtesting in Excel and R series will provide some resources to help smooth the transition from the familiarity and comfort of Excel to the potentially strange and intimidating world of R.

I made my voyage from Excel to R more than 5 years ago and learned mostly by trial and error (and reading the R manuals).  Most people don't prefer my approach of "keep at it until you figure it out", so I don't have a lot of personal advice to share.  My main piece of advice is that the best way to learn R is to use it, so most of the resources below focus on "how-to" do certain things in R.


While R for Windows comes with a very basic GUI (I'm not familiar with the R for Mac OS X GUIs), most people will want and benefit from something more elaborate.  The GUIs below were taken from the R GUI Projects page (visit the page for more information / alternatives) and I added the newcomer, RStudio:
Blogs / Videos

I haven't read these two books personally, but I've heard very good things about both of them.  Both focus on how to accomplish specific tasks with R.
I hope this is helpful.  As always, please add suggestions in the comments!

Thursday, February 17, 2011

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:
  1. Resources that make it easier to move from Excel to R
  2. How to test DVI in Excel
  3. 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.  Jared was kind enough to create the Excel framework for post 2, but did not have time to devote to a full post.  Thankfully, Damian Roskill has agreed to write post 2 using Jared's Excel file.

Hopefully this will be a useful example for those of you who currently use Excel but would like to explore how to use R.

Monday, February 14, 2011

Stack Exchange: Quantitative Finance in public beta

The Quantitative Finance Stack Exchange community entered public beta last week.  To quote the FAQ:
The Quantitative Finance Stack Exchange is intended specifically for professionals and traders working in investment banking, and academics involved in teaching and research.
Topics include pricing of securities, derivatives, options, risk modeling, quantitative techniques, and the mathematics used in quantitative finance.
Several members of the R/Finance community are active there too.  Some names you may recognize: Patrick Burns, Shane, Dirk Eddelbuettel, Jeff Ryan, and Paul Teetor.