[R-SIG-Finance] Writing sell rules with quantstrat

fc_11 jyorio at gmail.com
Sat May 3 23:16:52 CEST 2014


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.



More information about the R-SIG-Finance mailing list