Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot emulate ExitOnClose in Code

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

    Cannot emulate ExitOnClose in Code

    Summary:

    I am trying to emulate the ExitOnClose flag programatically. So, when backtesting I have ExitOnClose set to false and instead I'm using this code:

    Code:

    protected bool NYSEClose() {
    return ToTime(Time[0]) >= ToTime(12, 58, 00);
    }


    protected void CloseEOD() {
    // Closing the Day - only if we trade intraday
    bool closeOpen = intraday ? NYSEClose() : true;
    if (closeOpen)
    {
    if(Position.MarketPosition == MarketPosition.Long)
    ExitLong("Exit Long", "");
    if(Position.MarketPosition == MarketPosition.Short)
    ExitShort("Exit Short", "");
    }
    }

    My setup:
    • I'm running this on a 30min chart against the ES ##-##
    • I have 'intraday' set to true as the default.
    • CalculateOnBarClose is set to false in my Initialize() method
    • At the end of my OnBarUpdate() method I call CloseEOD();
    Problem:

    For some reason I'm not being closed out at 12:58 (I'm in CA, so this is actually 2 minutes before the close of the session). Instead it's closing out the next morning at the first candle: http://screencast.com/t/gFUnogKb

    The reason why I cannot use the ExitOnClose flag is that I am sending myself emails and I seem to be unable to generate an event/alert when the ExitOnClose flag is being triggered.

    Any help would be greatly appreciated. Please ask if anything is unclear.

    #2
    Check your NYSEClose() function to see if you are actually getting the proper bool state returned.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Not sure what you mean...

      Originally posted by NinjaTrader_Josh View Post
      Check your NYSEClose() function to see if you are actually getting the proper bool state returned.
      What do you mean - it's right there in the code I provided. Here it is again:

      protected bool NYSEClose() {
      return ToTime(Time[0]) >= ToTime(12, 58, 00);
      }

      Why would it return the wrong state? Seems pretty obvious to me...

      Michael

      Comment


        #4
        New data

        This appears to be an NT strategy problem. When I set the NYSEClose method to this it closes our at 12:30pm PDT:

        protected bool NYSEClose() {
        return ToTime(Time[0]) >= ToTime(12, 30, 00);
        }

        If I set it to this it closes out the next day:

        protected bool NYSEClose() {
        return ToTime(Time[0]) >= ToTime(12, 31, 00);
        }

        So, this is obviously related to the fact that I'm running this strategy on a 30min chart. Again, I have set CalculateOnBarClose to false. Please advise on how to fix this.

        Michael

        Comment


          #5
          You want to check to see if the function is returning a proper true/false when you want. If you are running on 30 minute charts that for sure will happen. When you check Time[0] you aren't checking time on your PC clock. You are checking the timestamp of the bar.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Josh View Post
            You want to check to see if the function is returning a proper true/false when you want. If you are running on 30 minute charts that for sure will happen. When you check Time[0] you aren't checking time on your PC clock. You are checking the timestamp of the bar.
            Okay, I think I get it - but what is the solution? How often is my onBarUpdate() method being called when I set CalculateOnBarClose to false? Also, if I use the C# date function then I cannot backtest it properly.

            Can you perhaps offer a solution of how to make this work?

            Comment


              #7
              molecool,

              OnBarUpdate() triggers for every incoming tick with CalculateOnBarClose = false in real-time. If you use C# time for sure it won't work in a backtest. You can try using multi-time frames to add more granularity.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                This doesn't work either

                I just tried to use this:

                DateTime t1 = Convert.ToDateTime(DateTime.Now);
                DateTime t2 = Convert.ToDateTime("12:21 PM");

                if (t1.TimeOfDay.Ticks > t2.TimeOfDay.Ticks) {
                Alert("alert", Priority.Medium, "Close Positions", "Alert4.wav", 1, Color.White, Color.Black);
                }
                Doesn't get called either, despite my strategy being set to CalculateOnBarClose = false.

                Not happy that I cannot make this work...

                Comment


                  #9
                  molecool,

                  Manipulating DateTime objects with C# is outside the scope of what we can offer support for.

                  Hint: DateTime.Now is your PC clock time. It is not correlated with anything on your strategy. Alert() only works in real-time also. As always, you need to debug your statements. Print out what that t1.TimeOfDay.Ticks is as well as what the t2 one is.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by molecool View Post
                    I just tried to use this:

                    DateTime t1 = Convert.ToDateTime(DateTime.Now);
                    DateTime t2 = Convert.ToDateTime("12:21 PM");

                    if (t1.TimeOfDay.Ticks > t2.TimeOfDay.Ticks) {
                    Alert("alert", Priority.Medium, "Close Positions", "Alert4.wav", 1, Color.White, Color.Black);
                    }
                    Doesn't get called either, despite my strategy being set to CalculateOnBarClose = false.

                    Not happy that I cannot make this work...
                    Do this:

                    if (DateTime.Now.TimeOfDay > new TimeSpan(12, 21, 00))
                    {
                    ..
                    }

                    EDIT: Oops, read through the thread more closely. As Josh said, if you're doing this in backtesting, you're not going to see any ticks. The "ticks" in DateTime.Now has no relation to stock market ticks.
                    Last edited by heech; 03-25-2009, 03:37 PM.

                    Comment


                      #11
                      A solution

                      Okay, I was able to arrive at a possible solution by adding two different time intervals to my strategy:

                      Add("ES ##-##", PeriodType.Minute, 30);
                      Add("ES ##-##", PeriodType.Minute, 1);

                      So, when calling my indicator in my strategy I use for example:

                      ma = SMA(Closes[0], 20); // not really using MA but just an example

                      Now, the problem with that is that I will have to write a strategy for each contract I want to trade. For instance if I want to trade the NQ then I'll have to make changes to the source:

                      Add("NQ ##-##", PeriodType.Minute, 30);
                      Add("NQ ##-##", PeriodType.Minute, 1);

                      Is there a way around that? In a perfect world I would be able to somehow refer to the current chart's contract and add two separate period types implicitly.

                      Any input would be appreciated.

                      Thanks,

                      Michael

                      Comment


                        #12
                        Just don't call a symbol and it will take whatever base symbol was on the chart for those additional time frames.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          You know what...

                          It's such a crazy idea it might just work! ;-)

                          Yeah, damn - why didn't I think of that - of course you've got that method sig... I'll go away now... LOL

                          Thanks!!

                          Michael



                          Originally posted by NinjaTrader_Josh View Post
                          Just don't call a symbol and it will take whatever base symbol was on the chart for those additional time frames.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          630 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          364 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          566 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          568 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X