[R-SIG-Finance] "stoplimit" orders for the "short" side

mattw30030 mwitt at belvederetrading.com
Wed Dec 26 18:48:46 CET 2012


I've been able to get 'stoplimit' orders to work on the "long" side of the
trade, however, quantstrat seems to be treating 'stoplimit'  orders
differently on the "short" side.  
 Looking through the orderbook, it appears 'stoplimit' orders on the "short"
side are not being "canceled", as the "short" trade exits but, but are be
"closed" , thus executing at the 'stoplimit' price rather  than the 'exit'
price generated by the 'exit' rule.  

Here is the code I am running on 60 minute data:

dummy <- currency('USD')
stock.str = c("spy15min")
for(symbol in stock.str)
  stock(symbol, currency="USD",multiplier=1)

start.data <- as.Date("2011-12-27")
end.data <- as.Date("2011-12-27")
initDate <- start.data-1

# inz portfolio, account, orders, strategy
 strat.name <- "MACD"
initEq=1000000

dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate)
dummy <- initAcct(name=strat.name,portfolios=strat.name,
                  initDate=initDate, initEq=initEq)
initOrders(portfolio=strat.name,initDate=initDate)
strat <- strategy(strat.name)
addPosLimit("MACD", "spy15min", timestamp=initDate, maxpos=100, minpos=-100)


# indicators:
 strat <- add.indicator(strategy = strat, name = "MACD",
                         arguments = list(x=quote(Cl(mktdata))) 

# signals:
strat <- add.signal(strategy = strat, name="sigThreshold",
            arguments =
list(column="signal",relationship="gt",threshold=0,cross=TRUE),
            label="signal.gt.zero")

strat <- add.signal(strategy = strat,name="sigThreshold",
                 arguments =
list(column="signal",relationship="lt",threshold=0,cross=TRUE),
                    label="signal.lt.zero")


# rules:

strat <- add.rule(strategy = strat,name='ruleSignal',
          arguments = list(sigcol="signal.gt.zero",sigval=TRUE,
orderqty=100,
            ordertype='market', orderside='long', osFUN=osMaxPos
),type='enter')

strat <- add.rule(strategy = strat,name='ruleSignal',
          arguments = list(sigcol="signal.lt.zero",sigval=TRUE,
orderqty=-100,
            ordertype='market', orderside='short',
osFUN=osMaxPos),type='enter')

strat <- add.rule(strategy = strat,name='ruleSignal', arguments =
list(sigcol="signal.gt.zero",sigval=TRUE, orderqty='all', 
            ordertype='stoplimit',orderside='long',  threshold=
-.1,tmult=TRUE, 
              orderset='exit2',replace=TRUE),type='exit',label='risk')

strat <- add.rule(strategy = strat,name='ruleSignal', arguments =
list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', 
            ordertype='stoplimit',orderside='short',  threshold=
.1,tmult=TRUE, 
              orderset='exit2',replace=TRUE),type='exit',label='risk')


strat <- add.rule(strategy = strat,name='ruleSignal',
           arguments = list(sigcol="signal.lt.zero",sigval=TRUE,
orderqty='all',
             ordertype='market', orderside='long'),type='exit')

strat <- add.rule(strategy = strat,name='ruleSignal',
                  arguments = list(sigcol="signal.gt.zero",sigval=TRUE,
orderqty='all',
                    ordertype='market', orderside='short'),type='exit')


# parameters:

fastMA = 12
slowMA = 26
signalMA = 9
maType="EMA"

# apply strategy

out<-try(applyStrategy(strategy=strat, portfolios=strat.name,
        parameters=list(nFast=fastMA, nSlow=slowMA,
nSig=signalMA,maType=maType),
          verbose=F))

getTxns(Portfolio=strat.name, Symbol=stock.str)

getOrderBook('MACD')

############

And here is the output from the orderbook:

$MACD$spy15min
                    Order.Qty Order.Price Order.Type  Order.Side
