[R-SIG-Finance] Pair Trade
Guy Yollin
guy.yollin at rotellacapital.com
Thu May 13 22:03:16 CEST 2010
Hi Trey,
Good example code.
I believe you want to change the sign of the residuals for one of the symbols:
replace:
GLD$RES <- residuals(mod.bd)
GDX$RES <- residuals(mod.bd)
with:
GLD$RES <- residuals(mod.bd)
GDX$RES <- -residuals(mod.bd)
Hope this helps.
Best,
-- G
-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Trey Johnson
Sent: Thursday, May 13, 2010 11:57 AM
To: r-sig-finance at stat.math.ethz.ch
Subject: [R-SIG-Finance] Pair Trade
Hello All,
I'm trying to set up a back-test on a pairs trade using
blotter, but I'm stuck. The code below results in both securities being
bought or sold for each condition, when I need one security bought the other
security sold for each condition. For example, if the residual is greater
than 5, but GLD sell GDX. I've tried numerous alterations to no avail. I'm
using R version 2.10.1(2009-12-14). Any help is greatly appreciated.
Best Regards,
Trey Johnson
library(quantmod)
library(TTR)
library(blotter)
# Set initial values
initDate='2007-07-31'
endDate='2008-03-31'
initEq=100000
symbols = c("GLD", "GDX")
# Set currency and instruments
currency("USD")
for(symbol in symbols){
stock(symbol, currency="USD",multiplier=1)
}
# Load data with quantmod
print("Loading data")
getSymbols(symbols, from=initDate, to=endDate,
index.class=c("POSIXt","POSIXct"))
#use adjusted price
for(symbol in symbols){
x=get(symbol)
x = adjustOHLC(x, use.Adjusted=TRUE)
assign(symbol,x)
}
mod <- specifyModel(Cl(GLD) ~ Cl(GDX))
mod.bd <- buildModel(mod, method = "lm",
training.per=c("2007-07-31","2008-03-31"))
res<-residuals(mod.bd)
GLD$RES <- residuals(mod.bd)
GDX$RES <- residuals(mod.bd)
# Set up a portfolio object and an account object in blotter
initPortf(name='Pair Trade', symbols=symbols, initDate=initDate)
initAcct(name='Pair Acct', portfolios='Pair Trade', initDate=initDate,
initEq=initEq)
verbose = TRUE
# Create trades
for( i in 2:NROW(GLD) ) {
CurrentDate=time(GLD)[i]
equity = getEndEq(Account='Pair Acct', CurrentDate)
for(symbol in symbols) {
sym = get(symbol)
ClosePrice = as.numeric(Cl(sym[i,]))
Posn = getPosQty(Portfolio='Pair Trade', Symbol=symbol,
Date=CurrentDate)
UnitSize = as.numeric(100)
#as.numeric(trunc((equity/NROW(symbols))/ClosePrice))
#UnitSize=as.numeric(trunc(equity/ClosePrice))
# Position Entry (assume fill at close)
if( Posn == 0 ) {
#enter position
if( as.numeric(sym[i,'RES']) > as.numeric(5) ) {
addTxn(Portfolio= 'Pair Trade', Symbol=symbol, TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty=UnitSize, TxnFees=0, verbose=verbose)
} else
#enter position
if( as.numeric(sym[i,'RES']) < as.numeric(-5) ) {
addTxn(Portfolio= 'Pair Trade', Symbol=symbol, TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty=-UnitSize, TxnFees=0, verbose=verbose)
}
} else {
# exit position
if( (Posn > 0 && as.numeric(sym[i,'RES']) < as.numeric(0)) || (Posn <
0 && as.numeric(sym[i,'RES']) > as.numeric(0)) ) {
# Store trade with blotter
addTxn(Portfolio='Pair Trade', Symbol=symbol, TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty= -Posn, TxnFees=0, verbose=verbose)
}
}
} #end symbol loop
# Calculate P&L and resulting equity with blotter
updatePortf(Portfolio='Pair Trade', Dates=CurrentDate)
updateAcct(name='Pair Acct', Dates=CurrentDate)
updateEndEq(Account='Pair Acct', Dates=CurrentDate)
} # End date loop
[[alternative HTML version deleted]]
_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list