Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit Orders

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

    Limit Orders

    Hi,

    In my strategy if there is a EnterLongLimit(...) order waiting to trigger and if I get a short signal, it won't cancel the EnterLongLimit(...) order and place EnterShortLimit(...) order. I was wondering if I could write in CancelOrder() one line above EnterLongLimit and EnterShortLimit to cancel any orders?

    Thanks

    #2
    Hello AgriTrdr,

    Correct, calling a working entry in the opposite direction will cause that order to be ignored.


    Yes, you can assign the order to a variable in OnOrderUpdate(), and then supply the variable holding that order to CancelOrder() to cancel it (after checking the order is not null and is OrderState.Accepted or OrderState.Working).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I've a loop created for the entry order:

      EnterLongLimit(0, true, tradeSizes[idx], BarOpen, string.Format("Buy #{0}",idx+1));

      Would I be able to use the order name as "Buy #0"?

      Also, the example says "myEntryOrder", I would need to create a separate one for "myBuyOrder" and "mySellOrder", correct?

      Comment


        #4
        Hello AgriTrdr,

        Yes, that could be the signal name of the order.

        Yes, I would recommend using unique signal names for the buy and sell orders (or for any different order type).
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi,

          I followed the example and have done the code below, but it's still not canceling the order and creating a new limit order in the opposite direction. Am I calling the order to be cancelled in the correct place?

          Code:
          OnBarUpdate.....
          if (rules...)
          {
          if (myBuyOrder == null)
          
          {
          
          if ((mySellOrder.OrderState == OrderState.Working) || (mySellOrder.OrderState == OrderState.Accepted))
          
          {
          
          CancelOrder(mySellOrder);
          
          }
          
          for (int idx=0;idx<5;idx++)
          
          if (tradeSizes[idx]>0)
          
          EnterLongLimit(0, true, tradeSizes[idx], BarOpen, string.Format("Buy #{0}",idx+1));
          
          }
          ​
          }
          
          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 == "Buy #{0}")
          
                myBuyOrder = order;
          
           
          
            if (order.Name == "Sell short #{0}")
          
                mySellOrder = order;
          
           
          
            // Evaluates for all updates to myEntryOrder.
          
            if (myBuyOrder != null && myBuyOrder == order)
          
            {
          
                // Check if myEntryOrder is cancelled.
          
                if (myBuyOrder.OrderState == OrderState.Cancelled)
          
                {
          
                    // Reset myEntryOrder back to null
          
                    myBuyOrder = null;
          
                }
          
            }
          
           
          
            if (mySellOrder != null && mySellOrder == order)
          
            {
          
                // Check if myEntryOrder is cancelled.
          
                if (mySellOrder.OrderState == OrderState.Cancelled)
          
                {
          
                    // Reset myEntryOrder back to null
          
                    mySellOrder = null;
          
                }
          
            }
          
          }
          ​

          Comment


            #6
            Hello AgriTrdr,

            Where you have:

            if (order.Name == "Buy #{0}")

            The {0} should be replaced by the number of the order.

            You are naming the orders with:
            string.Format("Buy #{0}",idx+1)

            You will need that same signal name where {0} is being replaced by idx+1.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I tried the below and I'm receiving an error message when it's triggering the order.

              if (order.Name == string.Format("Buy #idx+1"))

              myBuyOrder = order;



              if (order.Name == string.Format("Sell short #idx+1"))

              mySellOrder = order;

              Error message:
              Error on calling 'OnBarUpdate' method on bar 9798: Object reference not set to an instance of an object.

              Comment


                #8
                Hello AgriTrdr,

                In the other code you have:

                string.Format("Buy #{0}",idx+1)

                Where index is a variable defined in a loop which holds a number.

                for (int idx=0;idx<5;idx++)

                In this last post you have

                string.Format("Sell short #idx+1")

                Where idx is now part of the string and no longer the number.

                You need to supply the same string used for the signal name.

                Try printing the signal name in OnOrderUpdate() so you can see what it is.

                Print(order.Name);


                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                50 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                69 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X