# The Average Investor's Blog

## A software developer view on the markets

Advertisements

## Pre-computing a trading plan in parallel

Posted by The Average Investor on Nov 11, 2011

R version 2.14 introduced a new package, called parallel. This new package combines the functionality from two previous packages: snow and multicore. Since I was using multicore to parallelise my computations, I had to migrate to the new package and decided to publish some code.

Often trading strategies are tested using the daily closing price both to determine the position and to perform the trading. Since we need to pre-compute an action plan, parallelisation may be necessary if the computations are heavy.

The code at the end of this post is pre-computing the actions for the CSS Analytics’ DVI indicator. The entry point in the code is as follows:

```library( quantmod )
library( parallel )

# Load the code at the end of this post

# Get the SPY ETF from Yahoo
getSymbols( "SPY", from="1900-01-01" )

# Compute the actions
computeDVIActionPar( Cl( SPY ), range=10, cores=8 )
```

This basically requests to compute the position for all possible closing prices between -10% and +10%, parallelising the work 8 fold. The output of the command is something like:

```   Price    Pct Position
1 111.59 -10.00        1
2 127.97   3.21       -1
3 136.38  10.00       -1
```

This output tells us that if the SPY doesn’t advance more than 3.21%, closing above \$127.97, we should establish a long position at the close, otherwise – short. With that knowledge, and depending on the current position, what is left to do is to go to our Interactice Broker account and to put a limit on-close order. The complete code for the missing functions follows.

```computeOneDVIAction = function( close, x )
{
x[tail( index( x ), 1 )] = close
dvi = DVI( x )
val = as.numeric( tail( dvi\$dvi, 1 ) )

# Short if DVI > 0.5, long otherwise
if( is.na( val ) )
{
return( 0 )
}
else if( val > 0.5 )
{
return( -1 )
}

return( 1 )
}

computeDVIActionPar = function( x, step=0.01, range=5, cores )
{
require( quantmod, quietly=TRUE )
require( parallel, quietly=TRUE )

prices = c( )
positions = c( )

latestClose = as.numeric( coredata( last( x ) ) )

# Shift to the left to use the last entry as the "guessed" close
yy = lag( x, -1 )

# range is percentages
range = range / 100

# Compute the vector with all closing prices within the range
close = latestClose * ( 1 - range )
lastClose = latestClose * ( 1 + range )

close = round( close / step ) * step
numSteps = ( close - latestClose ) / step + 1

close = round( close, 2 )
lastClose = ceiling( lastClose * 100 ) / 100

closes = close

repeat
{
if( close >= lastClose ) break

close = round( latestClose + step*numSteps, 2 )

numSteps = numSteps + 1

closes = c( closes, close )
}

# Detect the cores if not supplied
if( missing( cores ) )
{
cores = parallel:::detectCores()
}

res = mclapply( closes,
computeOneDVIAction,
x = yy,
mc.cores = cores )

# Summarize the positions
prices = c()
pcts = c()
positions = c()

# Impossible position
lastPosition = -1e9

len = length( closes )
for( ii in 1:(len - 1) )
{
if( res[[ii]] != lastPosition )
{
positions = append( positions, res[[ii]] )
prices = append( prices, closes[ii] )
pcts = append( pcts, round( ( closes[ii] - latestClose ) /
latestClose * 100, 2 ) )
lastPosition = res[[ii]]
}
}

positions = append( positions, res[[len]] )
prices = append( prices, closes[ii] )
pcts = append( pcts, round( ( closes[len] - latestClose ) /
latestClose * 100, 2 ) )

df = data.frame( prices, pcts, positions )
colnames( df ) = c( "Price", "Pct", "Position" )
return( df )
}
```
Advertisements

1. ### S&P 500 10 month SMA action preview « The Average Investor's Blogsaid

[…] compute these values, I use a parallel approach to the one shown for the DVI indicator. The code will be posted soon, but right now I am quite busy with some improvements on the […]

2. ### Hexanolsaid

I’ve found the blog very informative… but I’m running into an error when trying to compute this myself. I was wondering if you could help, I’m fairly new to R. Using the daily data I can produce an output, but I wanted to try using monthly data. I simply used SPY <- to.monthly(SPY)

and when I run computeDVIActionPar(Cl(SPY), range=5, cores=8) I get the following error:

Error in runSum(x, n) : Series contains non-leading NAs\n
Warning message:
In mclapply(closes, computeOneDVIAction, x = yy, mc.cores = cores) :
all scheduled cores encountered errors in user code

Any help would be appreciated! Thanks!

• ### The Average Investorsaid

If you post a piece of code that reproduces it, I might be able to help you. Tried a few things but, never got the same error. Sounds like the problem is that there are NA somewhere, so if it’s in the input, you can trim them using na.trim from the zoo package.

DVI is a very specifically optimized to daily S&P500, you might want to look elsewhere for a good monthly strategy.

3. ### Hexanolsaid

Thanks, I was just interested to see how it would have performed on a monthly basis. I’ll try a few more things this weekend to see if I can reproduce the problem again.

4. ### DVI Position Changes « The Average Investor's Blogsaid

[…] gain as of yesterday’s close. For more details how to pre-compute the DVI actions, see my previous post. GA_googleAddAttr("AdOpt", "1"); GA_googleAddAttr("Origin", "other"); […]