On a Sim account, when doing a quantity change with the Account.Change() call from within an Addon, if the position size is larger than the Risk template's allowed position size or there is insufficient margin available, the order state freezes on the ChangePending state.
Is there a proper way to check for and handle order failures like this? Nothing seems to come through in the handled Account_OrderUpdate() event.
Steps to reproduce the error:
1) Ensure the default NT Brokerage risk template is being used
2) Place a valid limit anywhere on the chart
3) Run the code below (changing the 'accountName' variable to match the account name used.
// Retrieve the account string accountName = "Sim202"; Account account = null; if (string.IsNullOrEmpty(accountName) == false) lock (Account.All) account = Account.All.FirstOrDefault(a => a.Name == accountName); if (account == null) { Print(string.Format("The specified account [{0}] could not be retrieved from the system.", accountName)); return; } // Find the first working entry order on the chart. Order order = null; for (int i = 0; i < account.Orders.Count; i++) { order = account.Orders[i]; if (order.OrderType == OrderType.Limit && order.OrderState == OrderState.Working) { Print(string.Format("Found active order [{0}]", order.ToString())); break; } } if (order == null) { Print("No active entry Limit order could be found."); return; } // We found our order. Update the quantity and submit. int newQuantity = int.Parse(testCaseInput); order.QuantityChanged = newQuantity; Print(string.Format("Updating order [{0}] with new position size of {1} for account {2}", order.Id, newQuantity, account.Name)); try { account.Change(new Order[] { order }); } catch (Exception ex) { Print(string.Format(" Exception occurred changing the order; Message: [{0}]", ex.Message)); } finally { Print("Change complete."); }
Comment