Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to Change Order Error NT8b10

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

    Unable to Change Order Error NT8b10

    I get the following error in NT8b10:

    "Strategy submitted an order that generated the following error 'Unable to change order'. Strategy has sent cancel requests, attempted to close the position an terminate itself.

    Pic is attached.

    Here is a snippet of the code. Note isLivUntilCancelled is set to TRUE.


    if (Trade Condition occurs....)
    {

    myEntryOrder = EnterLongLimit(0, true, DefaultQuantity, bidPrice, "LongStrategyEntry");
    SetProfitTarget("LongStrategyEntry", CalculationMode.Ticks, ProfitTargetValue);
    SetStopLoss("LongStrategyEntry", CalculationMode.Ticks, StopLossValue, True);
    barNumberOfOrder = CurrentBar;
    myCancelOrder = myEntryOrder;
    }

    // If more than CancelOffset bars has elapsed, cancel the entry order
    if (CurrentBar > barNumberOfOrder + CancelOffset)
    {
    CancelOrder(myCancelOrder);
    }

    and here is the event handler...

    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    // Check if Order is Filled or Partially Filled
    if (myEntryOrder != null && myEntryOrder == order)
    {
    if (myEntryOrder.OrderState == OrderState.Filled)
    {
    myEntryOrder = null;
    myCancelOrder = null;
    }
    }

    if (myCancelOrder != null && myCancelOrder == order)
    {
    if (myCancelOrder.OrderState == OrderState.Cancelled)
    {
    myEntryOrder = null;
    myCancelOrder = null;
    }
    }
    }


    I'm thinking there might be a race condition. Perhaps somebody else can see something I am missing. Any help would be greatly appreciated!
    Attached Files

    #2
    Hello geekodude,

    I did notice one path which causes CancelOrder to be called with null as its argument :

    • At bar 0 : you enter position and set the variable cancelorder
    • At bar 10 : you call your cancel method on cancelorder
    • Your cancel method sets cancelorder to null
    • At bar 11 : you call your cancel method on (a now null) cancelorder

    I would recommend adding a check to see if you are in position to your "if (CurrentBar > barNumberOfOrder + CancelOffset)" statement. While a simple null check in the style you have been using would likely suffice, I am including a link to the NT8 help guide page on Position checking methods.





    Please let us know if you continue to see this message, or if there are any other questions we could answer.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Jessica. I re-wrote my code modeling the example presented here in the code breaking changes with OnOrderUpdate:

      Navigation: NinjaScript > Code Breaking Changes

      "Strategies, Orders, and Accounts
      Low level access has been provided to allow more flexibility with the information pertaining to trade data.

      • IOrders, IExecution, and IPosition interfaces have all been replaced directly with the corresponding object
      • The signatures of the related NinjaScript events have changed to match the NinjaTrader internal Update events
      • Methods now return and update with the object instance generated, instead of the previously used interface

      Tip: Since NinjaTrader 8 now exposes the direct Order object, rather than an IOrder interface, it is possible to receive null object reference errors if you attempt to access an order object before the entry or exit order method has returned. To prevent these situations, it is recommended to assign your strategies Order variables in the OnOrderUpdate() method and match them by their signal name (order.Name). Please see the example beginning on line #22 below for demonstration of assigning order objects to private variables."


      Code:
      Order myOrder = null;
       
      protected override void OnBarUpdate()
      {         
        if (Position.MarketPosition == MarketPosition.Flat && myOrder == null)
          EnterLongLimit(Low[0], "Entry");
        
        if (myOrder != null)
        {
          Print(myOrder.OrderState);
          
          if (myOrder.OrderState == OrderState.Cancelled || myOrder.OrderState == OrderState.Filled)
              myOrder = null;           
        }
      }     
       
      protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, 
        int quantity, int filled, double averageFillPrice, 
        Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
      {     
        // compare the order object created via EnterLongLimit by the signal name
        if (myOrder == null && order.Name == "Entry")
        {
          // assign myOrder to matching order update
          myOrder = order;         
        }
      }
      I originally modeled my code using the CancelOrder() example found here:

      Navigation: NinjaScript > Language Reference > Strategy > Order Methods > Managed Approach >
      CancelOrder()

      HTML Code:
      private Order myEntryOrder = null;
      private int barNumberOfOrder = 0;
       
      protected override void OnBarUpdate()
      {
        // Submit an entry order at the low of a bar
        if (myEntryOrder == null)
        {
          // use 'live until canceled' limit order to prevent default managed order handling which would expire at end of bar
          myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
          barNumberOfOrder = CurrentBar;
        }
       
        // If more than 5 bars has elapsed, cancel the entry order
        if (CurrentBar > barNumberOfOrder + 5)
          CancelOrder(myEntryOrder);
      }
       
      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
        double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {
        // Checks for all updates to myEntryOrder.
        if (myEntryOrder != null && myEntryOrder == order)
        {
          // Check if myEntryOrder is cancelled.
          if (myEntryOrder.OrderState == OrderState.Cancelled)
          {
              // Reset myEntryOrder back to null
              myEntryOrder = null;
          }
        }
      }
      This new code I am currently testing. So far I have not received errors. I will report back if there are any issues.

      Thank you
      Last edited by geekodude; 03-23-2016, 09:39 AM.

      Comment


        #4
        As a follow-up, after the re-write there is no errors with the CancelOrder(). I would suggest Ninjatrader to include a CancelOrder() example documentation to reflect the new Order object.

        Thank you

        Comment


          #5
          Hello geekodude,

          Thank you for providing that updated information. When I compared your suggested update to the code sample, I did notice the following. Lines beginning with a - are the code in the help guide sample,



          Lines beginning with a + are from your code. A ... indicates I have skipped code.


          Code:
             if (myEntryOrder == null)
             {
               // use 'live until canceled' limit order to prevent default managed order handling which would expire at end of bar
          -    EnterLongLimit(0, true, 1, Low[0], "Long Entry");
          +    myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
               barNumberOfOrder = CurrentBar;
             }
           
          ...
          
           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 entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
          -  // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
          -  if (order.Name == "Long Entry" && orderState == OrderState.Filled)
          -      entryOrder = order;
          -
          -
             // Checks for all updates to myEntryOrder.
             if (myEntryOrder != null && myEntryOrder == order)
             {

          I believe the form setting entryOrder in OnOrderUpdate, rather than OnBarUpdate, to be correct, for the reasons stated in the comment block.

          Given this, may I ask, which changes would you like to be made to the NT8 Help Guide?
          Jessica P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by dpolyakov, 03-29-2024, 11:30 AM
          4 responses
          150 views
          2 likes
          Last Post NinjaTrader_RyanS  
          Started by traderqz, Today, 12:06 AM
          1 response
          2 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by kujista, Today, 06:23 AM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Pattontje, Yesterday, 02:10 PM
          2 responses
          34 views
          0 likes
          Last Post Pattontje  
          Started by abdo22, Yesterday, 03:15 PM
          3 responses
          15 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X