[R-SIG-Finance] Quantstrat pair trade

G See gsee000 at gmail.com
Sun May 8 03:40:31 CEST 2011

Normally, I would calculate a spread ahead of time and treat it like a
single stock, but in the interest of creating examples for quantstrat, I
created a strategy that will enter each leg separately.

The idea is to calculate a *ratio* of the 2 stocks (StockA / StockB).  If
that *ratio* goes above it's 2 stdev band, then, when it crosses back
through it, sell StockA and buy Stock B.  If the *ratio* goes below it's 2
stdev band, then, when it crosses back above it, buy StockA and sell Stock
B.  When the *ratio* cross it's moving average, flatten out.

I needed to make a *rule* act differently for different symbols.  i.e. when
the *ratio* crosses it's upper bound, the *rule* for StockA should be buy
and the *rule* for StockB should be sell.  But, that would only work if the
*rule* knew which stock it was operating on.  So, the plan was to have an
order sizing function check the symbol and return the opposite quantity if
it is the second symbol.  (e.g. -100 instead of 100).

But, it is not as easy to change the orderside.

I modified osMaxPos and *ruleSignal*, but that's not enough.

What function writes to the orderbook object?
Is there a better way to do this?
I tried setting orderside with the <<- operator, but that doesn't help.

My code is attached.  Sorry it's so long, but it has to include custom
osMaxPos and *ruleSignal* functions.  You'll see in the orderbook for XOM
that where orderside is "short" I have a positive orderqty and vice versa.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110507/62ed10ba/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: quantstrat pair trade.r
Type: application/octet-stream
Size: 13384 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110507/62ed10ba/attachment.obj>

More information about the R-SIG-Finance mailing list