Order.Threshold Order.Status
2011-12-25 18:00:00 "0"       NA          "init"      "long"     "0"            
"closed"    
2012-01-30 11:00:00 "all"     "143.803"   "stoplimit" "short"    "13.073"       
"closed"    
2012-01-30 11:00:00 "-100"    "130.73"    "market"    "short"    NA             
"closed"    
2012-02-01 12:00:00 "all"     "119.61"    "stoplimit" "long"     "-13.29"       
"canceled"  
2012-02-01 12:00:00 "100"     "132.9"     "market"    "long"     NA             
"closed"    
2012-03-05 12:00:00 "all"     "150.007"   "stoplimit" "short"    "13.637"       
"closed"    
2012-03-05 12:00:00 "-100"    "136.37"    "market"    "long"     NA             
"closed"    
2012-03-05 12:00:00 "-100"    "136.37"    "market"    "short"    NA             
"canceled"  
2012-03-09 09:00:00 "all"     "123.516"   "stoplimit" "long"     "-13.724"      
"canceled"  
2012-03-09 09:00:00 "100"     "137.24"    "market"    "short"    NA             
"closed"    
2012-03-09 09:00:00 "100"     "137.24"    "market"    "long"     NA             
"canceled"  
2012-03-22 12:00:00 "all"     "153.318"   "stoplimit" "short"    "13.938"       
"closed"    
2012-03-22 12:00:00 "-100"    "139.38"    "market"    "short"    NA             
"closed"    
2012-03-26 13:00:00 "all"     "126.9"     "stoplimit" "long"     "-14.1"        
"canceled"  
2012-03-26 13:00:00 "100"     "141"       "market"    "long"     NA             
"closed"    
2012-03-29 11:00:00 "all"     "153.142"   "stoplimit" "short"    "13.922"       
"closed"    
2012-03-29 11:00:00 "-100"    "139.22"    "market"    "long"     NA             
"closed"    
2012-03-29 11:00:00 "-100"    "139.22"    "market"    "short"    NA             
"canceled"  
2012-04-02 10:00:00 "all"     "127.107"   "stoplimit" "long"     "-14.123"      
"canceled"  
2012-04-02 10:00:00 "100"     "141.23"    "market"    "short"    NA             
"closed"    
2012-04-02 10:00:00 "100"     "141.23"    "market"    "long"     NA             
"canceled"  
2012-04-04 13:00:00 "all"     "153.725"   "stoplimit" "short"    "13.975"       
"closed"    
2012-04-04 13:00:00 "-100"    "139.75"    "market"    "short"    NA             
"closed"    
2012-04-17 14:00:00 "all"     "125.298"   "stoplimit" "long"     "-13.922"      
"canceled"  
2012-04-17 14:00:00 "100"     "139.22"    "market"    "long"     NA             
"closed"    
2012-04-23 09:00:00 "all"     "149.919"   "stoplimit" "short"    "13.629"       
"closed"    
2012-04-23 09:00:00 "-100"    "136.29"    "market"    "long"     NA             
"closed"    
2012-04-23 09:00:00 "-100"    "136.29"    "market"    "short"    NA             
"canceled"  
2012-04-25 14:00:00 "all"     "125.199"   "stoplimit" "long"     "-13.911"      
"canceled"  
2012-04-25 14:00:00 "100"     "139.11"    "market"    "short"    NA             
"closed"    
2012-04-25 14:00:00 "100"     "139.11"    "market"    "long"     NA             
"canceled"  
2012-05-04 09:00:00 "all"     "151.888"   "stoplimit" "short"    "13.808"       
"closed"    
2012-05-04 09:00:00 "-100"    "138.08"    "market"    "short"    NA             
"closed"    
2012-05-25 11:00:00 "all"     "119.376"   "stoplimit" "long"     "-13.264"      
"canceled"  
2012-05-25 11:00:00 "100"     "132.64"    "market"    "long"     NA             
"closed"    
2012-05-31 10:00:00 "all"     "143.682"   "stoplimit" "short"    "13.062"       
"closed"    
2012-05-31 10:00:00 "-100"    "130.62"    "market"    "long"     NA             
"closed"    
2012-05-31 10:00:00 "-100"    "130.62"    "market"    "short"    NA             
"canceled"  
2012-06-06 15:00:00 "all"     "118.818"   "stoplimit" "long"     "-13.202"      
"canceled"  
2012-06-06 15:00:00 "100"     "132.02"    "market"    "short"    NA             
"closed"    
2012-06-06 15:00:00 "100"     "132.02"    "market"    "long"     NA             
"canceled"  
2012-06-22 10:00:00 "all"     "146.168"   "stoplimit" "short"    "13.288"       
"closed"    
2012-06-22 10:00:00 "-100"    "132.88"    "market"    "short"    NA             
"closed"    
2012-06-29 11:00:00 "all"     "121.878"   "stoplimit" "long"     "-13.542"      
"canceled"  
2012-06-29 11:00:00 "100"     "135.42"    "market"    "long"     NA             
"closed"    
2012-07-09 13:00:00 "all"     "148.742"   "stoplimit" "short"    "13.522"       
"closed"    
2012-07-09 13:00:00 "-100"    "135.22"    "market"    "long"     NA             
"closed"    
2012-07-09 13:00:00 "-100"    "135.22"    "market"    "short"    NA             
"canceled"  
2012-07-16 10:00:00 "all"     "121.851"   "stoplimit" "long"     "-13.539"      
"canceled"  
2012-07-16 10:00:00 "100"     "135.39"    "market"    "short"    NA             
"closed"    
2012-07-16 10:00:00 "100"     "135.39"    "market"    "long"     NA             
"canceled"  
2012-07-23 12:00:00 "all"     "148.28"    "stoplimit" "short"    "13.48"        
"closed"    
2012-07-23 12:00:00 "-100"    "134.8"     "market"    "short"    NA             
"closed"    
2012-07-27 10:00:00 "all"     "123.786"   "stoplimit" "long"     "-13.754"      
"canceled"  
2012-07-27 10:00:00 "100"     "137.54"    "market"    "long"     NA             
"closed"    
2012-08-02 14:00:00 "all"     "149.798"   "stoplimit" "short"    "13.618"       
"closed"    
2012-08-02 14:00:00 "-100"    "136.18"    "market"    "long"     NA             
"closed"    
2012-08-02 14:00:00 "-100"    "136.18"    "market"    "short"    NA             
"canceled"  
2012-08-03 12:00:00 "all"     "125.487"   "stoplimit" "long"     "-13.943"      
"canceled"  
2012-08-03 12:00:00 "100"     "139.43"    "market"    "short"    NA             
"closed"    
2012-08-03 12:00:00 "100"     "139.43"    "market"    "long"     NA             
"canceled"  
2012-08-23 10:00:00 "all"     "155.298"   "stoplimit" "short"    "14.118"       
"closed"    
2012-08-23 10:00:00 "-100"    "141.18"    "market"    "short"    NA             
"closed"    
2012-08-28 09:00:00 "all"     "127.026"   "stoplimit" "long"     "-14.114"      
"canceled"  
2012-08-28 09:00:00 "100"     "141.14"    "market"    "long"     NA             
"closed"    
2012-08-30 10:00:00 "all"     "154.429"   "stoplimit" "short"    "14.039"       
"closed"    
2012-08-30 10:00:00 "-100"    "140.39"    "market"    "long"     NA             
"closed"    
2012-08-30 10:00:00 "-100"    "140.39"    "market"    "short"    NA             
"canceled"  
2012-09-06 10:00:00 "all"     "129.141"   "stoplimit" "long"     "-14.349"      
"canceled"  
2012-09-06 10:00:00 "100"     "143.49"    "market"    "short"    NA             
"closed"    
2012-09-06 10:00:00 "100"     "143.49"    "market"    "long"     NA             
"canceled"  
2012-09-24 09:00:00 "all"     "159.896"   "stoplimit" "short"    "14.536"       
"closed"    
2012-09-24 09:00:00 "-100"    "145.36"    "market"    "short"    NA             
"closed"    
2012-10-03 13:00:00 "all"     "130.446"   "stoplimit" "long"     "-14.494"      
"canceled"  
2012-10-03 13:00:00 "100"     "144.94"    "market"    "long"     NA             
"closed"    
2012-10-09 13:00:00 "all"     "158.983"   "stoplimit" "short"    "14.453"       
"closed"    
2012-10-09 13:00:00 "-100"    "144.53"    "market"    "long"     NA             
"closed"    
2012-10-09 13:00:00 "-100"    "144.53"    "market"    "short"    NA             
"canceled"  
2012-10-16 13:00:00 "all"     "130.986"   "stoplimit" "long"     "-14.554"      
"canceled"  
2012-10-16 13:00:00 "100"     "145.54"    "market"    "short"    NA             
"closed"    
2012-10-16 13:00:00 "100"     "145.54"    "market"    "long"     NA             
"canceled"  
2012-10-19 15:00:00 "all"     "157.674"   "stoplimit" "short"    "14.334"       
"closed"    
2012-10-19 15:00:00 "-100"    "143.34"    "market"    "short"    NA             
"closed"    
2012-11-01 14:00:00 "all"     "128.538"   "stoplimit" "long"     "-14.282"      
"canceled"  
2012-11-01 14:00:00 "100"     "142.82"    "market"    "long"     NA             
"closed"    
2012-11-05 14:00:00 "all"     "155.859"   "stoplimit" "short"    "14.169"       
"closed"    
2012-11-05 14:00:00 "-100"    "141.69"    "market"    "long"     NA             
"closed"    
2012-11-05 14:00:00 "-100"    "141.69"    "market"    "short"    NA             
"canceled"  
2012-11-06 11:00:00 "all"     "128.88"    "stoplimit" "long"     "-14.32"       
"canceled"  
2012-11-06 11:00:00 "100"     "143.2"     "market"    "short"    NA             
"closed"    
2012-11-06 11:00:00 "100"     "143.2"     "market"    "long"     NA             
"canceled"  
2012-11-07 12:00:00 "all"     "153.978"   "stoplimit" "short"    "13.998"       
"closed"    
2012-11-07 12:00:00 "-100"    "139.98"    "market"    "short"    NA             
"closed"    
2012-11-19 15:00:00 "all"     "125.199"   "stoplimit" "long"     "-13.911"      
"canceled"  
2012-11-19 15:00:00 "100"     "139.11"    "market"    "long"     NA             
"closed"    
2012-12-05 11:00:00 "all"     "155.826"   "stoplimit" "short"    "14.166"       
"closed"    
2012-12-05 11:00:00 "-100"    "141.66"    "market"    "long"     NA             
"closed"    
2012-12-05 11:00:00 "-100"    "141.66"    "market"    "short"    NA             
"canceled"  
2012-12-06 09:00:00 "all"     "127.314"   "stoplimit" "long"     "-14.146"      
"canceled"  
2012-12-06 09:00:00 "100"     "141.46"    "market"    "short"    NA             
"closed"    
2012-12-06 09:00:00 "100"     "141.46"    "market"    "long"     NA             
"canceled"  
2012-12-14 13:00:00 "all"     "156.662"   "stoplimit" "short"    "14.242"       
"closed"    
2012-12-14 13:00:00 "-100"    "142.42"    "market"    "short"    NA             
"closed"    
2012-12-18 09:00:00 "all"     "129.483"   "stoplimit" "long"     "-14.387"      
"canceled"  
2012-12-18 09:00:00 "100"     "143.87"    "market"    "long"     NA             
"closed"    
2012-12-21 13:00:00 "all"     "156.871"   "stoplimit" "short"    "14.261"       
"closed"    
2012-12-21 13:00:00 "-100"    "142.61"    "market"    "long"     NA             
"closed"    
2012-12-21 13:00:00 "-100"    "142.61"    "market"    "short"    NA             
"canceled"  


Any thoughts?

Thanks,

Matt




--
View this message in context: http://r.789695.n4.nabble.com/stoplimit-orders-for-the-short-side-tp4653968.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list