Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Best way to cancel all orders at the end of the day?

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

    Best way to cancel all orders at the end of the day?

    Hi,

    How can I best cancel all orders at the end of the day? I am working with GTCs want them pulled at the end of the day?

    #2
    GKonheiser,

    You can create a time filter for the end of the day that will cancel any working orders or close any positions that you may have open.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Exit on close is taking care of the positions its just the orders that need to be cancelled. What is the command to pull all orders?

      Comment


        #4
        There are two methods to use.

        The first one is the supported method -
        CancelOrder(IOrder order);

        The second is unsupported code -
        CancelAllOrders(bool Entries, bool Exits);

        Note that with the unsupported code, we would not be able to provide assistance with any debugging or scripting with it.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          I have 12 possible orders that can exits on close, can I just cancel all of the possibilites or will this generate errors, ie should I do a null check first:

          Secondly is there a command to check for the last bar in order to cancel the order, something like:-

          If(lastBar)

          Comment


            #6
            It is generally good practice to do a null check for your script.

            There is no last bar function in NinjaScript as the script would not know what the last bar would be.
            This is why I suggested the Time filter. You can use the GetNextBeginEnd() to get the time for when the session ends and use that in your time filter-
            GetNextBeginEnd()
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Thanks that great. I will play round with that. Quick question how do I set max look back bars to infinity?

              Comment


                #8
                MaximumBarsLookBack = MaximumBarsLookBack.Infinite

                This will go in the Initialize() Method.

                http://www.ninjatrader.com/support/h...rslookback.htm
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Im getting an error when I try and and do the check for 5 mins before end of session, if(ToTime(Time[0]) == sessionEnd.AddMinutes(-5)),

                  Here is the full code snippet.

                  if(ToTime(Time[0]) == 80500) // Get the end of Session time for when to cancel orders
                  {
                  Bars.Session.GetNextBeginEnd(Time[0], out sessionBegin, out sessionEnd);

                  Print(sessionEnd.AddMinutes(-5));
                  }

                  if(ToTime(Time[0]) == sessionEnd.AddMinutes(-5)) // Order Canceling at end 5 mins before end of session.
                  {

                  Comment


                    #10
                    What's the error?

                    Did you make sure to define sessionBegin and sessionEnd as DateTime variables?
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      Yup,

                      private DateTime sessionBegin;
                      private DateTime sessionEnd;

                      error is :-

                      Strategy\PivotTrader2.cs Operator '-' cannot be applied to operands of type 'System.DateTime' and 'int' CS0019 - click for info 149 11
                      Strategy\PivotTrader2.cs Operator '==' cannot be applied to operands of type 'int' and 'System.DateTime' CS0019 - click for info 154 7

                      Comment


                        #12
                        GKonheiser,

                        You do not need to use the ToTime() for the second condition.

                        The second error is coming from line 154 which is not displayed with the sample code you sent.
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks. I changed that and it compiles fine now but I am getting the below error when I enable to strategy:-

                          **NT** Error on calling 'OnBarUpdate' method for strategy 'PivotTrader2/25bde81be8aa42489dd9972dff8c0030': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                          I just did a debug with Visual Studio and I can see what is causing the problem.

                          sessionEnd is coming back with a strange value, see below:

                          - sessionEnd {01/01/0001 00:00:00} System.DateTime

                          do you know why this would be happening?
                          Last edited by GKonheiser; 11-24-2013, 01:08 PM.

                          Comment


                            #14
                            Hello GKonheiser,

                            Thank you for your response.

                            Please add the following to the beginning of the OnBarUpdate() method:
                            Code:
                            if (Bars.FirstBarOfSession && sessionCount < 2)
                                sessionCount++;
                               
                               if (sessionCount < 2) return;
                            You will need to add an int for the sessionCount in the Variables region as well.

                            Please let me know if this resolves the error message.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by andrewtrades, Today, 04:57 PM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by chbruno, Today, 04:10 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post chbruno
                            by chbruno
                             
                            Started by josh18955, 03-25-2023, 11:16 AM
                            6 responses
                            436 views
                            0 likes
                            Last Post Delerium  
                            Started by FAQtrader, Today, 03:35 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post FAQtrader  
                            Started by rocketman7, Today, 09:41 AM
                            5 responses
                            19 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X