[R-SIG-Finance] apply.paramset and parallel

Russell Miller uaruss at gmail.com
Wed Mar 26 18:59:21 CET 2014


I'm receiving an error when trying to run apply.paramset in parallel.
It works fine when running "registerDoSEQ()" (line ~146), however,
with that commented out I get "error calling combine function: ....".

I'm running this on windows and I understand there are some
differences in the parallel functionality between Windows and Linux,
but I think I have those addressed properly.

Code is below, basically a cut from the luxor demo (I experienced the
same problem with the actual demo code).  Any help is greatly
appreciated.

Thanks -
Russell




# most of the code below is courtesy of the r-sig-finance mailing list
(Rob Schmidt, 4/21/13)
require(quantstrat)

# first pass values of sweep parameters for chart
.fast = 5
.slow = 20

.qty=100000
initDate = '2002-10-21'
.from='2002-10-21'
.to='2002-10-30'
#.to='2002-10-31'

currency(c('GBP', 'USD'))
exchange_rate(c('GBPUSD'), tick_size=0.0001)
setSymbolLookup.FI(system.file('extdata', package='quantstrat'),
                   'GBPUSD')

getSymbols('GBPUSD', from=.from, to=.to, verbose=FALSE)
GBPUSD = to.minutes30(GBPUSD)
GBPUSD = align.time(to.minutes30(GBPUSD), 1800)

strat.st = 'xoverStrat'
port.st = 'xoverPort'
acct.st = 'xoverAcct'

###################################################################

try(rm("order_book.xoverPort", pos=.strategy))
try(rm("account.xoverAcct", "portfolio.xoverPort", pos=.blotter))

initPortf(port.st, symbols='GBPUSD', initDate=initDate, currency='USD')
initAcct(acct.st, portfolios=port.st, initDate=initDate, currency='USD')
initOrders(port.st, initDate=initDate)

strategy(strat.st, store=TRUE)

addPosLimit(portfolio=port.st,
            symbol='GBPUSD',
            timestamp=initDate,
            maxpos=.qty)

### indicators

add.indicator(strat.st, name = "SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .fast
              ),
              label="nFast"
)

add.indicator(strat.st, name="SMA",
              arguments = list(
                x = quote(Cl(mktdata)[,1]),
                n = .slow
              ),
              label="nSlow"
)

### signals

add.signal(strat.st, name = 'sigComparison',
           arguments = list(
             columns=c("nFast","nSlow"),
             relationship="gte",
             cross=FALSE
           ),
           label='smaUp'
)

add.signal(strat.st, name = 'sigComparison',
           arguments = list(
             columns=c("nFast","nSlow"),
             relationship="lt",
             cross=FALSE
           ),
           label='smaDn'
)

### rules

add.rule(strategy=strat.st,
         name='ruleSignal',
         type='enter',
         arguments=list(sigcol="smaUp",
                        sigval=TRUE,
                        orderqty=+.qty,
                        ordertype='market',
                        osFUN=osMaxPos,
                        orderside='long'))

add.rule(strategy=strat.st,
         name='ruleSignal',
         type='exit',
         arguments=list(sigcol="smaDn",
                        sigval=TRUE,
                        orderqty='all',
                        ordertype='market',
                        orderside='long'))

add.rule(strategy=strat.st,
         name='ruleSignal',
         type='enter',
         arguments=list(sigcol="smaDn",
                        sigval=TRUE,
                        orderqty=-.qty,
                        ordertype='market',
                        osFUN=osMaxPos,
                        orderside='short'))

add.rule(strategy=strat.st,
         name='ruleSignal',
         type='exit',
         arguments=list(sigcol="smaUp",
                        sigval=TRUE,
                        orderqty='all',
                        ordertype='market',
                        orderside='short'))

#applyStrategy(strat.st, port.st, prefer='Open', verbose = FALSE)
#updatePortf(port.st, Symbols='GBPUSD',
,Dates=paste('::',as.Date(Sys.time()),sep=''))
#chart.Posn(port.st, "GBPUSD")

###################################################################

require(foreach)
#require(doMC)  #  for linux
#registerDoMC(cores=4)  # (my quad core)


require(doParallel)  # for windows

cl1 <- makeCluster(2)
registerDoParallel(cl1) #cluster, cores=2) # by default number of
physical cores -1

# registerDoSEQ: do sequential execution (single thread)
# can be used to detect nontrading param combinations
# which will give this error:
#     error calling combine function:
#     <simpleError in results[[r$portfolio.st]] <- r: attempt to
#     select less than one element>
#registerDoSEQ()   #  comment this command for parallel calc

# parameter sweep distributions
.FastSMA = c(7,8)
.SlowSMA = c(10,11)

add.distribution(strat.st,
                 paramset.label = 'SMA',
                 component.type = 'indicator',
                 component.label = 'nFast',
                 variable = list(n = .FastSMA),
                 label = 'nFAST'
)

add.distribution(strat.st,
                 paramset.label = 'SMA',
                 component.type = 'indicator',
                 component.label = 'nSlow',
                 variable = list(n = .SlowSMA),
                 label = 'nSLOW'
)

add.distribution.constraint(strat.st,
               paramset.label = 'SMA',
               distribution.label.1 = 'nFAST',
               distribution.label.2 = 'nSLOW',
               operator = '<',
               label = 'SMA'
)

results <- apply.paramset(strat.st,
                          paramset.label='SMA',
                          portfolio.st=port.st,
                          account.st=acct.st)

print(results$tradeStats[,c(1,2,25,31)])

stopCluster(cl1)



# END CODE
##############################################
##############################################
##############################################
##############################################



More information about the R-SIG-Finance mailing list