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]]

