Hello Brian,
i have realized that the delay i needed could be modeled easily using
already available logic.

Solution: so I simply lag the desired signal using data = quote(lag(mktdata,
delay))

s <- add.signal( strategy = s ,name="sigThreshold", arguments = list(data =
quote(lag(mktdata, delay)), ...................) , ...........)

as opposed to modeling via

s <- add.rule(strategy = s,name='ruleSignal', arguments = list(......, delay
= delay), ....... )

The first is a delay between receiving the signal and issuing order (e.g.
Portfolio manager checks Close price during the next trading day and
immediately places an order, so delay = 1), whereas
The latter is different kind of delay - an infrastructural one: between
order and trade, which usually expects seconds or minutes, not days.

So my need was really to model signal-to-order delay, not the order-to-trade
delay.

This solves the problem when the delayed day falls on a non-trading day. The
other problem with the date format is solved with your patch, yet I realize
it is best to work with the POSIX within the quantstrat.

Thanks,
Daniel
On Tue, Aug 9, 2011 at 10:29 PM, Daniel Krizian <daniel.krizian@gmail.com>wrote:

> 'delay' parameter would be nice to have to analyze how the signal
> performance might deteriorate over time after its occurrence (i.e. passing
> delay = 1 day, 2 days, 3 days etc. (or weeks). In other words, testing
> information quality of the signal. Just an idea.
>
> Regarding the other problem with 'delay' on non-trading days , I suggest
> something along the lines:
>
> if ( !is.null( getPrice(last(mktdata[txntime])) ) )      txnprice =
> as.numeric(getPrice(last(mktdata[txntime])))
>    else      txnprice = as.numeric ( getPrice ( first (mktdata[ paste(
> txntime, "::" , sep = "") ] )))
>
> - to replace original line:
>   txnprice=as.numeric(getPrice(last(mktdata[txntime])))
>
> Always happy to help.
> Daniel Krizian
>
> On Tue, Aug 9, 2011 at 8:33 PM, Brian G. Peterson <brian@braverock.com>wrote:
>
>> '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@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
>>
>>
>

	[[alternative HTML version deleted]]

