[R-SIG-Finance] quanstrat exit rules

Jon Golenbock jongolenbock at gmail.com
Tue Apr 18 16:31:59 CEST 2017


Hi, trying to get a handle on quantstrat and was hoping somebody could
explain the what I'm doing wrong.

The strategy itself is simple enough (and just for testing purposes - buy
when RSI goes below 30, sell back out when RSI goes above 70.

What is unintended, is that short trades (exit) are processed before long
trades (entry) ever are. Why is this happening?

Also, I was playing around with order sizing - it seems like one should be
able to specify "all" for exit trades, but that doesn't seem to work for me
when I make the substitution in the below code... the issues may be
related, though, so I am putting this on the back burner for now.

Any help appreciated, and thanks in advance.

Jon

----

rm(list=ls(.blotter), envir=.blotter)
rm(list=ls(.strategy), envir=.strategy)
.blotter <- new.env()
.strategy <- new.env()




initEq <- 5000
n <- 14
nSMA <- 200
days_count <- 100
initDate <- Sys.Date()-days(days_count + 1)
.orderqty = 100





symbols <- getSymbols(c("AAPL","QQQ"))


currency("USD")
Sys.setenv(TZ="UTC")
stock(symbols, currency="USD", multiplier=1)

strategy.st <- 'teststrat'
portfolio.st <- 'teststrat'
account.st <- 'teststrat'
initPortf(portfolio.st, symbols = symbols, initDate=initDate,currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
initEq=initEq, currency='USD')
initOrders(portfolio=portfolio.st, initDate=initDate)



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







add.indicator(
  strategy = strategy.st,
  name = 'RSI',
  arguments = list(price=quote(Cl(mktdata)),n=n),
  label = "rsi")


add.signal(
  strategy = strategy.st,
  name = 'sigThreshold',
  arguments=list(
    threshold = 30,
    column = "rsi",
    relationship="lt",
    cross = TRUE),
  label = "RSI_long"
)

add.signal(
  strategy = strategy.st,
  name = 'sigThreshold',
  arguments=list(
    threshold = 70,
    column = "rsi",
    relationship="gt",
    cross = TRUE),
  label = "RSI_short"
)



add.rule(
  strategy=strategy.st,
  name='ruleSignal',
  arguments=list(
    sigcol='RSI_long',
    sigval = TRUE,
    orderside = 'long',
    ordertype='market',
    orderqty=.orderqty,
    replace=FALSE
  ),
  type='enter',
  label='Enterlong',
  path.dep = TRUE
)

add.rule(
  strategy=strategy.st,
  name='ruleSignal',
  arguments=list(
    sigcol='RSI_short',
    sigval = TRUE,
    orderside = 'short',
    ordertype='market',
    orderqty=75,
    replace=TRUE
  ),
  type="exit",
  label='Exitlong',
  path.dep = TRUE

)


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

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list