Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Session Times

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

    Session Times

    I’m trying to implement having a strategy stop trading at a certain time of day. I need code the time comparison in a time-zone neutral way. In other words, it should work in any time zone.
    I’ve coded it as follows. ( the _strTimeSpanDuration should be something like 6:30 to mean trade for 6 1/2 hours from the open )
    Code:
    // In the OnStartUp() method:
             TimeSpan timeSpanSessionStart = Bars.Session.NextBeginTime.TimeOfDay;
             timeSpanDuration = TimeSpan.Parse(_strTimeSpanDuration);
    
    // the time rule will return false if trading should stop
    protected virtual bool TimeRule()
          {
             TimeSpan timeSpanSessionStart = Bars.Session.NextBeginTime.TimeOfDay;
             TimeSpan timeSpanTradingStop = timeSpanSessionStart.Add(timeSpanDuration);
             return this.Time[0].TimeOfDay < timeSpanTradingStop;
          }
    I’m not sure if this is the right way to do it. I expect that the times I get from the session are the times associated with the session template, adjusted for the user’s locale. In other words, if the session template is specified in Chicago and the user’s computer is in the Eastern time, then the Session.NextBeginTime.TimeOfDay should return 9:30 AM for the CME Stock Index Futures RTH template. If that’s true then the timestamps on the bars ( always in local time? ) can be compared to that time.

    Is this all correctly done? It is working on my computer ( Easter Time ) but I don't know if it's correct for other zones.

    #2
    Hello,

    You may be able to go about this an easier way, rather than using TimeOfDay. You could simply instantiate a DateTime object at the beginning of the session, then halt trading after a certain number of hours has passed, like so:

    DateTime startTime = DateTime.Now;
    DateTime endTime = startTime.AddHours(6.5);

    protected override void OnBarUpdate()
    {
    if (DateTime.Now == endTime) Disable();
    }

    Alternatively, you might consider using GetNextBegin End, which returns the date and time representing the next beginning/end of a session:

    http://www.ninjatrader.com/support/h...ime_vs_bac.htm
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Session Times

      It should work the way I have it coded but I just want to confirm that. The way you recommended might work for real time trading but it won't for historical bars.
      The key to my approach working is this:
      TimeSpan timeSpanSessionStart = Bars.Session.NextBeginTime.TimeOfDay;
      This line actually returns the time that the session starts in the local time of the computer. For example, if I run this code on my computer in the eastern time zone for a session template that's CME Stock Index Futures RTH ( for which the sessions are specified to start at 8:30 AM in the exchange time ) then I get a time: 9:30am ( 9:30am eastern time == 8:30 am chicago/exchange time )

      Comment


        #4
        That sounds good. If everything is working well the way you have it, then there is probably no need to make any changes.
        Dave I.NinjaTrader Product Management

        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
        332 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