An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20100513/026bf3b6/attachment.pl>
Pair Trade
3 messages · Trey Johnson, Me, Guy Yollin
Hi there. from a quick glance at you code I see this:
The residuals for GDX and GLD are obviously identical at all
times(GLD$RES==GDX$RES) , so you do NOT need the symbol loop within the
date loop.
"..for(symbol in symbols){.. "
which is causing everything to be bought twice. instead i would suggest to
just use the GLD$RES and whenever you go long GLD, go short GDX within the
same "if" block. [if that is what you want to do here]
best regards
hope you can forgive the incomplete quoting style here but it is late in
the day :)
Soren
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.
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
_______________________________________________
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.