refill:
attaching order <-> oca discussion.
as i said, attaching orders (that is by providing parent order id for function place_order) makes TWS to create an oca group implicitly. you can easily check out this in TWS -> open Order Ticket.
so what happens if you do it like here:
//Create buy order
BuyOrderID = TWSLink.Place_Order(ID, 0,"BUY","LMT",LastValue(Shares),LastValue(LimitPrice),0.0,"Day",0,0,"","", 0);
//Create a STOP ORDER. Create on TWS Side only.
StopOrderID = TWSLink.Place_Order(ID,0,"SELL","STP",LastValue(Shares),0.0,LastValue(Stop),"GTC",0,BuyOrderID,"","",0);
//Create the PROFIT ORDER. Create on TWS Side only.
TargetOrderID = TWSLink.Place_ORder(ID,0,"SELL","LMT",LastValue(Shares),LastValue(Target),0,"GTC",0,BuyOrderID,"","",0);
// THIS OVERWRITES OCA group and is no good idea. check oca field in TWS of stop and target order before executing lines below.
bret = TWSLink.SET_ORDERVAL (TargetOrderID,19,OCA,0);
bret = TWSLink.SET_ORDERVAL (StopOrderID,19,OCA,0);
How to do correctly:
--------------------
[1] Don't attach, use oca only for SELL orders:
(cancel one of the sell orders will cancel the other sell order. cancel buy order will ONLY cancel buy order (because buy order in no oca group))
//Create buy order
BuyOrderID = TWSLink.Place_Order(ID, 0,"BUY","LMT",LastValue(Shares),LastValue(LimitPrice),0.0,"Day",1,0,"","", 0);
//Create a STOP ORDER. Create on TWS Side only.
StopOrderID = TWSLink.Place_Order(ID,0,"SELL","STP",LastValue(Shares),0.0,LastValue(Stop),"GTC",0,0,"","",0);
//Create the PROFIT ORDER. Create on TWS Side only.
TargetOrderID = TWSLink.Place_ORder(ID,0,"SELL","LMT",LastValue(Shares),LastValue(Target),0,"GTC",0,0,"","",0);
// set target and stop in oca (cancelling buy would here not affect stop and target)
bret = TWSLink.SET_ORDERVAL (TargetOrderID,19,OCA,1);
bret = TWSLink.SET_ORDERVAL (StopOrderID,19,OCA,1);
ostatus = TWSLink.WAIT_FOR_ORDER_STATUS_RNG( TargetOrderID,5,8,3000,12) ;
[2] Don't attach, use oca for all orders:
(cancel any order will cancel all other orders)
//Create buy order
BuyOrderID = TWSLink.Place_Order(ID, 0,"BUY","LMT",LastValue(Shares),LastValue(LimitPrice),0.0,"Day",0,0,"","", 0);
//Create a STOP ORDER. Create on TWS Side only.
StopOrderID = TWSLink.Place_Order(ID,0,"SELL","STP",LastValue(Shares),0.0,LastValue(Stop),"GTC",0,0,"","",0);
//Create the PROFIT ORDER. Create on TWS Side only.
TargetOrderID = TWSLink.Place_ORder(ID,0,"SELL","LMT",LastValue(Shares),LastValue(Target),0,"GTC",0,0,"","",0);
// set target and stop in oca (cancelling buy would here not affect stop and target)
bret = TWSLink.SET_ORDERVAL (BuyOrderID,19,OCA,1);
bret = TWSLink.SET_ORDERVAL (TargetOrderID,19,OCA,1);
bret = TWSLink.SET_ORDERVAL (StopOrderID,19,OCA,1);
ostatus = TWSLink.WAIT_FOR_ORDER_STATUS_RNG( TargetOrderID,5,8,3000,12) ;
[3] Attach , don't use oca:
(cancel one of the sell orders will cancel the other sell order;
cancel buy order cancels all orders)
//Create buy order
BuyOrderID = TWSLink.Place_Order(ID, 0,"BUY","LMT",LastValue(Shares),LastValue(LimitPrice),0.0,"Day",0,0,"","", 0);
//Create a STOP ORDER. Create on TWS Side only.
StopOrderID = TWSLink.Place_Order(ID,0,"SELL","STP",LastValue(Shares),0.0,LastValue(Stop),"GTC",0,BuyOrderID,"","",0);
//Create the PROFIT ORDER. Create on TWS Side only.
// NOTE: the paramvalue after "GTC" is 1, what means transmit to IB. this will transmit all orders of implicit oca group
// as well.
TargetOrderID = TWSLink.Place_ORder(ID,0,"SELL","LMT",LastValue(Shares),LastValue(Target),0,"GTC",1,BuyOrderID,"","",0);
ostatus = TWSLink.WAIT_FOR_ORDER_STATUS_RNG( TargetOrderID,5,8,3000,12) ;
depending on what you want, use attachments or oca.