[R-SIG-Finance] Writing sell rules with quantstrat
Joshua Ulrich
josh.m.ulrich at gmail.com
Sun May 4 16:33:58 CEST 2014
On Sat, May 3, 2014 at 4:16 PM, fc_11 <jyorio at 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'
>
Thanks for the report. Patched in r1608 on R-Forge.
>
>
> 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)
> }
>
>
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
More information about the R-SIG-Finance
mailing list