Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CancelOrder() problem

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

    CancelOrder() problem

    Hi Josh
    I’m struggling to understand how to use the CancelOrder() command, I’ve looked at your other threads but I’m no further on…
    I want to write a very simple breakout strategy which places a buy or sell limit order at the breakout point, but I want the limit orders to be cancelled if it is not filled within 3 bars. This is what I have so far which unfortunately doesn’t work… could you shed some light on this? Thanks
    Regards
    John

    #region Variables
    private IOrder myEntryOrder = null;
    private int barNumberOfOrder = 0;

    #endregion

    CalculateOnBarClose = false;
    }

    protected override void OnBarUpdate()
    {

    if Rising(DonchianChannel(14).Upper) == true
    && (myEntryOrder == null)

    {
    myEntryOrder = EnterLongLimit(1, DonchianChannel(14).Upper[0], "");
    barNumberOfOrder = CurrentBar;
    }


    if (CurrentBar > barNumberOfOrder + 3)
    CancelOrder(myEntryOrder);

    {

    #2
    Hi John833,

    Instead of using barNumberOfOrder you can try using the BarsSinceEntry() method.

    Code:
    if (BarsSinceEntry() > 3)
         CancelOrder(myEntryOrder);
    Other than that please try using TraceOrders = true to see what your orders are doing. It will provide you with information if some action is ignored or rejected. Please see this tip about TraceOrders: http://www.ninjatrader-support.com/v...ead.php?t=3627
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      thanks Josh,
      So I still need to have in the Variables section

      private IOrder myEntryOrder = null;


      and should I be able to backtest this strategy?
      Last edited by John833; 10-29-2008, 09:52 AM.

      Comment


        #4
        Yeah, you should be able to.

        You do still need that in Variables.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh

          With the code....

          if (BarsSinceEntry() > 3)
          CancelOrder(myEntryOrder);

          do I only need to have this in the script once, or under each condition set which uses it?

          (sorry for the basic question)

          Thanks

          JOhn

          Comment


            #6
            If you have multiple entry orders it is best to give them all unique signal names. Then when you use BarsSinceEntry() you want to pass in that signal name to tie them together. That way you can submit your CancelOrder() to the correct entry order.

            You will need to do this for every condition if you want to cancel every entry order.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              thanks Josh, that's a great help

              Regards

              John

              Comment


                #8
                Josh
                Thanks for the code yesterday, the script almost works. The problem is that when a limit order is placed but not executed, it doesn’t cancel on the 3rd bar, it cancels it on the next bar.

                Also with Iorder variable, does it also need to be defined in #region Properties at the bottom?

                I am still a newbie with NT so I am wondering if I may have put the code in the wrong structure, you could point me in the right direction… Thanks !


                #region Variables
                private IOrder myEntryOrder = null;
                #endregion

                protectedoverridevoid OnBarUpdate()
                {
                if (Historical)
                return;

                if (myEntryOrder == null)


                // Condition set 1
                if Rising(DonchianChannel(14).Upper) == true

                {
                myEntryOrder = EnterLongLimit(1, DonchianChannel(14).Upper[1], "");
                }

                if (BarsSinceEntry() >= 3)
                CancelOrder(myEntryOrder);
                }
                Last edited by John833; 10-30-2008, 03:45 AM.

                Comment


                  #9
                  John833,

                  The Properties region is only needed when you are making plots on indicators or user defined parameters so you do not need to mess around in there for this situation.

                  What do you mean by is "placed but not executed"? The limit order is generally not executed until you get filled. Are you saying it is just sitting around in a "Working" state and it gets cancelled on the second bar?

                  You may also need to remove that first line you have checking if (myEntryOrder == null). It may be causing problems later on in your code because you are not using any { } or placing any logic to go with that if-statement.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Josh
                    Sorry if I wasn’t clear, what I meant to say and what I’m trying to do is…
                    if a limit order is submitted but not executed within 3 bars, then it is cancelled

                    I’ve taken out (myEntryOrder==null) and the strategy still cancels the limit order (if it is not executed) on the second bar instead of the third.
                    Do I need to use {} around CancelOrder, what are your thoughts?

                    Comment


                      #11
                      You do not need to use { } around it because it is just a single line. I suggest you print out the values of BarsSinceEntry() to determine why it is evaluating to true.
                      Code:
                      Print(BarsSinceEntry().ToString());
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Josh
                        I’ve used TraceOrders and this is the output that I have when a Limit Order is cancelled on the second bar instead of the third….
                        10:22AM Entered internal PlaceOrder() method at 10:22:00AM: Action=SellShort OrderType=Limit Quantity =1…..etc
                        10:23AM Cancelled expired order. BarsInProgress=0: Order=…..etc

                        I’m not really sure what this means…. But do I need to code something with ‘BarsInProgress’ ?

                        Comment


                          #13
                          You do not need to use BarsInProgress unless you are using a multi-time/instrument strategy which I do not believe you are.

                          The error explains exactly what is happening here. Your order is not being kept alive with a resubmission on the next bar. There are two ways to keep your order alive. One, resubmit the order every bar to prevent it from expiring. Two, use the liveUntilCancelled = true parameter. The second option is what I would suggest you to use.

                          EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)

                          Code:
                          EnterLongLimit(0, true, 100, High[0], "Long");
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Josh, I’ve changed the order initiation line, and this is the structure that I have…. Unfortunately I am getting the same results as before and the exact same TraceOrder output. I’m not using a multi-time/instrument strategy.
                            Is the structure correct or have I written something wrong?

                            #region Variables
                            private IOrder myEntryOrder = null;
                            #endregion

                            protectedoverridevoid OnBarUpdate()
                            {

                            if (Historical)
                            return;


                            // Condition set 1
                            if Rising(DonchianChannel(14).Upper) == true

                            {
                            myEntryOrder = EnterLongLimit(0, true, 1, DonchianChannel(14).Upper[1], "");
                            }

                            if (BarsSinceEntry() >= 3)
                            CancelOrder(myEntryOrder);
                            }

                            Comment


                              #15
                              Did that code compile for you? You are missing ( ) on your if-statement.

                              Code:
                              [COLOR=blue][FONT=Courier New]private[/FONT][/COLOR][COLOR=black][FONT=Courier New] IOrder myEntryOrder = [/FONT][/COLOR][COLOR=blue][FONT=Courier New]null[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
                              [COLOR=blue][FONT=Courier New]#endregion[/FONT][/COLOR]
                               
                              [COLOR=blue][FONT=Courier New]protected[/FONT][/COLOR][COLOR=blue][FONT=Courier New]override[/FONT][/COLOR][COLOR=blue][FONT=Courier New]void[/FONT][/COLOR][COLOR=black][FONT=Courier New] OnBarUpdate()[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]{[/FONT][/COLOR]
                               
                              [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (Historical)[/FONT][/COLOR]
                              [COLOR=blue][FONT=Courier New]return[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
                               
                               
                              [COLOR=green][FONT=Courier New]// Condition set 1[/FONT][/COLOR]
                              [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] [SIZE=6][COLOR=Red]([/COLOR][/SIZE]Rising(DonchianChannel(14).Upper) == [/FONT][/COLOR][COLOR=blue][FONT=Courier New]true[/FONT][/COLOR][SIZE=5][COLOR=Red])[/COLOR][/SIZE]
                               
                              [COLOR=black][FONT=Courier New]{[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]myEntryOrder = EnterLongLimit([/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New], true, 1, DonchianChannel(14).Upper[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]], [/FONT][/COLOR][COLOR=maroon][FONT=Courier New]""[/FONT][/COLOR][COLOR=black][FONT=Courier New]);[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
                               
                              [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (BarsSinceEntry() >= [/FONT][/COLOR][COLOR=purple][FONT=Courier New]3[/FONT][/COLOR][COLOR=black][FONT=Courier New])[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]CancelOrder(myEntryOrder);[/FONT][/COLOR]
                              [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              578 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X