[R-SIG-Finance] delay argument of ruleSignal buggy ? (quantstrat)

Brian G. Peterson brian at braverock.com
Tue Aug 9 20:33:17 CEST 2011


'delay' isn't well tested and wasn't really intended for daily or lower
frequency data.

I'm certainly be willing to entertain a patch, but I haven't had
problems on tick data.

We originally had daily data execute no sooner than the next observation
after the signal, but people complained, so we allowed same-observation
execution on daily or lower frequency data.

I suppose something like 

if(is.timeBased(timestamp) && class(timestamp) != 'Date' )
ordertime<-timestamp+delay

might work.

Regards,

   - Brian

On Tue, 2011-08-09 at 20:09 +0200, Daniel Krizian wrote:
> Hello,
> 
> in quantstrat, I tried to pass "delay" argument inside ruleSignal function
> (via add.rule, my code attached) and found about the following error:
> 
> Error in if (ifelse(is.null(txnprice), FALSE, !is.na(txnprice))) { :
>   missing value where TRUE/FALSE needed
> 
> I think there is a bug in ruleOrderProc function in the following line:
> 
>     if(is.timeBased(timestamp)) ordertime<-timestamp+delay
>     else ordertime<-as.POSIXct(timestamp)+delay
> 
> #------------Here is the environment dump-------------------
> 
> > applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st)
> Error in if (ifelse(is.null(txnprice), FALSE, !is.na(txnprice))) { :
>   missing value where TRUE/FALSE needed
> > debugger()
> Message:  Error in if (ifelse(is.null(txnprice), FALSE, !is.na(txnprice))) {
> :
>   missing value where TRUE/FALSE needed
> Available environments had calls:
> 1: applyStrategy(strategy = stratMACROSS, portfolios = portfolio.st)
> 2: ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = mktdata,
> timespan = timespan, ...)
> 
> Enter an environment number, or 0 to exit
> Selection: 2
> Browsing in the environment with call:
>    ruleOrderProc(portfolio = portfolio, symbol = symbol, mktdata = mktdata,
> timespan = timespan, ...)
> Called from: debugger.look(ind)
> 
> Browse[1]> txnprice
> numeric(0)
> Browse[1]> timestamp
> [1] "2001-06-27"
> Browse[1]> delay
> [1] 86400
> Browse[1]> class(timestamp)
> [1] "Date"
> Browse[1]> is.timeBased(timestamp)
> [1] TRUE
> Browse[1]> if(is.timeBased(timestamp)) ordertime<-timestamp+delay
> Browse[1]> ordertime
> [1] "2238-01-16"
> 
> Browse[1]> getOrderBook("macross")
> $macross
> $macross$AAPL
>            Order.Qty Order.Price Order.Type Order.Side Order.Threshold
> Order.Status Order.StatusTime Order.Set Txn.Fees
> 1999-12-31 "0"       NA          "init"     "long"     "0"
> "closed"     "1999-12-31"     "1"       "0"
> 2238-01-15 "100"     "23.75"     "market"   "long"     NA
>  "open"       NA               NA        "0"
> 
> #------------end of the environment dump-------------------
> 
> *Side note - same symptom, different case: NON-TRADING DAYS*
> Another problematic line in ruleOrderProc is:
> 
> txnprice=as.numeric(getPrice(last(mktdata[txntime])
> 
> Consider what happens witch such delay value and mktdata that
>  timestamp+delay value falls inside a non-trading day (there is an irregular
> timestamping in mktdata).
> 
> > txnprice
> numeric(0)
> 
> Same error will ensue. Txnprice cannot be extracted, as mktdata doesn't have
> data at that timestamp (non-trading day). I used "POSIXct" class instead of
> "Date" class this time, to avoid previous case issue with "Date".
> 
> I can post demo on non-trading day case as well, if needed.
> 
> #----------- Here is my code (simplified demo maCross.R )  ----------
> 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='1999-12-31'
> 
> delay = 1*24*60*60 # trade with one day delay after signal
> 
> initEq=1000000
> 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)
> 
> stratMACROSS<- strategy(portfolio.st)
> 
> stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA",
> arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
> stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA",
> arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200")
> 
> stratMACROSS <- add.signal(strategy =
> stratMACROSS,name="sigCrossover",arguments =
> list(columns=c("ma50","ma200"),relationship="gt"),label="ma50.gt.ma200")
> stratMACROSS <- add.signal(strategy =
> stratMACROSS,name="sigCrossover",arguments =
> list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
> 
> stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
> arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100,
> ordertype='market', orderside='long', delay= delay ),type='enter')
> stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal',
> arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100,
> ordertype='market', orderside='long', delay= delay ),type='exit')
> 
> getSymbols(stock.str,from=initDate)
> 
> applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st)
> 
> #----------- end of code
> --------------------------------------------------------
> 
> With Best Regards,
> Daniel Krizian
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list