Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ChangeOrder error handling?

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

    ChangeOrder error handling?

    How do I catch and handle errors when calling ChangeOrder?

    I tried wrapping in a try/catch and it doesn't work.

    My strategy sometimes causes error "can't change stop price below market" when trying to move SL to breakeven.
    I get the error popup window, but seems there's no way for my strategy to be aware of whether the ChangeOrder failed or succeeded.

    Update: I have added an OnOrderUpdate where I try to catch the rejection and any errors.. But still doesn't work
    Last edited by iantriestrading; 05-01-2025, 02:29 PM.

    #2
    Hello iantriestrading,,

    Any errors when doing a change order will be a seperate order event, that can be observed using OnOrderUpdate. You won't be able to catch rejections using the default script settings, it will disable before the event gets to the strategy, if you feel confident that you can handle all types of rejections in code you can disable the rejection handling.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    You should otherwise try to change the logic which is changing the order to avoid placing it at an incorrect price before doing the change order request.

    Comment


      #3
      Hi Jesse,

      I am already using RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors in my strategy.

      But I can't catch the error in OnOrderUpdate.

      Is there any example OnOrderUpdate that catches order rejections and errors? I do'nt get it

      Comment


        #4
        Hello iantriestrading,

        There are not any samples for that besides the one in the sample I linked to, you would just have to use the passed in order event. How are you currently trying to catch the error? Try catch wont work, you need to observe the order event from the override.

        Comment


          #5
          Hi Jesse,

          I was able to get the error information just now:


          4/29/2025 10:10:00 AM: OnOrderUpdate:: Stop order update - State: ChangePending, Stop price: 19560.75, Error: NoError, Native error:
          4/29/2025 10:10:00 AM: OnOrderUpdate:: Stop order update - State: ChangeSubmitted, Stop price: 19576.50, Error: NoError, Native error:
          4/29/2025 10:10:00 AM: OnOrderUpdate:: Stop order update - State: Accepted, Stop price: 19560.75, Error: UnableToChangeOrder, Native error: Stop price can't be changed above the market.

          Here's the OnOrderUpdate:
          Code:
          protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice,
          OrderState orderState, DateTime time, ErrorCode error, string nativeError)
          {
          // Check if this is our stop loss order
          bool isStopOrder = order.OrderType == OrderType.StopMarket &&
          order.Name == stopOrder?.Name;
          
          if (isStopOrder)
          {
          PrintDebugInfo($"Stop order update - State: {orderState}, Stop price: {stopPrice:F2}, Error: {error}, Native error: {nativeError}");
          
          switch (orderState)
          {
          case OrderState.Rejected:
          case OrderState.CancelPending when error != ErrorCode.NoError:
          case OrderState.ChangePending when error != ErrorCode.NoError:
          case OrderState.ChangeSubmitted when error != ErrorCode.NoError:
          // Handle any rejection or error state
          PrintDebugInfo($"Stop loss order error - State: {orderState}, Error: {error}");
          PrintDebugInfo($"Error details: {nativeError}");
          
          // Reset flags since the stop move failed
          if (tradeSaverActivated)
          {
          tradeSaverActivated = false;
          isSetToBreakeven = false;
          PrintDebugInfo("Resetting trade saver flags due to order error");
          }
          break;
          
          case OrderState.Working:
          PrintDebugInfo($"Stop loss order successfully updated to {stopPrice:F2}");
          break;
          
          case OrderState.Cancelled:
          PrintDebugInfo("Stop loss order cancelled");
          stopOrder = null;
          break;
          }
          
          // Update our stop order reference for non-cancelled states
          if (orderState != OrderState.Cancelled)
          {
          stopOrder = order;
          }
          }
          }​

          So now, looks like I need to track stopOrder state in a more granular way.. I need to keep track of the state of stopOrder change attempt...and if OnOrderUpdate reveals that change was rejected, i can try again, i guess.

          Thanks Jesse

          Comment


            #6
            Hello iantriestrading,

            Yes you would handle it however you plan to, disabling the realtime errors will let you program the strategy however you want assuming you will make logic for any errors that you may observe.

            Comment


              #7
              Perfect. Thank you so much for your help. This has been a glaring hole in my order handling

              Comment


                #8
                Is there still no way to disable the popup dialog with the order rejection error?

                Comment


                  #9
                  Hello iantriestrading,

                  At this time there is not, the general platform design is meant to be watched while working so the popup messages ensure the user who is watching has a idea of what's happening. It can be annoying to have to close rejection messages if you have developed something yourself but that also gives you a direct feedback of how your logic is working. If the strategy is submitting many rejected orders it may be wise to review the logic used to see if there is a better way toward the goal you were trying for.

                  Comment


                    #10
                    That's ok - please add my vote to the feature request to at least be able to disable those. I understand why it's not a priority though, as the rejection comes from the broker so a strat could be stuck in a loop sending thousands of ChangeOrder requests for example, with user unaware.

                    Anyway, my strategy now keeps track of the attempted order change and knows if it fails, then retries, thanks to your help.
                    I also improved my stop loss price calulation for moving to breakeven, check bid/ask instead of Close[0], and my "stop can't be changed below market" errors are gone

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    71 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    143 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    76 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    47 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    51 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X