Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - quantstrat orders cancel each other

Tags:

r

quantstrat

How to enter orders that cancel each other in quantstrat? For example, once I enter a trade, I immediately open two orders: "stop loss" and "take profit". Once one gets filled, the other will be cancelled.

#Enter signal 

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE, orderqty=100,
                                    ordertype="market", orderside="long", 
                                    pricemethod="market", osFUN=osMaxPos),
                     type="enter", path.dep=TRUE)

#Stop loss

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE, 
                                    orderqty="all", ordertype="stoplimit", 
                                    orderside="short", threshold=-5, 
                                    tmult=FALSE), 
                     type="exit", path.dep=TRUE)

#Take profit

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE,
                                    orderqty="all", ordertype="stoplimit", 
                                    orderside="short", threshold=5, 
                                    tmult=FALSE),
                     type="exit", path.dep=TRUE) 

Currently, they are working independently.

like image 996
SilverSpoon Avatar asked Jan 16 '23 18:01

SilverSpoon


1 Answers

SVN r 1010 adds code to quantstrat to make it easier to use OCO (One Cancels Other) order sets. There is an example in the 'macd' demo here which uses the newly exposed orderset parameter to provide OCO functionality on the exit orders.

You will need to be using current svn (r1010 or later) to use this functionality. I'll also note that the order sizing functionality is a bit broken right now, we're working on it.

Your example, to use order sets, would look something like this:

#Enter signal 

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE, orderqty=100,
                                    ordertype="market", orderside="long", 
                                    pricemethod="market", osFUN=osMaxPos),
                     type="enter", path.dep=TRUE)

#Stop loss

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE, 
                                    orderqty="all", ordertype="stoplimit", 
                                    orderside="long", threshold=-5, 
                                    tmult=FALSE, orderset='altexits'), 
                     type="exit", path.dep=TRUE)

#Take profit

strategy <- add.rule(strategy, name="ruleSignal", 
                     arguments=list(sigcol="EnterBuy", sigval=TRUE,
                                    orderqty="all", ordertype="stoplimit", 
                                    orderside="long", threshold=5, 
                                    tmult=FALSE, orderset='altexits'),
                     type="exit", path.dep=TRUE) 

Note the addition of the orderset='altexits' parameter to the arguments list for ruleSignal.

like image 154
Brian G. Peterson Avatar answered Jan 19 '23 07:01

Brian G. Peterson