Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot query Exit on close order with OnExecution?

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

    Cannot query Exit on close order with OnExecution?

    I need to update some statistics when the trading for the day is done, so I attempted to query the "Exit on close" order and use it to trigger the final statistical display. Not being able to get the information, I decided to find out if any information at all was available from code, as the information is clearly in the log. No thanks, I will if I must, but I would rather not have to scrape the log to use as my function update trigger.

    I have attached a simple Strategy, made to guarantee that there will be "Exit on Close" orders, and to print some data about all executions. The output shows many orders being successfully queried and reported, but not a single order named "Exit on close". Was this a design decision that some executions cannot be queried even by name?
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class ExitOnCloseTest : Strategy
        {
            #region Variables
    
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
                ExitOnClose = true;
                ClearOutputWindow();
                DefaultQuantity = 100;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 1) return;
                
                if (Close[1] > Open[1] && Close[0] > Open[0]) EnterLong(DefaultQuantity, "LongEntry");
                if (Close[1] < Open[1] && Close[0] < Open[0]) EnterShort(DefaultQuantity, "ShortEntry");
            }
            
            protected override void OnExecution(IExecution execution)
            {
                if (execution.Order != null)
                {
                    Print("*************************");
                    Print("Execution Time: " + execution.Order.Time);
                    Print("Execution Name: " + execution.Order.Name);
                    Print("*************************");
                }
            }
    
            #region Properties
    
            #endregion
        }
    }
    Attached Files

    #2
    Bump. bump.

    Comment


      #3
      Hi koganam,

      Looking into this for you.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hi koganam,

        Thanks for your patience.

        This order is like the synchronization orders, they are not captured by OnExecution() (or other order methods) because they are generated by the core not by the script.

        I am submitting a feature request to have those orders trigger the order methods to our development department in your behalf.

        Thank you for notifying us of this.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi koganam,

          Thanks for your patience.

          This order is like the synchronization orders, they are not captured by OnExecution() (or other order methods) because they are generated by the core not by the script.

          I am submitting a feature request to have those orders trigger the order methods to our development department in your behalf.

          Thank you for notifying us of this.
          Thanks, Chelsea. Now, on to get my hacking hat for some digging for a hack.

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hi koganam,

            Thanks for your patience.

            This order is like the synchronization orders, they are not captured by OnExecution() (or other order methods) because they are generated by the core not by the script.

            I am submitting a feature request to have those orders trigger the order methods to our development department in your behalf.

            Thank you for notifying us of this.
            Interesting. No need for a hack. Your reply got me to thinking. Yes, the strategy did not execute the order, so nothing is returned by it. However, the order is still specific to the strategy (from reading the log), I figured that the information about the order might be available in OnOrderUpdate(). Surprise, surprise, it is available there.

            The information is posted for anyone else who ever needs to query that information.

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hi koganam,

              Thanks for your patience.

              This order is like the synchronization orders, they are not captured by OnExecution() (or other order methods) because they are generated by the core not by the script.

              I am submitting a feature request to have those orders trigger the order methods to our development department in your behalf.

              Thank you for notifying us of this.
              Chelsea, further testing makes this look like a bug. One needs to merely include the OnOrderUpdate() event handler, and in fact the execution then shows up in OnExecution(). Note: It is the mere inclusion of the OnOrderUpdate() event handler shell that enables this; the OnOrderUpdate() event handler itself may be completely empty of any code.

              IOW, we have a situation where the inclusion of an (unneeded event handler) in the class, is required for a different event handler to provide information about it, the latter's, event handling. This would be called a side effect, then, and side effects of this kind are just bad in principle.
              Last edited by koganam; 06-24-2014, 03:34 PM.

              Comment


                #8
                Hi koganam,

                Just to clarify, you had removed the OnBarUpdate() method, and this causes the script to not capture the execution for ExitOnClose?

                Do you have a sample script that can replicate?
                I just tested with and without OnBarUpdate and I'm getting this print on both.

                Execution='2eae63d360a941e98101deccf565f2f9' Instrument='ES 09-14' Account='Sim101' Name='Exit on close' Exchange=Default Price=1943.75 Quantity=1 Market position=Short Commission=0 Order='9c11e6a3688c480f99f6c95a4c6d7f57' Time='6/24/2014 3:55:30 PM'
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hi koganam,

                  Just to clarify, you had removed the OnBarUpdate() method, and this causes the script to not capture the execution for ExitOnClose?

                  Do you have a sample script that can replicate?
                  I just tested with and without OnBarUpdate and I'm getting this print on both.

                  Execution='2eae63d360a941e98101deccf565f2f9' Instrument='ES 09-14' Account='Sim101' Name='Exit on close' Exchange=Default Price=1943.75 Quantity=1 Market position=Short Commission=0 Order='9c11e6a3688c480f99f6c95a4c6d7f57' Time='6/24/2014 3:55:30 PM'
                  No Chelsea. OnBarUpdate() is not in the mix. There is the original file in this thread. Test it and you will see that OnExecution() never reports the "Exit on close" execution. Now, add an empty OnOrderUpdate() event handler and retest, and you will find the OnExecution() now reports the "Exit on close" execution.

                  I am not talking about the log here. The log reports the action: it is that the order properties were not being reported by the OnExecution() event handler.
                  Last edited by koganam; 06-24-2014, 11:18 PM.

                  Comment


                    #10
                    Hi koganam,

                    Tried again removing OnOrderUpdate().

                    With and without it seems to be ok.

                    Attached is test script.

                    Output below:
                    **NT** Enabling NinjaScript strategy 'DetectExitOnCloseTest/0753fb510a6a468abdd17b7f4fe1d957' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=False CalculateOnBarClose=True MaxRestarts=4 in 5 minutes

                    exit on close detected
                    Execution='9d1f51385ad340018f905df2342fc230' Instrument='ES 09-14' Account='Sim101' Name='Exit on close' Exchange=Default Price=1946.5 Quantity=1 Market position=Short Commission=0 Order='3ddf8091bcd145879fa3384d15b5718a' Time='6/25/2014 10:10:30 AM'
                    After testing this I added the OnOrderUpdate() in and got the same.
                    Attached Files
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi koganam,

                      I missed that we are looking for the Order object child of the execution order.

                      I was just using the execution name and not the Order object name.

                      I am able to confirm this now, sorry about the confusion. (I mean adding the OnOrderUpdate causes the Order child object to return)

                      We are looking into this.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi koganam,

                        Here is what we think is going on. The OnExecution and OnOrderUpdate methods run asynchronously. When the exit on close happens, the OnExecution is returning slightly before the IOrder object has finished being created and thus does not get returned with the execution object.
                        When adding the OnOrderUpdate override to the script, NinjaTrader has to check the code of the override and this is enough time for the IOrder object to be returned.

                        That is the currently the theory we are testing.

                        One quick question. I have found that this issue does not happen in real time data. Are you finding the same? (In other words, it only seems to happen in backtest for me)
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hi koganam,

                          Here is what we think is going on. The OnExecution and OnOrderUpdate methods run asynchronously. When the exit on close happens, the OnExecution is returning slightly before the IOrder object has finished being created and thus does not get returned with the execution object.
                          When adding the OnOrderUpdate override to the script, NinjaTrader has to check the code of the override and this is enough time for the IOrder object to be returned.

                          That is the currently the theory we are testing.

                          One quick question. I have found that this issue does not happen in real time data. Are you finding the same? (In other words, it only seems to happen in backtest for me)
                          That raises a question, Chelsea. Honestly, I thought that I first noticed it when my stats did not update in real time, after printing the "Exit on close" order on the chart, but all my tests after that were with just refreshing the chart (with Strategy) historically. Since then, I have modified the file to use "the Kludge (not yet sure if it actually is one)", so I will have to go back and try to run the test on a live feed. I wonder if you have already verified using Market Replay?

                          Thanks for looking into this: it was something of a surprise when I discovered that my code must not have been run: it seemed so obvious that it would have.

                          Comment


                            #14
                            koganam,

                            I did test this with the Market Replay and was able to capture the exit on close during playback.

                            I'm testing now with a live demo account.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi koganam,

                              I tested with a live demo account and was able to detect the exit on close. So far this seems to be limited to backtesting.

                              Please let me know if you find something different.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              639 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              572 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X