IKTrading
IKTrading copied to clipboard
using sigAND with doParallel results in error when combining
If you use the sigAND signal in sequential mode, there are no problems using doParallel, but in parallel mode, it results in this error. Apparently this is not compatible, but I'd think many users of this handy signal would want to run tests in parallel. Cod below reproduces the issue
error calling combine function: <simpleError in results[[r$portfolio.st]] <- r: attempt to select less than one element>
`####################################################################
Load packages
library(dplyr) suppressMessages(require(quantstrat)) require(IKTrading)
remove objects
rm(list = ls(all.names = T))
if(!exists(".instrument")) .instrument <<- new.env() if(!exists(".blotter")) .blotter <<- new.env() if(!exists(".strategy")) .strategy <- new.env()
DEFINE VARIABLES or parameters
initDate = "2000-01-01" symbol.st = 'CYB_DAY' portf.st = 'bug' acct.st = 'colony' strat.st = 'bee' initEq = 100000 nFast = 10 nSlow = 30 nSd = 1
GET DATA
set your working directory where the data is stored
setwd("/Users/tyamada/Google Drive/CFRM551/supplemental/") currency('USD') # initiate currency stock(symbol.st ,currency='USD', multiplier=1) # initiate stock
getSymbols(Symbols = symbol.st, src = "csv")
initPortf( portf.st, symbol.st, initDate=initDate) # initiate portfolio
initAcct( acct.st, portf.st, initEq=initEq, initDate=initDate) # initiate account
initOrders( portf.st, initDate=initDate ) # initiate order_book
bee = strategy(strat.st) # create strategy object
addPosLimit(
portfolio=portf.st,
symbol=symbol.st,
timestamp=initDate,
maxpos=300, longlevels = 3) # only trade in one direction once
bee <- add.indicator( strategy = strat.st, name = 'BBands', # TA name arguments = list(HLC=quote(HLC(mktdata)), n=nSlow, sd=nSd), label = 'BBand')
SMA column
bee <- add.indicator( strategy = strat.st, name = 'SMA', # TA name arguments = list(x=quote(Cl(mktdata)), n=nFast), label = 'MA' )
bee <- add.signal( strategy = strat.st, name = 'sigCrossover', arguments = list(columns=c('MA','dn'), relationship='lt'), label = 'MA.lt.dn')
SMA cross over upperBand
bee <- add.signal( strategy = strat.st, name = 'sigCrossover', arguments = list(columns=c('MA','up'), relationship='gt'), label = 'MA.gt.up')
add.signal(strat.st, name="sigAND",arguments=list(columns=c("MA.lt.dn","MA.gt.up"),cross=TRUE),label="unlikleyCross")
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.gt.up',
sigval = TRUE,
replace = F,
orderqty = 100,
ordertype = 'market',
# one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter', label = 'EnterLONG')
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'unlikleyCross',
sigval = TRUE,
replace = F,
orderqty = 500,
ordertype = 'market',
# one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter', label = 'EnterLONGBIG')
exitLong when SMA cross under LowerBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.lt.dn', sigval = TRUE, replace = F, orderqty = 'all', ordertype = 'market', orderside = 'long'), type = 'exit', label = 'ExitLONG')
enterShort when SMA cross under LowerBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.lt.dn', sigval = TRUE, replace = F, orderqty = -100, ordertype = 'market', orderside = 'short', osFUN = 'osMaxPos'), type = 'enter', label = 'EnterSHORT')
exitShort when SMA cross over upperBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.gt.up', sigval = TRUE, replace = F, orderqty = 'all', ordertype = 'market', orderside = 'short'), type = 'exit', label = 'ExitSHORT')
applyStrategy( strat.st, portf.st, prefer='Open', # why prefer='Open' verbose=T)
updatePortf( portf.st) #,
updateAcct( acct.st) # ,
updateEndEq( Account = acct.st)#,
User Set up pf parameter ranges to test
.nFastList = 5:13 .nSlowList = 10:40 .nSdList = 1:3
number of random samples of the parameter distribution to use for random run
.nsamples = 10
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'BBand', #this is the label given to the indicator in the strat variable = list(n = .nSlowList), label = 'BBandMA' )
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'BBand', #this is the label given to the indicator in the strat variable = list(sd = .nSdList), label = 'BBandSD' )
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'MA', #this is the label given to the indicator in the strat variable = list(n = .nFastList), label = 'MAn' )
add.distribution.constraint(strat.st, paramset.label = 'SMA_BBparams', distribution.label.1 = 'BBandMA', distribution.label.2 = 'MAn', operator = '>', label = 'BBandMA>MAn' )
parallel computing to speed up
if( Sys.info()['sysname'] == "Windows" ) { library(doParallel) registerDoParallel(cores=detectCores()) registerDoSEQ() } else { library(doMC) registerDoMC(cores=detectCores()) }
results <- apply.paramset(strat.st, paramset.label='SMA_BBparams', portfolio.st=portf.st, account.st=acct.st, samples= 0, # take all options #.nsamples, only take 10 samples verbose=TRUE)
results$tradeStats %>% View() `
Try using sigFormula and see if the error persists.
On Wed, Jun 1, 2016 at 9:07 PM, sonicfray [email protected] wrote:
If you use the sigAND signal in sequential mode, there are no problems using doParallel, but in parallel mode, it results in this error. Apparently this is not compatible, but I'd think many users of this handy signal would want to run tests in parallel. Cod below reproduces the issue
error calling combine function:
`#################################################################### Load packages
##################################################################### library(dplyr) suppressMessages(require(quantstrat)) require(IKTrading)
#################################################################### remove objects
########################################################################
rm(list = ls(all.names = T))
if(!exists(".instrument")) .instrument <<- new.env() if(!exists(".blotter")) .blotter <<- new.env() if(!exists(".strategy")) .strategy <- new.env()
######################################################################## DEFINE VARIABLES or parameters
########################################################################
initDate = "2000-01-01" symbol.st = 'CYB_DAY' portf.st = 'bug' acct.st = 'colony' strat.st = 'bee' initEq = 100000 nFast = 10 nSlow = 30 nSd = 1
######################################################################## GET DATA
############################################################### set your working directory where the data is stored
setwd("/Users/tyamada/Google Drive/CFRM551/supplemental/") currency('USD') # initiate currency stock(symbol.st ,currency='USD', multiplier=1) # initiate stock
getSymbols(Symbols = symbol.st, src = "csv")
initPortf( portf.st, symbol.st, initDate=initDate) # initiate portfolio
initAcct( acct.st, portf.st, initEq=initEq, initDate=initDate) # initiate account
initOrders( portf.st, initDate=initDate ) # initiate order_book
bee = strategy(strat.st) # create strategy object
addPosLimit( portfolio=portf.st, symbol=symbol.st, timestamp=initDate,
maxpos=300, longlevels = 3) # only trade in one direction once
bee <- add.indicator( strategy = strat.st, name = 'BBands', # TA name arguments = list(HLC=quote(HLC(mktdata)), n=nSlow, sd=nSd), label = 'BBand') SMA column
bee <- add.indicator( strategy = strat.st, name = 'SMA', # TA name arguments = list(x=quote(Cl(mktdata)), n=nFast), label = 'MA' )
bee <- add.signal( strategy = strat.st, name = 'sigCrossover', arguments = list(columns=c('MA','dn'), relationship='lt'), label = 'MA.lt.dn') SMA cross over upperBand
bee <- add.signal( strategy = strat.st, name = 'sigCrossover', arguments = list(columns=c('MA','up'), relationship='gt'), label = 'MA.gt.up')
add.signal(strat.st, name="sigAND",arguments=list(columns=c("MA.lt.dn","MA.gt.up"),cross=TRUE),label="unlikleyCross")
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.gt.up', sigval = TRUE, replace = F, orderqty = 100, ordertype = 'market',
one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long', osFUN = 'osMaxPos'),
type = 'enter', label = 'EnterLONG')
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'unlikleyCross', sigval = TRUE, replace = F, orderqty = 500, ordertype = 'market',
one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long', osFUN = 'osMaxPos'),
type = 'enter', label = 'EnterLONGBIG') exitLong when SMA cross under LowerBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.lt.dn', sigval = TRUE, replace = F, orderqty = 'all', ordertype = 'market', orderside = 'long'), type = 'exit', label = 'ExitLONG') enterShort when SMA cross under LowerBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.lt.dn', sigval = TRUE, replace = F, orderqty = -100, ordertype = 'market', orderside = 'short', osFUN = 'osMaxPos'), type = 'enter', label = 'EnterSHORT') exitShort when SMA cross over upperBand
bee <- add.rule( strategy = strat.st, name = 'ruleSignal', arguments = list(sigcol = 'MA.gt.up', sigval = TRUE, replace = F, orderqty = 'all', ordertype = 'market', orderside = 'short'), type = 'exit', label = 'ExitSHORT')
applyStrategy( strat.st, portf.st, prefer='Open', # why prefer='Open' verbose=T)
updatePortf( portf.st) #,
updateAcct( acct.st) # ,
updateEndEq( Account = acct.st)#, User Set up pf parameter ranges to test
.nFastList = 5:13 .nSlowList = 10:40 .nSdList = 1:3 number of random samples of the parameter distribution to use for random run
.nsamples = 10
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'BBand', #this is the label given to the indicator in the strat variable = list(n = .nSlowList), label = 'BBandMA' )
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'BBand', #this is the label given to the indicator in the strat variable = list(sd = .nSdList), label = 'BBandSD' )
add.distribution(strat.st, paramset.label = 'SMA_BBparams', component.type = 'indicator', component.label = 'MA', #this is the label given to the indicator in the strat variable = list(n = .nFastList), label = 'MAn' )
add.distribution.constraint(strat.st, paramset.label = 'SMA_BBparams', distribution.label.1 = 'BBandMA', distribution.label.2 = 'MAn', operator = '>', label = 'BBandMA>MAn' ) parallel computing to speed up
if( Sys.info()['sysname'] == "Windows" ) { library(doParallel) registerDoParallel(cores=detectCores()) registerDoSEQ() } else { library(doMC) registerDoMC(cores=detectCores()) }
results <- apply.paramset(strat.st, paramset.label='SMA_BBparams', portfolio.st=portf.st, account.st=acct.st, samples= 0, # take all options #.nsamples, only take 10 samples verbose=TRUE)
results$tradeStats %>% View() `
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/IlyaKipnis/IKTrading/issues/5, or mute the thread https://github.com/notifications/unsubscribe/AEAO0WXNinBgzmxwE50UT8_YglalmtdAks5qHiy9gaJpZM4IsHie .
That worked- thanks. I really enjoy your blog, by the way and other students in my electronic trading class have found it useful too!
Date: Wed, 1 Jun 2016 18:24:29 -0700 From: [email protected] To: [email protected] CC: [email protected]; [email protected] Subject: Re: [IlyaKipnis/IKTrading] using sigAND with doParallel results in error when combining (#5)
Try using sigFormula and see if the error persists.
On Wed, Jun 1, 2016 at 9:07 PM, sonicfray [email protected] wrote:
If you use the sigAND signal in sequential mode, there are no problems
using doParallel, but in parallel mode, it results in this error.
Apparently this is not compatible, but I'd think many users of this handy
signal would want to run tests in parallel. Cod below reproduces the issue
error calling combine function:
`####################################################################
Load packages
#####################################################################
library(dplyr)
suppressMessages(require(quantstrat))
require(IKTrading)
####################################################################
remove objects
########################################################################
rm(list = ls(all.names = T))
if(!exists(".instrument")) .instrument <<- new.env()
if(!exists(".blotter")) .blotter <<- new.env()
if(!exists(".strategy")) .strategy <- new.env()
########################################################################
DEFINE VARIABLES or parameters
########################################################################
initDate = "2000-01-01"
symbol.st = 'CYB_DAY'
portf.st = 'bug'
acct.st = 'colony'
strat.st = 'bee'
initEq = 100000
nFast = 10
nSlow = 30
nSd = 1
########################################################################
GET DATA
###############################################################
set your working directory where the data is stored
setwd("/Users/tyamada/Google Drive/CFRM551/supplemental/")
currency('USD') # initiate currency
stock(symbol.st ,currency='USD', multiplier=1) # initiate stock
getSymbols(Symbols = symbol.st, src = "csv")
initPortf(
portf.st,
symbol.st,
initDate=initDate) # initiate portfolio
initAcct(
acct.st,
portf.st,
initEq=initEq,
initDate=initDate) # initiate account
initOrders(
portf.st,
initDate=initDate ) # initiate order_book
bee = strategy(strat.st) # create strategy object
addPosLimit(
portfolio=portf.st,
symbol=symbol.st,
timestamp=initDate,
maxpos=300, longlevels = 3) # only trade in one direction once
bee <- add.indicator(
strategy = strat.st,
name = 'BBands', # TA name
arguments = list(HLC=quote(HLC(mktdata)),
n=nSlow,
sd=nSd),
label = 'BBand')
SMA column
bee <- add.indicator(
strategy = strat.st,
name = 'SMA', # TA name
arguments = list(x=quote(Cl(mktdata)),
n=nFast),
label = 'MA' )
bee <- add.signal(
strategy = strat.st,
name = 'sigCrossover',
arguments = list(columns=c('MA','dn'),
relationship='lt'),
label = 'MA.lt.dn')
SMA cross over upperBand
bee <- add.signal(
strategy = strat.st,
name = 'sigCrossover',
arguments = list(columns=c('MA','up'),
relationship='gt'),
label = 'MA.gt.up')
add.signal(strat.st,
name="sigAND",arguments=list(columns=c("MA.lt.dn","MA.gt.up"),cross=TRUE),label="unlikleyCross")
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.gt.up',
sigval = TRUE,
replace = F,
orderqty = 100,
ordertype = 'market',
one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterLONG')
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'unlikleyCross',
sigval = TRUE,
replace = F,
orderqty = 500,
ordertype = 'market',
one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterLONGBIG')
exitLong when SMA cross under LowerBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.lt.dn',
sigval = TRUE,
replace = F,
orderqty = 'all',
ordertype = 'market',
orderside = 'long'),
type = 'exit',
label = 'ExitLONG')
enterShort when SMA cross under LowerBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.lt.dn',
sigval = TRUE,
replace = F,
orderqty = -100,
ordertype = 'market',
orderside = 'short',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterSHORT')
exitShort when SMA cross over upperBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.gt.up',
sigval = TRUE,
replace = F,
orderqty = 'all',
ordertype = 'market',
orderside = 'short'),
type = 'exit',
label = 'ExitSHORT')
applyStrategy(
strat.st,
portf.st,
prefer='Open', # why prefer='Open'
verbose=T)
updatePortf(
portf.st) #,
updateAcct(
acct.st) # ,
updateEndEq(
Account = acct.st)#,
User Set up pf parameter ranges to test
.nFastList = 5:13
.nSlowList = 10:40
.nSdList = 1:3
number of random samples of the parameter distribution to use for random
run
.nsamples = 10
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'BBand', #this is the label given to the indicator in
the strat
variable = list(n = .nSlowList),
label = 'BBandMA'
)
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'BBand', #this is the label given to the indicator in
the strat
variable = list(sd = .nSdList),
label = 'BBandSD'
)
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'MA', #this is the label given to the indicator in the
strat
variable = list(n = .nFastList),
label = 'MAn'
)
add.distribution.constraint(strat.st,
paramset.label = 'SMA_BBparams',
distribution.label.1 = 'BBandMA',
distribution.label.2 = 'MAn',
operator = '>',
label = 'BBandMA>MAn'
)
parallel computing to speed up
if( Sys.info()['sysname'] == "Windows" )
{
library(doParallel)
registerDoParallel(cores=detectCores())
registerDoSEQ()
} else {
library(doMC)
registerDoMC(cores=detectCores())
}
results <- apply.paramset(strat.st,
paramset.label='SMA_BBparams',
portfolio.st=portf.st,
account.st=acct.st,
samples= 0, # take all options
#.nsamples, only take 10 samples
verbose=TRUE)
results$tradeStats %>% View()
`
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/IlyaKipnis/IKTrading/issues/5, or mute the thread
https://github.com/notifications/unsubscribe/AEAO0WXNinBgzmxwE50UT8_YglalmtdAks5qHiy9gaJpZM4IsHie
.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Came across this article of a couple of years ago today on encountering a similar problem with parallel execution of quantstrat strategies. Solution worked and thanx very much.