Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code Execution Sequence

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

    Code Execution Sequence

    I would like to clarify code execution between the OnBarUpdate and OnExecution event handlers.

    Suppose I get a signal and EnterLong() in OnBarUpdate. Code in my OnExecution handler will fire. Is it true that as soon as EnterLong() is called, the OnExecution handler runs and completes and then returns to execute the line of code following EnterLong() in OnBarUpdate?

    #2
    Hello reynoldsn,

    Thank you for your inquiry.

    The OnExecution() method runs on an incoming execution (or fill) of an order. It is entirely possible for other code to execute before OnExecution() is even called when using EnterLong(). Here's a simple example to test this out:
    Code:
    private int realTimeTicks = 0;
    
    protected override void OnBarUpdate()
    {
         if (realTimeTicks == 150)
         {
              EnterLong();
              Print("EnterLong() called!");
         }
    
         if (!Historical)
         {
              realTimeTicks++;
              Print(String.Format("{0} ticks", realTimeTicks));
         }
    }
    
    protected override void OnExecution(IExecution execution)
    {
         if (!Historical)
         {
              Print("OnExecution() called!");
         }
    }
    In my output window:
    Code:
    EnterLong() called!
    151 ticks
    152 ticks
    153 ticks
    154 ticks
    155 ticks
    156 ticks
    157 ticks
    158 ticks
    159 ticks
    160 ticks
    161 ticks
    162 ticks
    163 ticks
    164 ticks
    165 ticks
    166 ticks
    167 ticks
    168 ticks
    169 ticks
    170 ticks
    171 ticks
    172 ticks
    173 ticks
    174 ticks
    175 ticks
    176 ticks
    177 ticks
    178 ticks
    179 ticks
    180 ticks
    181 ticks
    182 ticks
    183 ticks
    184 ticks
    185 ticks
    186 ticks
    187 ticks
    188 ticks
    189 ticks
    190 ticks
    191 ticks
    192 ticks
    193 ticks
    194 ticks
    195 ticks
    196 ticks
    197 ticks
    198 ticks
    OnExecution() called!
    As you can see, ticks were still printing out after we called EnterLong() and before OnExecution() was called.

    To reiterate, OnExecution() is not called directly after an EnterLong(). It is called once an order is filled.

    Here is a link to our help guide for more information about OnExecution(): http://ninjatrader.com/support/helpG...nexecution.htm
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      OK, I wasn't clear on this and it turns out I need to fix my strategy accordingly.

      However, I only found it as I let the strategy run in sim mode against a live data feed. This problem has never been seen during Market Replay or using the Backtester after many, many tests. I wonder why the Market Replay simulation engine never exposed this. Can you please explain?

      Comment


        #4
        Originally posted by reynoldsn View Post
        OK, I wasn't clear on this and it turns out I need to fix my strategy accordingly.

        However, I only found it as I let the strategy run in sim mode against a live data feed. This problem has never been seen during Market Replay or using the Backtester after many, many tests. I wonder why the Market Replay simulation engine never exposed this. Can you please explain?
        There is really nothing to explain. The Execution event has nothing to do with the BarUpdate event, so how they relate is essentially indeterminate. BarUpdate is an event triggered by a tick, Execution is triggered by an execution, whenever the order is executed, which can be anytime after the order is issued.

        Yes, even market orders do not always execute immediately; at least not before other ticks may have come in. How an order executes is determined by the exchange, not NT.
        Last edited by koganam; 08-20-2015, 09:15 PM. Reason: Corrected spelling.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 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