[R-SIG-Finance] quantstrat: getting an error when using many symbols and %-based order sizing function
Sergey Pisarenko
drseergio at gmail.com
Fri Feb 17 21:01:12 CET 2012
Greetings to the R finance community,
I have written a sizing function (pretty much a slight modification of
a function from Guy Yollin's slides). The problem is that it fails
whenever I use many symbols. It works fine for one or few symbols.
The problem manifests itself inside of the sizing function, when
updatePortf is run. The error I get is:
> source('tainted_2.R')
Error in if (nzchar(intervals[1])) s <- as.POSIXlt(do.call(firstof,
parse.side(intervals[1]))) :
argument is of length zero
Traceback:
> traceback()
13: xts:::.parseISO8601(Dates)
12: first(xts:::.parseISO8601(Dates))
11: .updatePosPL(Portfolio = pname, Symbol = as.character(symbol),
Dates = Dates, Prices = Prices, ... = ...)
10: updatePortf(Portfolio = portfolio, Dates = paste("::", as.Date(timestamp),
sep = "")) at tainted_2.R#100
9: osFUN(strategy = strategy, data = data, timestamp = timestamp,
orderqty = orderqty, ordertype = ordertype, orderside = orderside,
portfolio = portfolio, symbol = symbol, ... = ..., ruletype = ruletype,
orderprice = as.numeric(orderprice))
8: function (data = mktdata, timestamp, sigcol, sigval, orderqty = 0,
ordertype, orderside = NULL, threshold = NULL, tmult = FALSE,
replace = TRUE, delay = 1e-04, osFUN = "osNoOp", pricemethod =
c("market",
"opside", "active"), portfolio, symbol, ..., ruletype,
TxnFees = 0, prefer = NULL, sethold = FALSE)
...
7: do.call(fun, .formals)
6: ruleProc(strategy$rules[[type]], timestamp = timestamp, path.dep = path.dep,
mktdata = mktdata, portfolio = portfolio, symbol = symbol,
ruletype = type, mktinstr = mktinstr, ...)
5: applyRules(portfolio = portfolio, symbol = symbol, strategy = strategy,
mktdata = mktdata, Dates = NULL, indicators = sret$indicators,
signals = sret$signals, parameters = parameters, ..., path.dep = TRUE)
4: applyStrategy(strategy = "s", portfolios = "p")
3: eval.with.vis(expr, envir, enclos)
2: eval.with.vis(ei, envir)
1: source("tainted_2.R")
The sizing function looks like:
osPercentEquity <- function(timestamp, orderqty, portfolio, symbol,
ruletype, ...) {
posn <- getPosQty(portfolio, symbol, timestamp)
if (posn == 0) {
tempPortfolio <- getPortfolio(portfolio)
dummy <- updatePortf(Portfolio=portfolio, Dates=paste('::',
as.Date(timestamp), sep='')) # <<<<<< this is where it fails
trading.pl <- sum(getPortfolio(portfolio)$summary$Net.Trading.PL)
assign(paste("portfolio.", portfolio, sep=""), tempPortfolio, pos=.blotter)
total.equity <- equity + trading.pl
tradeSize <- total.equity * prcTrade
ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
orderqty <- sign(orderqty) * round(tradeSize / ClosePrice)
return(orderqty)
}
}
The add.rule looks like:
s <- add.rule(strategy=s, name='ruleSignal',
arguments=list(data=quote(mktdata), sigcol='buySig', sigval=TRUE,
orderqty=qtyDef, ordertype='market', orderside=NULL, threshold=NULL,
osFUN='osPercentEquity', ruletype='enter'), type='enter')
Traceback suggests that there is a problem with the date format. I
have inserted print statements and verified that the date itself is
fine. It is also weird that the issue does not occur when using few
symbols.
I have attached runnable code that reproduces the problem. Please
excuse any apparent inefficiencies you see. I would like to get
strategy working first and then optimize for performance.
Is there something wrong with the approach I have taken? Once the
number of symbols is reduced the problem does not trigger.
Truly appreciate your help.
--
Kind Regards,
Sergey Pisarenko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tainted_2.R
Type: application/octet-stream
Size: 5148 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120217/c842422f/attachment.obj>
More information about the R-SIG-Finance
mailing list