[R-SIG-Finance] Quantstrat - Error while applying strategy

soren wilkening me at censix.com
Mon Apr 25 14:06:57 CEST 2011


Hi

I had a look. the main bug was that your 'randPos' function needs to return
a vector instead of a scalar. See the code. Seems to run ok. no credit for
elegance though :)

Soren

#
=================================================================================================
# The code provided here is from the crossMA example in quantstrat except in
the middle where said
#
=================================================================================================
require(quantstrat)
try(rm("order_book.macross",pos=.strategy),silent=TRUE)
try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'),
silent=TRUE)
stock.str='AAPL' # what are we trying it on
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
initDate = '2007-12-31'
initEq = 3000
portfolio.st='macross'
account.st='macross'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)


#
=================================================================================================
# Here is my own part of the code
# randPos is a function that provides an output -1, 0 or 1 for short,
neutral or long respectively.
# Up signal if random indicator = 1
# Down signal if random indicator = -1
# Enter rules: long or short if respectively up or down signal
# Exit rules: exit long or exit short if respectively down or up signal 
#
=================================================================================================

# need to redefine this. indicators have to be vector-valued functions
nDays <- 20
probDn <- 1/2
probUp <- 1/2
# Random rebalancing
randPos <- function(x, nDays, probDn, probUp) {
.randPos <- function(i) {
    # random rebalancing (no rebalancing = 0, rebalancing = 1)
    rebal <- ifelse(runif(1) < 1/nDays, 1, 0)
    # random position (short = -1, long = 1, neutral = 0)
    a <- runif(1)
    if (a < probDn) {
        pos <- -1 
    } else {
        pos <- ifelse(a > (1 - probUp), 1, 0)
    }
    # no rebalancing or neutral = 0, long = 1, short = -1
    return(rebal*pos)
}

return( xts( sapply(1:nrow(x), .randPos), order.by=index(x)) )
}


stratMACROSS<- strategy(portfolio.st)
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "randPos", 
    arguments = list(x = quote(mktdata), nDays = nDays, probDn = probDn,
probUp = probUp), label = "randInd")  #need to use quote(mktdata)

stratMACROSS <- add.signal(strategy = stratMACROSS, name = "sigThreshold", 
    arguments = list(label = "randInd", column = "randInd", threshold = -1,
relationship = "eq"), label = "sigDn")  #need to omit data=mktdata
stratMACROSS <- add.signal(strategy = stratMACROSS, name = "sigThreshold", 
    arguments = list(label = "randInd", column = "randInd", threshold = 1,
relationship = "eq"), label = "sigUp")   #need to omit data=mktdata
     
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal", 
    arguments = list(sigcol = "sigDn", sigval = TRUE, orderqty = -100,
ordertype = "market", orderside = "short", TxnFees = -5), type = "enter")
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal", 
    arguments = list(sigcol = "sigUp", sigval = TRUE, orderqty = 100,
ordertype = "market", orderside = "short", TxnFees = -5), type = "exit")
    
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal", 
    arguments = list(sigcol = "sigUp", sigval = TRUE, orderqty = 100,
ordertype = "market", orderside = "long", TxnFees = -5), type = "enter")
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal", 
    arguments = list(sigcol = "sigDn", sigval = TRUE, orderqty = -100,
ordertype = "market", orderside = "long", TxnFees = -5), type = "exit")
#
=================================================================================================
# End of my part
#
=================================================================================================

getSymbols(stock.str,from=initDate)
for (i in stock.str) {
  assign(i, adjustOHLC(get(i),use.Adjusted = TRUE))
}

start_t <-Sys.time()
out <-try(applyStrategy(strategy = stratMACROSS, portfolios=portfolio.st))
end_t <-Sys.time()
print(end_t-start_t)

start_t <-Sys.time()
updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
end_t <-Sys.time()
print("trade blotter portfolio update:")
print(end_t-start_t)

chart.Posn(Portfolio='macross',Symbol=stock.str)



-----
http://censix.com
--
View this message in context: http://r.789695.n4.nabble.com/Quantstrat-Error-while-applying-strategy-tp3472438p3473009.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list