4

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.

Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21
SilverSpoon
  • 655
  • 4
  • 8
  • 17
  • this functionality will be covered by 'order sets'. I've got code to use order sets to provide OCO (one cancels other) order functionality, but it needs testing before committing. I'll provide a formal answer once there is a svn rev that contains the code. – Brian G. Peterson May 04 '12 at 11:20

1 Answers1

9

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.

Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21
  • thx Brian. However, would like to ask you how to configure so that only when enter signals get filled, stop loss and take profit orders will be placed in case i want to change my enter orders to limit orders or they arent executed due to position limit. – SilverSpoon May 07 '12 at 10:58
  • ive modified ruleOrderProc and obtained my requirements. A few bugs in this file. – SilverSpoon May 08 '12 at 08:03