Search code examples
rquantmodquantstrat

Quantstrat: trades execution problems.


So I am trying to just test simple strategy, when open goes crosses moving average up buy, vise versa; Signal seems to work fine with View(mktdata) but for some reason it's not executing the trades. Any insights?

rm(list=ls(.blotter), envir=.blotter)



rm.strat(strategy.st)
strategy.st<-"firststrat"
portfolio.st<-"firststrat"
account.st<-"firststrat"


#assignsymbol
getSymbols("SPY",auto.assign=TRUE,adjust=TRUE)

initdate<-"2009-01-01"
from<-"2010-01-01"
to<-"2016-11-01"
Sys.setenv(TZ="UTC")
currency("USD")
stock("SPY",currency="USD",multiplier=1)
tradesize<-10000
inieq<-100000

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



add.indicator(strategy = strategy.st,name="EMA",arguments=list(x=quote(Cl(mktdata)),n=50),label="EMA50")
#if closing price goes over moving average 50 and TSi fference is less then 0.15, then long
#short when closing price touches below original closing price by x(depends on atr? previous lows?) 


add.signal(strategy.st,name="sigCrossover",
           arguments = list(columns=c("Close","EMA50"),
                            relationship="gt"),
                       label="crossentry"    
                            )
add.signal(strategy.st,name="sigCrossover",
           arguments = list(columns=c("Close","EMA50"),
                            relationship="lt"),
           label="crossexit"    
)


add.rule(strategy.st,name="ruleSignal",
         arguments=list(sigcol = "crossentry",
                        sigval=TRUE,
                        orderqty="all",
                        ordertype="market",
                        orderside="long",
                        replace=FALSE,
                        prefer="Open",
                        path.dep=TRUE
                        ),
         type="enter"
         )           

add.rule(strategy.st,name="ruleSignal",
         arguments=list(sigcol = "crossexit",
                        sigval=TRUE,
                        orderqty="all",
                        ordertype="market",
                        orderside="long",
                        replace=FALSE,
                        prefer="Open",
                        path.dep=TRUE
         ),
         type="exit"
)           


out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)

View(mktdata)

> out
NULL

Solution

  • You need to specify an orderqty value indicating the size of each trade for market orders to enter positions (not "all", which can be used for exits, stops, take profits). Otherwise the strategy doesn't know how what the trade sizes should actually be.

    You code should run if you modify your entry rule like this:

    add.rule(strategy.st,name="ruleSignal",
             arguments=list(sigcol = "crossentry",
                            sigval=TRUE,
                            orderqty= 100,  # numeric qty value
                            ordertype="market",
                            orderside="long",
                            replace=FALSE,
                            prefer="Open",
                            path.dep=TRUE
             ),
             type="enter"
    )