Why not try a custom signal function that simply lags the signal by the
amount of periods?

Here's a demo I wrote to demonstrate:

sigLag <- function(label, data=mktdata, column, cross=FALSE, nLag=1){
  colNum <- match.names(column, colnames(data))
  ret_sig <- lag(data[,colNum], nLag)
  if (isTRUE(cross))
    ret_sig <- diff(ret_sig) == 1
  if (!missing(label))
    colnames(ret_sig) <- label
  return(ret_sig)
}

#data boilerplate

currency('USD')
Sys.setenv(TZ="UTC")

symbols <- c("SPY")

stock(symbols, currency="USD", multiplier=1)
getSymbols(symbols, src='yahoo',
           index.class=c("POSIXt","POSIXct"),
           from = "1998-12-01", adjust=TRUE)

#initialization boilerplate
rm(list=ls(.blotter), envir=.blotter)
initDate="1990-09-08"

strategy.st="sigLagger"
portfolio.st="sigLagger"
account.st="sigLagger"

rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD')
initOrders(portfolio.st, initDate=initDate)

#strategy
strategy(strategy.st, store=TRUE)

add.indicator(strategy.st, name="RSI",
arguments=list(price=quote(Cl(mktdata)), n=4), label="RSI")
add.signal(strategy.st, name="sigThreshold", arguments=list(column="RSI",
threshold=25, cross=TRUE), label="longEntry")
add.signal(strategy.st, name="sigLag", arguments=list(column="longEntry",
nLag=5, cross=FALSE), label="longExit")
add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longEntry",
sigval=TRUE, orderqty=100, ordertype="market",
                                                        orderside="long",
replace=FALSE, prefer="Open"), type="enter", path.dep=TRUE)
add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longExit",
sigval=TRUE, orderqty="all", ordertype="market",
                                                        orderside="long",
replace=FALSE, prefer="Open"), type="exit", path.dep=TRUE)

t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1)

######## ANALYTICS ##########

updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)

(tStats <- tradeStats(Portfolios = portfolio.st))


On Sat, May 3, 2014 at 5:16 PM, fc_11 <jyo...@gmail.com> wrote:

> i've been trying to implement a similar 'holding period based' exit in a
> similar fashion and tried Sergey's code today.  i get the following error
> upon running applyStrategy using either sergey's 'seller' function and rule
> or mine:
>
> Error in .firstCross(dindex, curIndex, "gt") :
>   REAL() can only be applied to a 'numeric', not a 'integer'
>
>
>
> my exit logic:
> add.rule(strategy.st, name = 'maxHoldingPeriod',arguments=list(x =
> quote(Cl(mktdata)[,1]),timestamp,portfolio.st,maxp=10),type='exit',
> enabled=TRUE)
>
>
>
> maxHoldingPeriod <- function (x, timestamp, portfolio, symbol,maxp)
> {
>   # updatePortf(portfolio, Symbols=symbol) #may or maynot work
>   position<-(getPos(portfolio,symbol,timestamp,Columns=c("Pos.Qty")))
>   position_init<-index(position)
>   pheld<-paste0(position_init, "/", timestamp)
>   period<-as.numeric(nrow(x[pheld]))
>   result<-as.xts(0,order.by=timestamp)
>   colnames(result)<-c('HoldingPeriod')
>   if(period>=maxp)
>   {
>     if(position[,1]>0)
>     {
>       result<-1
>       addOrder(portfolio, symbol, timestamp, qty='all', ordertype='market',
> side='long', prefer = 'Open',replace = TRUE,
>                return = FALSE, ..., TxnFees = 0, label = "MaxBarLong")
>     }
>     if(position[,1]<0)
>     {
>       result<-1
>       addOrder(portfolio, symbol, timestamp, qty='all', ordertype='market',
> side='short', prefer = 'Open',replace = TRUE,
>                return = FALSE, ..., TxnFees = 0, label = "MaxBarShort")
>     }
>   }
>   return(result)
> }
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Writing-sell-rules-with-quantstrat-tp4389599p4689930.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> 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.
>

        [[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.

Reply via email to