1

I am doing a simple backtest in R based on blotter and found it to be very very slow. Is there a way to speed things up? Would parallel processing help out? Or is the package still in development and improvements will be made?

Where can I check out the source code of blotter?

Thanks

The entire system:

library(quantmod)
library(TTR)
library(blotter) # r-forge revision 193
library(PerformanceAnalytics)

# Set initial values
initDate='2002-07-31'
endDate='2009-10-31'
initEq=100000

# Set currency and instruments
currency("USD")
stock("IEF",currency="USD",multiplier=1)
stock("SPY",currency="USD",multiplier=1)
stock("TLT",currency="USD",multiplier=1)

# Load data with quantmod
print("Loading data")
symbols = c("IEF", "SPY","TLT")
getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct"))

# Adjust prices for splits/dividends (thanks pg)
#IEF = adjustOHLC(IEF)
#SPY = adjustOHLC(SPY)

# Convert data to monthly frequency (to.weekly() needs drop.time=FALSE)
#IEF = to.monthly(IEF, indexAt='endof')
#SPY = to.monthly(SPY, indexAt='endof')
#TLT = to.monthly(TLT, indexAt='endof')

# Set up indicators with TTR
print("Setting up indicators")
IEF$SMA = SMA(Cl(IEF), 200)
SPY$SMA = SMA(Cl(SPY), 200)
TLT$SMA = SMA(Cl(TLT), 200)

# Set up a portfolio object and an account object in blotter
initPortf(name='default', symbols=symbols, initDate=initDate)
initAcct(name='default', portfolios='default', initDate=initDate, initEq=initEq)
verbose = FALSE

# Create trades
for( i in 200:NROW(SPY) ) {
  CurrentDate=time(SPY)[i]
  equity = getEndEq(Account='default', CurrentDate)

  for( symbol in symbols ) {
    sym = get(symbol)
    ClosePrice = as.numeric(Cl(sym[i,]))
    Posn = getPosQty(Portfolio='default', Symbol=symbol, Date=CurrentDate)
    UnitSize = as.numeric(trunc((equity/NROW(symbols))/ClosePrice))

    # Position Entry (assume fill at close)
    if( Posn == 0 ) {
      # No position, so test to initiate Long position
      if( Cl(sym[i,]) > sym[i,'SMA'] ) {
       # Store trade with blotter
        addTxn('default', Symbol=symbol, TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty=UnitSize, TxnFees=0, verbose=verbose)
      }
    } else {
      # Have a position, so check exit
      if( Cl(sym[i,]) < sym[i,'SMA'] ) {
        # Store trade with blotter
        addTxn(Portfolio='default', Symbol=symbol, TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty=-Posn, TxnFees=0, verbose=verbose)
      }
    }
  } # End symbols loop

  # Calculate P&L and resulting equity with blotter
  updatePortf(Portfolio='default', Dates=CurrentDate)
  updateAcct(name='default', Dates=CurrentDate)
  updateEndEq(Account='default', Dates=CurrentDate)

} # End dates loop

# Buy and Hold cumulative equity
buyhold = exp(cumsum( ( 0.5*ROC(Cl(IEF)) + 0.5*ROC(Cl(SPY)) )[-1] ))

# Final values
cat('Tactical Asset Allocation Return: ',(getEndEq(Account='default', Date=CurrentDate)-initEq)/initEq,'\n')
cat('Buy and Hold Return: ',tail(buyhold,1)-1,'\n')

# Plot Strategy Summary
png(filename="20091118_blotter_strategy.png", 720, 720)
#charts.PerformanceSummary(ROC(getAccount('default')$TOTAL$End.Eq)[-1],main="Tactical Asset Allocation")
charts.PerformanceSummary(ROC(getAccount('default')$summary$End.Eq)[-1],main="Tactical Asset Allocation")
dev.off()

# Plot Buy and Hold Summary
png(filename="20091118_blotter_buyhold.png", 720, 720)
charts.PerformanceSummary(ROC(buyhold)[-1],main="Buy & Hold")
dev.off()

My system: i7-2630 2.0GHz 4GB memory

user1234440
  • 22,521
  • 18
  • 61
  • 103
  • 2
    You haven't provided any code to illustrate how blotter is slow. My assumption is that you're doing something completely unrelated to blotter that makes your code slow. The [source code is on R-forge](https://r-forge.r-project.org/scm/?group_id=316) (the same place you got the package). – Joshua Ulrich Aug 17 '12 at 21:00
  • im running the simple trading system on your website. http://blog.fosstrading.com/2009/11/tactical-asset-allocation-using-blotter.html – user1234440 Aug 17 '12 at 21:38
  • when i add one more instruments it takes 4:29 minutes to run the entire simulation – user1234440 Aug 17 '12 at 21:38
  • i edited the post. What was your time? I was thinking it may be that R utilizes single core.. – user1234440 Aug 17 '12 at 22:10
  • @Tom: `svn checkout svn://svn.r-forge.r-project.org/svnroot/blotter/` If that doesn't mean anything to you, see [this post](http://stackoverflow.com/q/11105131/967840) – GSee Aug 17 '12 at 22:31
  • i copied and pasted the system on FOSS trading, but i am running the latest blotter package...0.8.10..which i got form http://r-forge.r-project.org/R/?group_id=316 – user1234440 Aug 17 '12 at 22:45
  • You asked in your question: "Where can I check out the source code of blotter?" Sounds like you found it. – GSee Aug 17 '12 at 22:54
  • yes, i thought you were implying that my blotter version was causing the slowness – user1234440 Aug 17 '12 at 22:59
  • No. The for loop is causing the slowness. – GSee Aug 17 '12 at 23:02

1 Answers1

2

The code in the blog post you reference (in the comments) uses monthly data. Your code uses daily data. In the sample period, there are 24 times more days than there are months, so it's reasonable to expect your code to take 24 times longer.

As the blog post says, you should use quantstrat if you only want to test trading systems. It even provides a link to another post showing you how to test the same strategy using quantstrat.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418