I am coding a strategy that uses ATM strategies to manage open positions, this strategy has to bracket the market, so naturally I am using the Unmanaged Order approach, and I would like the strategy to cancel the opposing entry order once any of them is hit. For this I thought about using the OnPositionUpdate function to listen for updates and cancel the orders when necessary.
This is where I ran into issues, if I submit the buy order first, then I submit the sell order, then the OnPositionUpdate method executes only for the sell order, and not for the buy order. If I change the order in which the orders are submitted, then only the last submitted order executes the OnPositionUpdate method.
My question is, how can I work around this to make it work. Below I have the code for the entry orders and the OnPositionUpdate code.
if(entry.PH[0] != 0){
if(loid.Length > 0 && entry.PH[0] != lP){
lP = entry.PH[0];
AtmStrategyChangeEntryOrder(0, entry.PH[0] + eT * TickSize, loid);
}else if(loid.Length == 0){
if(entry.PH[0] + eT * TickSize > Close[0]){
lP = entry.PH[0];
loid = GetAtmStrategyUniqueId();
LOID = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Stop, 0, entry.PH[0] + eT * TickSize, TimeInForce.Gtc, loid, buATM, LOID);
}
}
}
if(entry.PL[0] != 0){
if(soid.Length > 0 && entry.PL[0] != sP){
sP = entry.PL[0];
AtmStrategyChangeEntryOrder(0, entry.PL[0] - eT * TickSize, soid);
}else if(soid.Length == 0){
if(entry.PL[0] - eT * TickSize < Close[0]){
sP = entry.PL[0];
soid = GetAtmStrategyUniqueId();
SOID = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Sell, OrderType.Stop, 0, entry.PL[0] - eT * TickSize, TimeInForce.Gtc, soid, beATM, SOID);
}
}
}
protected override void OnPositionUpdate(IPosition position){
Print("exec all");
if(loid.Length != 0 && GetAtmStrategyMarketPosition(LOID) == MarketPosition.Long){
Print("exec L");
if(soid.Length != 0 && SOID.Length != 0)
CancelOrd(ref SOID, ref soid);
}else if(soid.Length != 0 && GetAtmStrategyMarketPosition(SOID) == MarketPosition.Short){
Print("exec S");
if(SOID.Length > 0 && soid.Length > 0)
CancelOrd(ref LOID, ref loid);
}
}

Comment