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

Exit Orders Submitted via OnExecutionUpdate Exit Early

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

    Exit Orders Submitted via OnExecutionUpdate Exit Early

    Consider the following example:

    I am using High Order Fill Resolution! This seems to be important to see the issue.

    I have a strategy that does the following during historical backtesting.

    1) If flat and some conditions are true, then I EnterLong
    2) In OnExecutionUpdate, I submit an ExitLongStopMarket with a certain stop price

    On subsequent bars in OnBarUpdate, my code continues to test if we're flat and if we are flat, then may submit a new entry order.

    It's pretty simple.

    What I've found is that the stop exit appears to execute a full bar early sometimes which leaves the position flat and a new entry order is submitted in OnBarUpdate.

    Then we're actually entering with a market order at the next bar open BEFORE the stop order would have exited during that next bar.

    I seem to recall reading something in the documentation about orders submitted from OnExecutionUpdate, that the processing engine will actually look ahead one bar when processing.

    This seems to be confirmed by my observations, but I would really like to know why this is done this way.

    This seems to violate the most sacred tenet of back testing that you should never use future information.

    Plus it creates situations that couldn't happen in the real world. I would never have entered that entry order until one bar later because the position would still be open until the middle of the next bar when it gets stopped out.

    I suspect you know what I'm talking about here, but if not, see the sample strategy code below to recreate the scenario.

    To be clear, I think this behavior is probably intentional by NT. I would just like to understand why.

    Thanks,
    Steve

    Code:
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using System;
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies.Test
    {
        public class EarlyExecutionTest : Strategy
        {
            protected override void OnBarUpdate()
            {
                // Load "ES 09-12" Day bars from 5/1/2012 - 8/1/2012
    
                if (Time[0].Date == DateTime.Parse("2012-06-13").Date)
                {
                    var order = EnterShort("Entry1");
                }
                if (Time[0].Date == DateTime.Parse("2012-06-15").Date)
                {
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        // This entry happens at the open of the 6/18 bar, but the
                        // previous position doesn't "technically" exit until
                        // some time during the 6/18 bar, after the open.
                        EnterLong();
                    }
                }
                if (Time[0].Date == DateTime.Parse("2012-06-22").Date)
                {
                    ExitLong();
                }
            }
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                if (execution.Order.Name == "Entry1")
                {
                    ExitShortStopMarket(0, true, 1, 1347, "", "");
                }
            }
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name = "Early Execution Test";
                    IsExitOnSessionCloseStrategy = false;
                    Slippage = 0;
                    TraceOrders = true;
                    BarsRequiredToTrade = 0;
                    EntriesPerDirection = 1;
                    OrderFillResolution = OrderFillResolution.High;
                    OrderFillResolutionType = BarsPeriodType.Minute;
                    OrderFillResolutionValue = 1;
                }
            }
        }
    }
    Last edited by Steve L; 08-10-2019, 10:36 AM.
    Steve L
    NinjaTrader Ecosystem Vendor - Ninja Mastery

    #2
    Hello Steve L,

    I just wanted to post a quick note letting you know we have seen your post and will be reviewing it further. Once I have more details I will reply back here.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Any updates on this?

      Thanks,
      Steve
      Steve L
      NinjaTrader Ecosystem Vendor - Ninja Mastery

      Comment


        #4
        Hello Steve L,

        Not currently, This is in review at the moment but no additional details have been provided that I can relay. Once I have more details or tracking number for this request I will reply back. I am actively watching this issue so I will provide updates as they get posted.

        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          OK. Thanks.
          Steve L
          NinjaTrader Ecosystem Vendor - Ninja Mastery

          Comment


            #6
            Hello Steve L,

            I wanted to provide a follow up on this issue.

            It looks like this is expected based on the data available at the specified test time and based on how bars process in historical. ​The issue is the daily bar update is not yet seen (next daily bar open needs to happen), when the fill on the 1min series is seen. While you get the fill on the 1min the previous Daily bar is not closed yet. The 6/15/2012 2:15:00 PM is the last 1-minute bar of the day which is part of why we see this because here the order is filled with the timestamp of the next BIP1 bar which is 6/17/2012 4:01:00 PM. Development has made a note of this situation to better document going forward in future releases however it does seem to be expected based on the variables surrounding the situation.



            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GussJ, 03-04-2020, 03:11 PM
            11 responses
            3,221 views
            0 likes
            Last Post xiinteractive  
            Started by andrewtrades, Today, 04:57 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by chbruno, Today, 04:10 PM
            0 responses
            6 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
            9 views
            0 likes
            Last Post FAQtrader  
            Working...
            X