[R-SIG-Finance] Quantstrat code works for long position but not short position

Damon Verial verial.damon at gmail.com
Tue Dec 15 09:51:07 CET 2015


So I have this code that works fine for long positions. However, when I
reversed the code to make a short version, it breaks, giving me an error of:

Error in osFUN(strategy = strategy, data = mktdata, timestamp = timestamp,
:
  ATR corresponding to atrX is invalid at this point in time.
               Add a logical operator to account for this.

Why does this occur with the short position but not the long position? The
code follows, for reference:

library("quantstrat")
require(IKTrading)

initDate<-startDate<-'2015-01-01'
endDate<-'2015-12-09'

rm(list = ls(.blotter), envir = .blotter)
currency("USD")
Sys.setenv(TZ = "UTC")
symbols<-"SPY"

strategy.st <- portfolio.st <- account.st <- "Gaps"
getSymbols(symbols, src="yahoo",
index.class=c("POSIXt","POSIXct"),from=startDate,
to=endDate, adjust=T)
stock(symbols,currency="USD",multiplier=1)
tradeSize <- 100000
initEq <- tradeSize*length(symbols)

strategy.st <- portfolio.st <- account.st <- "Gaps"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)


period=10
pctATR=.1

####################
## UP GAPS ##
####################

#necessary functions

gap <- function(x,k) {
 low <- Lo(x)
 high<- Hi(x)
 yesHigh <- lag.xts(high,k=1)
 gapDay <- low > yesHigh
 gapDay[1] <- 0
 gaps <- x[gapDay==1,]
 gaps$takeProfit <- yesHigh[gapDay==1,]
 gaps<-cbind(gapDay,gaps$takeProfit)
 gaps$takeProfit<-na.locf(gaps$takeProfit)
 colnames(gaps) <- c("gap","TP")
 return(gaps)
}


period=10
pctATR=.1


#indicators

add.indicator(strategy.st, name="lagATR",
              arguments=list(HLC=quote(HLC(mktdata)),
                             n=period),
              label="atrX")

add.indicator(strategy.st, name="gap",
              arguments=list(x=quote(HLC(mktdata))),
              label="gap")


test <- applyIndicators(strategy.st, mktdata=SPY)
tail(test, 50)

#signals
add.signal(strategy.st, name="sigThreshold",
           arguments=list(column="gap.gap", threshold=.5,
                          relationship="gt", cross=TRUE),
           label="shortEntry")

test <- applySignals(strategy.st, test)
tail(test,50)

#Add rules
#rules
add.rule(strategy.st, name="ruleSignal",
         arguments=list(sigcol="shortEntry",
                        sigval=TRUE,
                        ordertype="market",
                        orderside="short",
                        replace=FALSE,
                        osFUN=osDollarATR,
                        tradeSize=tradeSize,
                        prefer="Open",
                        pctATR=pctATR,
                        atrMod="X",
                        orderset="orders"),
         type="enter", path.dep=TRUE,
         label="gapEntry")

add.rule(strategy.st, name="ruleSignal",
         arguments=list(sigcol="shortEntry",
                        sigval=TRUE,
                        ordertype="limit",
                        orderside="short",
                        replace=FALSE,
                        orderqty='all',
                        order.price=quote(mktdata$TP.gap[timestamp]),
                        orderset="orders"),
         type="chain",
         parent="gapEntry",
         label="takeProfitShort",
         path.dep=TRUE)


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

Damon Verial
Office: (206) 395-3688
Skype: DamonVerial
LinkedIn: https://www.linkedin.com/profile/view?id=175736313
A Seasonal Healthcare Portfolio
<http://seekingalpha.com/article/3693506-a-seasonal-healthcare-portfolio-using-vht>
I purposely keep my emails to 5 sentences or fewer - which is a miracle for
a writer.
I do this to show that I respect both your time and mine.

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list