Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Orders not cancelling...

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

    Orders not cancelling...

    Having an issue with a vers of SampleOnOrderUpdate which I've modified to use Limit Orders... but am having issues with the CancelOrder() command... my orders don't seem to be cancelling... I've tried TraceOrders but still am lost... appreciate any help... I'm calling the CancelOrders() from OnOrderUpdate()

    Code:
            protected override void OnBarUpdate()
            {
                // Submit an entry limit order if we currently don't have an entry order open
                if (entryOrder == null && Close[0] > Open[0])
                {
                    entryOrder = EnterLongLimit(0, true, 1, Median[0] + -2 * TickSize, "MyEntry");
                }
      
                if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2)))
                {
                    // Checks to see if our Stop Order has been submitted already
                    if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
                    {
                        // Modifies stop-loss to breakeven
                        stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
                    }
                }
            }
    
            /// <summary>
            /// Called on each incoming order event
            /// </summary>
            protected override void OnOrderUpdate(IOrder order)
            {
                // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                if (entryOrder != null && entryOrder.Token == order.Token)
                {    
    
                    // Reset the entryOrder object to null if order was cancelled without any fill
                    if (order.OrderState == OrderState.Working
                        && order.LimitPrice < GetCurrentBid() - 4 * TickSize)
                    {
                        CancelOrder(entryOrder);
                        entryOrder = null;
                    }
                }
            }
            
            /// <summary>
            /// Called on each incoming execution
            /// </summary>
            protected override void OnExecution(IExecution execution)
            {
                if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                        // Stop-Loss order 4 ticks below our entry price
                        stopOrder     = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                        
                        // Target order 8 ticks above our entry price
                        targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                        
                        // Resets the entryOrder object to null after the order has been filled or partially filled
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            entryOrder     = null;
                        }
                    }
                }
                
                // Reset our stop order and target orders' IOrder objects after our position is closed.
                if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                    {
                        stopOrder = null;
                        targetOrder = null;
                    }
                }
            }
    
            /// <summary>
            /// Called on each incoming position event
            /// </summary>
            protected override void OnPositionUpdate(IPosition position)
            {
                // Print our current position to the lower right hand corner of the chart
                DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
            }

    #2
    Hello,

    I will have someone take a look at this on Monday.
    DenNinjaTrader Customer Service

    Comment


      #3
      Please add Print() lines to your strategy and follow it sequentially at each step of the way. Evaluate your conditions and see if they are ever true.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Josh,

        when I get to the OnOrderUpdate() section, its not reading the order.LimitPrice variable

        Comment


          #5
          Please print that value along with GetCurrentBid calculation you had there and evaluate it by hand. Remember, GetCurrentBid() is a real-time method only. When not in real-time it will only return the close of the bar.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            real-time should include market replay, correct?

            Comment


              #7
              Correct. Please print and see what values you get.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Josh, so I did the Print() statement like you mentioned...

                I do get the the order.LimitPrice, and i get the current bid

                but as prices move, the current bid does not change

                Comment


                  #9
                  Then your bid is staying the same. Please use other conditions to check for to cancel your bar with. Please remember, in OnOrderUpdate() you only receive one event for the working state. It is not like OnOrderUpdate() is constantly checking for what your limit order price is vs the bid.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Josh View Post
                    Then your bid is staying the same. Please use other conditions to check for to cancel your bar with. Please remember, in OnOrderUpdate() you only receive one event for the working state. It is not like OnOrderUpdate() is constantly checking for what your limit order price is vs the bid.

                    i guess this is where i'm hitting my wall in terms of getting around it...

                    any suggestion in terms of something to try?

                    Comment


                      #11
                      You will just need to objectify how you want it to behave. Place it where it needs to go. Maybe in OnBarUpdate().
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        ok will give it a whirl and get back to you

                        Comment


                          #13
                          when I put in the cancel logic in OnBarUpdate()... in the Log tab I get the following error...
                          "Error on calling 'OnBarUpdate' method for strategy "SampleOnOrderUpdate'. Object reference not set to an instance of an object.

                          Code:
                                  protected override void OnBarUpdate()
                                  {
                                      // Submit an entry limit order if we currently don't have an entry order open
                                      if (entryOrder == null && Close[0] > Open[0])
                                      {
                                          entryOrder = EnterLongLimit(0, true, 1, Median[0] + -2 * TickSize, "MyEntry");
                                      }
                                      
                                      if (entryOrder.OrderState == OrderState.Working
                                          && entryOrder.LimitPrice < Median[0] - 4 * TickSize)
                                      {
                                          CancelOrder(entryOrder);
                                          entryOrder = null;
                                      }
                                     
                                      if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2)))
                                      {
                                          // Checks to see if our Stop Order has been submitted already
                                          if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
                                          {
                                              // Modifies stop-loss to breakeven
                                              stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
                                          }
                                      }
                                  }

                          Comment


                            #14
                            You still need to check for null first. http://www.ninjatrader-support2.com/...ead.php?t=4226
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Josh,
                              it works!


                              If you were an attractive lady, I'd kiss you!!!!


                              But in all honesty, thank you... I REALLY REALLY appreciate your help and patience!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              633 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              364 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              567 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X