Nuts and Bolts of Quantstrat, Part V

This post will be about pre-processing custom indicators in quantstrat–that is, how to add values to your market data that do not arise from the market data itself.

The first four parts of my nuts and bolts of quantstrat were well received. They are even available as a datacamp course. For those that want to catch up to today’s post, I highly recommend the datacamp course.

To motivate this post, the idea is that say you’re using alternative data that isn’t simply derived from a transformation of the market data itself. I.E. you have a proprietary alternative data stream that may predict an asset’s price, you want to employ a cross-sectional ranking system, or any number of things. How do you do this within the context of quantstrat?

The answer is that it’s as simple as binding a new xts to your asset data, as this demonstration will show.

First, let’s get the setup out of the way.





symbols <- 'SPY'
suppressMessages(getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE))  

stock(symbols, currency="USD", multiplier=1)

Now, we have our non-derived indicator. In this case, it’s a toy example–the value is 1 if the year is odd (I.E. 2003, 2005, 2007, 2009), and 0 if it’s even. We compute that and simply column-bind (cbind) it to the asset data.

nonDerivedIndicator <- as.numeric(as.character(substr(index(SPY), 1, 4)))%%2 == 1
nonDerivedIndicator <- xts(nonDerivedIndicator,

SPY <- cbind(SPY, nonDerivedIndicator)
colnames(SPY)[7] = "nonDerivedIndicator"

Next, we just have a very simple strategy–buy a share of SPY on odd years, sell on even years. That is, buy when the nonDerivedIndicator column crosses above 0.5 (from 0 to 1), and sell when the opposite occurs. <- <- <- "nonDerivedData"
initPortf(, symbols=symbols, initDate=initDate, currency='USD')
initAcct(,, initDate=initDate, currency='USD')
initOrders(, initDate=initDate)
strategy(, store=TRUE)

add.signal(, name = sigThreshold, 
           arguments = list(column = "nonDerivedIndicator", threshold = 0.5, relationship = "gte", cross = TRUE),
           label = "longEntry")

add.signal(, name = sigThreshold, 
           arguments = list(column = "nonDerivedIndicator", threshold = 0.5, relationship = "lte", cross = TRUE),
           label = "longExit")

tmp <- applySignals(strategy =, mktdata=SPY)

add.rule(, name="ruleSignal", 
         arguments=list(sigcol="longEntry", sigval=TRUE, ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open", orderqty = 1), 
         type="enter", path.dep=TRUE)

add.rule(, name="ruleSignal", 
         arguments=list(sigcol="longExit", sigval=TRUE, orderqty="all", 
                        ordertype="market", orderside="long", 
                        replace=FALSE, prefer="Open"), 
         type="exit", path.dep=TRUE)

#apply strategy
t1 <- Sys.time()
out <- applyStrategy(,
t2 <- Sys.time()

#set up analytics
dateRange <- time(getPortfolio($summary)[-1]

And the result:

chart.Posn(, 'SPY')

In conclusion, you can create signals based off of any data in quantstrat. Whether that means volatility ratios, fundamental data, cross-sectional ranking, or whatever proprietary alternative data source you may have access to, this very simple process is how you can use quantstrat to add all of those things to your systematic trading backtest research.

Thanks for reading.

Note: I am always interested in full-time opportunities which may benefit from my skills. I have experience in data analytics, asset management, and systematic trading research. If you know of any such opportunities, do not hesitate to contact me on my LinkedIn, found here.


6 thoughts on “Nuts and Bolts of Quantstrat, Part V

  1. Thanks for the interesting post. I’ve taken your DataCamp course, and I recommend it. I blindly tried to run the script but it died on this statement:

    tmp <- applySignals(strategy =, mktdata=SPY)

    with the error message…

    "Error in get(signal$name) : invalid first argument"

    Thanks again for a great blog.

  2. Pingback: Nuts and Bolts of Quantstrat, Part V | A bunch of data

  3. Pingback: Nuts and Bolts of Quantstrat, Part V – Mubashir Qasim

  4. Pingback: Quantocracy's Daily Wrap for 04/13/2017 | Quantocracy

  5. Someone may have already asked this question, but anyway, I’d like to ask about it because you have already met with this situation, and I mean using the script “demoData.R” I changed the source on google to get data for the local stock market
    library (quantstrat)
    initDate = “2000-01-01”
    from = “2010-01-01”
    to = “2016-12-31”
    options (width = 70)
    options (“getSymbols.warning4.0” = FALSE)
    rm (list = ls (.blotter), envir = .blotter)
    currency (‘PLN’)Sys.setenv (TZ = “UTC”)
    Symbols <- c ("WSE: AGO",
    "WSE: AMC",
    "WSE: PGD",
    "WSE: BDX",
    "WSE: KGH",
    "WSE: KTY",
    "WSE: RFK",
    "WSE: PGE",
    "WSE: TRK",
    "WSE: MSZ",
    "WSE: RMK",
    "WSE: TPE",
    "WSE: GPW",
    "WSE: PKO",
    "WSE: PKN")
    if (! "WSE: AGO"% in% ls ()) { (getSymbols (symbols, from = from,
    to = to, src = "google", auto.assign = TRUE))

    However, the result is : "Error in naCheck (x, n): Series contains non-leading NAs".
    This is because the "WSE: RMK" symbol in the last column(volume) contains values equal to zero. What should be done to the values of zero in this column to be accepted and let the script carry on without the error ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s