Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop price can't be changed above/below the market

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

    Stop price can't be changed above/below the market

    So, I see this error:
    'Stop price can't be changed below the market. affected Order: BuyToCover 1 StopMarket @ 11051.25'

    I know the reason, but how can I catch this error/message in the code? All what I see in the log file is:
    Code:
    2023-01-17 00:07:16:342|1|32|Order='9df18931ee2b40139f5e5a102907c221/Playback101' Name='Short#PB#11#SL' New state='Accepted' Instrument='NQ 03-23' Action='Buy to cover' Limit price=0 Stop price=11051.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='bce9a440833041fba1e23cdc7f9bc260' Filled=0 Fill price=0 Error='Unable to change order' Native error='Stop price can't be changed below the market.'
    2023-01-17 00:07:16:342|0|32|Playback101, Stop price can't be changed below the market. affected Order: BuyToCover 1 StopMarket @ 11051.25
    ​
    So, this is not OrderState.Rejected - I cannot catch it in OnOrderTrace (for instance). Where can I catch this?

    #2
    Hello webus,

    Thanks for your post.

    This error message means that the strategy attempted to change an order to the wrong side of the market. If this is due to market volatility then there isn't really a way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

    Something you could consider is using GetCurrentBid() and GetCurrentAsk() to offset orders so that they are more likely to land on the correct side of the market.

    See these help guide pages for more information.
    GetCurrentBid(): https://ninjatrader.com/support/help...currentbid.htm
    GetCurrentAsk(): https://ninjatrader.com/support/help...currentask.htm

    You could also consider using RealtimeErrorHandling.IgnoreAllErrors to trap order errors in OnOrderUpdate by checking error == ErrorCode.UnableToChangeOrder.

    Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking error == ErrorCode.UnableToChangeOrder within OnOrderUpdate() followed by defining your own order handling behavior for the order.

    Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping orders in OnOrderUpdate().

    RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm

    Let us know if we may assist further.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi NinjaTrader_BrandonH​,

      Originally posted by NinjaTrader_BrandonH View Post
      Hello webus,

      Thanks for your post.

      This error message means that the strategy attempted to change an order to the wrong side of the market. If this is due to market volatility then there isn't really a way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

      Something you could consider is using GetCurrentBid() and GetCurrentAsk() to offset orders so that they are more likely to land on the correct side of the market.

      See these help guide pages for more information.
      GetCurrentBid(): https://ninjatrader.com/support/help...currentbid.htm
      GetCurrentAsk(): https://ninjatrader.com/support/help...currentask.htm

      You could also consider using RealtimeErrorHandling.IgnoreAllErrors to trap order errors in OnOrderUpdate by checking error == ErrorCode.UnableToChangeOrder.

      Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking error == ErrorCode.UnableToChangeOrder within OnOrderUpdate() followed by defining your own order handling behavior for the order.

      Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping orders in OnOrderUpdate().

      RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm

      Let us know if we may assist further.​
      As I said - I know the reason of the issue. I'm using 'RealtimeErrorHandling.IgnoreAllErrors' for this case and I understand the risks.
      I'm asking how to catch this message from the code? Does NT8 have some callback for such errors? OnOrderTrace() or OnOrderUpdate() can't catch these cases.

      Comment


        #4
        Hello webus,

        Thanks for your note.

        To trap an 'Unable to change order' error, you would set RealtimeErrorHandling to IgnoreAllErrors and trap the order errors by checking error == ErrorCode.UnableToChangeOrder in OnOrderUpate() as mentioned in my previous post.

        From post # 2: "You could also consider using RealtimeErrorHandling.IgnoreAllErrors to trap order errors in OnOrderUpdate by checking error == ErrorCode.UnableToChangeOrder.

        Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking error == ErrorCode.UnableToChangeOrder within OnOrderUpdate() followed by defining your own order handling behavior for the order.

        Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping orders in OnOrderUpdate().

        RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm​"


        Please let me know if you have further questions.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hi NinjaTrader_BrandonH​,​

          To trap an 'Unable to change order' error, you would set RealtimeErrorHandling to IgnoreAllErrors and trap the order errors by checking error == ErrorCode.UnableToChangeOrder in OnOrderUpate() as mentioned in my previous post.
          Sorry, I did not undertand it correctly.
          However, I do not see the rejection. I must say that this is not placing an order - this is ChangeOrder() function. The order is not rejected - it remains on the chart. So I do not see this event in the OnOrderTrace() or OnOrderUpdate() callbacks. All what I see in the log file about the case is:
          Code:
          2023-01-17 00:07:16:342|1|32|Order='9df18931ee2b40139f5e5a102907c221/Playback101' Name='Short#PB#11#SL' New state='Accepted' Instrument='NQ 03-23' Action='Buy to cover' Limit price=0 Stop price=11051.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='bce9a440833041fba1e23cdc7f9bc260' Filled=0 Fill price=0 Error='Unable to change order' Native error='Stop price can't be changed below the market.'
          2023-01-17 00:07:16:342|0|32|Playback101, Stop price can't be changed below the market. affected Order: BuyToCover 1 StopMarket @ 11051.25
          ​​

          Comment


            #6
            Hello webus,

            Thanks for your note.

            The Log file you shared shows that the Error for this order error is Error='Unable to change order'.

            2023-01-17 00:07:16:342|1|32|Order='9df18931ee2b40139f5e5a102 907c221/Playback101' Name='Short#PB#11#SL' New state='Accepted' Instrument='NQ 03-23' Action='Buy to cover' Limit price=0 Stop price=11051.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='bce9a440833041fba1e23cdc7f9bc260' Filled=0 Fill price=0 Error='Unable to change order' Native error='Stop price can't be changed below the market.'


            To trap this error, you would need to check if the error == ErrorCode.UnableToChangeOrder within the OnOrderUpdate() method and then program your order handling logic. Some example code could be found below.

            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)
            {
              // Assign stopLossOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
              // This is more reliable than assigning Order objects in OnBarUpdate,
              // as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
              if (order.Name == "myStopLoss" && stopLossOrder != order)
                stopLossOrder = order;
            
              if (stopLossOrder != null && stopLossOrder == order)
              {
                // Rejection handling
                if (error == ErrorCode.UnableToChangeOrder)
                {
                    // Order was unable to be changed
                    // Do something about it here
                }
              }
            }​
            You could find information about OnOrderUpdate() and the different ErrorCode options on the OnOrderUpdate() help guide page linked below.

            OnOrderUpdate(): https://ninjatrader.com/es/support/h...rderupdate.htm

            Please let me know if I may assist further.
            Last edited by NinjaTrader_BrandonH; 01-17-2023, 11:53 AM.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hi NinjaTrader_BrandonH​,​​

              Originally posted by NinjaTrader_BrandonH View Post
              Hello webus,

              Thanks for your note.

              The Log file you shared shows that the Error for this order error is Error='Unable to change order'.

              2023-01-17 00:07:16:342|1|32|Order='9df18931ee2b40139f5e5a102 907c221/Playback101' Name='Short#PB#11#SL' New state='Accepted' Instrument='NQ 03-23' Action='Buy to cover' Limit price=0 Stop price=11051.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='bce9a440833041fba1e23cdc7f9bc260' Filled=0 Fill price=0 Error='Unable to change order' Native error='Stop price can't be changed below the market.'


              To trap this error, you would need to check if the error == ErrorCode.UnableToChangeOrder within the OnOrderUpdate() method and then program your order handling logic. Some example code could be found below.

              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)
              {
              // Assign stopLossOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
              // This is more reliable than assigning Order objects in OnBarUpdate,
              // as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
              if (order.Name == "myStopLoss" && orderState == OrderState.Filled)
              stopLossOrder = order;
              
              if (stopLossOrder != null && stopLossOrder == order)
              {
              // Rejection handling
              if (error == ErrorCode.UnableToChangeOrder)
              {
              // Order was unable to be changed
              // Do something about it here
              }
              }
              }​
              You could find information about OnOrderUpdate() and the different ErrorCode options on the OnOrderUpdate() help guide page linked below.

              OnOrderUpdate(): https://ninjatrader.com/es/support/h...rderupdate.htm

              Please let me know if I may assist further.
              Sorry, pehaps I'm not clear enought. I'm using the 'protected override void OnOrderUpdate' in my code and log each new OnOrderUpdate event. However, the case I'n trying to share does not lead to OnOrderUpdate() event. Even OnOrderTrace is not written for this case. Just the message about the error. This is why I'm asking if it possible to catch this message/error not from the OnOrderUpdate overriding.

              Comment


                #8
                Hello webus,

                Thanks for the clarification.

                OnOrderTrader() is a way of overriding the default behavior of TraceOrder prints. TraceOrders show when orders are being submitted, ignored, or automatically canceled from the behavior of the Managed Approach.

                See the help guide documentation below for more information about OnOrderTrace(), TraceOrders, and the Managed Approach.

                OnOrderTrace(): https://ninjatrader.com/es/support/h...ordertrace.htm
                TraceOrders: https://ninjatrader.com/es/support/h...aceorders2.htm
                Managed Approach: https://ninjatrader.com/es/support/h...d_approach.htm

                That said, you could try assigning the order to the stop loss order object (stopLossOrder) when stopLossOrder is not equal to the order. I have modified the sample code in post # 6 to reflect this. This could also be seen below.

                if (order.Name == "myStopLoss" && stopLossOrder != order)
                stopLossOrder = order;


                You should also print out the order object at the top of OnOrderUpdate() (outside of any conditions) to see the order object information print to the NinjaScript Output window​ (New > NinjaScript Output).

                OnOrderUpdate(): https://ninjatrader.com/es/support/h...rderupdate.htm
                NinjaScript Output window: https://ninjatrader.com/es/support/h...nt8/output.htm

                Please let me know if you have further questions.​
                Last edited by NinjaTrader_BrandonH; 01-17-2023, 12:21 PM.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                58 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                133 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X