Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to Handle OrderState.ChangePending Freeze

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to Handle OrderState.ChangePending Freeze

    Hello!

    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.

    Code:
    // 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.");
    }


    #2
    Hello MarkWise,

    Thank you for your post.

    I would expect a rejection to occur in this scenario, however, I am able to reproduce the behavior you're seeing with the code above. As an aside, thank you for the clear steps to reproduce, that was extremely helpful!

    I'm going to reach out to our development team to see if this is expected or not and what if any attempts you can make to catch that stuck order before it sticks. I'll let you know what they say.

    Thanks in advance for your patience; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thanks Kate!

      One of the reasons I asked about this, and perhaps I should have mentioned this originally, but there is no ErrorCode value set in the Account_OrderUpdate event arguments. Even if the order is changed to a 0 quantity, ErrorCode still equals ErrorCode.NoError.

      In our event handler, we have code similar to the code below, and it NEVER triggers. I find this a bit problematic, since we can't test our bad orders in the Sim accounts.

      Code:
      if (e.Error != ErrorCode.NoError)
      {
          Print(String.Format("WARNING: Processed order returned with an error: {0}; Order: {1}", e.Error, e.Order.ToString()));
      }

      Comment


        #4
        Hello Mark,

        Kate is out of the office at this time.

        I see similar behavior where if an order is changed to a quantity of 0, we end up with a stuck order instead of an order error. This situation should not happen in one's code, but I also see where an UnableToChangeOrder error would be expected.

        I've brought that up as a separate ticket which we will also follow up with here.
        Last edited by NinjaTrader_Jim; 03-28-2022, 09:16 AM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jclose, Today, 09:37 PM
        0 responses
        6 views
        0 likes
        Last Post jclose
        by jclose
         
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        10 responses
        1,414 views
        0 likes
        Last Post Traderontheroad  
        Started by firefoxforum12, Today, 08:53 PM
        0 responses
        11 views
        0 likes
        Last Post firefoxforum12  
        Started by stafe, Today, 08:34 PM
        0 responses
        11 views
        0 likes
        Last Post stafe
        by stafe
         
        Started by sastrades, 01-31-2024, 10:19 PM
        11 responses
        169 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X