Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel Order Using Managed Approach

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

    Cancel Order Using Managed Approach

    I put together some code and I wanted to see if this was looking correct as it is the very first time that I'm using the LiveUntilCancelled and CancelOrder approach. IOrder is not highlighted in blue in my strategy and I'm wondering if CancelOrder needs {} around it. Thanks!


    #region variables
    private IOrder myEntryOrder = null; //IOrder is NOT HIGHLIGHTED IN BLUE like in the help guide
    private int barNumberOfOrder = 0;
    private int ProfitTarget = 10;
    private int stoplossticks = 10;

    protected override void Initialize()
    //PROFIT TARGET AND STOP LOSS
    {
    SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
    SetStopLoss("", CalculationMode.Ticks, stoplossticks, false);
    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {

    if ((myEntryOrder == null)
    && CrossAbove(Close, EMA(LengthMA), 1)

    {
    EnterLongLimit(0, true, DefaultQuantity, Close[0], "Long Limit");
    barNumberOfOrder = CurrentBar;
    }

    if ((myEntryOrder == null)
    &&CrossBelow(Close, EMA(LengthMA), 1)
    {
    EnterShortLimit(0, true, DefaultQuantity, Close[0], "Short Limit");
    barNumberOfOrder = CurrentBar;
    }


    //Cancels Limit order I don't seem to need {} for the CancelOrder???????????????
    if (CurrentBar > barNumberOfOrder + 4)
    CancelOrder(myEntryOrder);


    }

    #2
    baseheadz, if statements will always execute the first line of code following the statement (if the condition is true), but if you have more than one line after an if statement, you'll need the brackets.
    Code:
    if (true)
    x = 1; // this will work since there is only one line
    
    if (true)
    x = 1;
    y = 2; // this would run whether or not the if statement is true
    
    if (true)
    {
    x = 1;
    y = 2; // this will run as part of the if statement
    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks Austin. Does everything else look good in the code that I posted?

      Comment


        #4
        I took a quick look and everything else seems like it is correct. You'll have to test it to find out 100% though. Please let me know if you have any other questions.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Austin,

          The last bit of code is giving me an error when I load the strategy:
          Error on calling OnBarUpdate method for Strategy: Object reference not set to an instance of an object.

          I narrowed down the code which is causing the error by commenting out the lines below, but I can't figure out why I'm generating the error:

          //Cancels Limit order
          if (CurrentBar > barNumberOfOrder + 5)
          CancelOrder(myEntryOrder);

          Comment


            #6
            I realized that I did not have "myEntryOrder =" in front of EnterLongLimit and EnterShortLimit. But when I added those, I still got the same exact error.

            Comment


              #7
              Ok... I think I have it fixed. I realized that I had to check for myEntryOrder to by null, and I also had to reset myEntryOrder back to null.

              Does this look ok????????

              if ((myEntryOrder != null) &&(CurrentBar > barNumberOfOrder + 7))
              {
              CancelOrder(myEntryOrder);
              myEntryOrder = null;
              }

              Comment


                #8
                Baseheadz, please see the help guide entry for IOrders for how to submit, use, and reset IOrders.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  I'm a little confused on this and I can not find any examples that help with Canceling the order.
                  Can I make myEntryOrder = null in the OnOrderUpdate like I have done?


                  In the OBU
                  //Cancels Limit order************************************
                  if ((myEntryOrder != null) &&(CurrentBar > barNumberOfOrder + 7))
                  {
                  CancelOrder(myEntryOrder);

                  }

                  protected override void OnOrderUpdate(IOrder order)
                  {
                  if ((myEntryOrder != null) &&(CurrentBar > barNumberOfOrder + 7))
                  {
                  myEntryOrder = null;
                  }

                  if (Position.MarketPosition != MarketPosition.Flat)
                  {
                  myEntryOrder = null;
                  }
                  }

                  Comment


                    #10
                    Hi Baseheadz,

                    We have a sample available for CancelOrder() here:
                    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until


                    You want to set the order back to null when either it's filled or cancelled.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Right, I have found a couple of example of how to reset back to null when the order is cancelled, but there are no examples of how to do this when the order is filled.

                      Comment


                        #12
                        It's the same, only you check filled order state along with cancelled. Example:

                        if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled)
                        entryOrder = null;

                        Another example on this page:
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          strike my last comment.

                          Here is what I understand. I am using the managed approach. Is this looking ok?


                          protected override void OnOrderUpdate(IOrder order)
                          {
                          if (myEntryOrder != null && myEntryOrder == order)
                          {
                          // Reset the entryOrder object to null if order was filled OR order was cancelled without any fill
                          if (order.OrderState == OrderState.Filled || order.OrderState == OrderState.Cancelled)
                          {
                          myEntryOrder = null;
                          }
                          }

                          }

                          Comment


                            #14
                            Yes, that looks correct.
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Thank you. Just to clarify, what exactly does this mean:
                              myEntryOrder == Order

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              670 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X