Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to convert TradingHours Sessions[i].BeginTime to Chart TimeZone or vice versa?

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

    How to convert TradingHours Sessions[i].BeginTime to Chart TimeZone or vice versa?

    Hey guys,

    I need to compare Time[0] with tradingHours.Sessions[i].BeginTime in OBU.
    Should I convert Time[0] to the template's TimeZone of my TradingHours custom property or the other way around?
    How to do this conversion in both directions?​

    #2
    Hello rafaelcoisa,

    Thank you for your post.

    You may get the TimeZoneInfo from both the trading hours template (Bars.TradingHours.TimeZoneInfo) and the set time zone configured at Tools > Options > General > Preferences (Core.Globals.GeneralOptions.TimeZoneInfo.) Based on that information, you could choose which way you would like to convert the time zone to suit your needs. Here are some helpful links and examples:
    Code:
    // Print the timezone before printing all sessions
    Print(String.Format("All sessions are in {0}", Bars.TradingHours.TimeZoneInfo));​
    Code:
    Print(String.Format("The application is currently operating in {0}", Core.Globals.GeneralOptions.TimeZoneInfo));
    Converting times between time zones is a general C# concept and is not specific to NinjaScript. For more details on how to convert times between time zones, please see the following publicly available link:Please let us know if we may be of further assistance.

    Comment


      #3
      Hey NinjaTrader_Emily,

      In my Trading Hours window, the time is displayed in 24h format.
      Is this the same for all users worldwide or do some users have this in other formats like AM/PM?
      Will the int returned from "tradingHours.Sessions[i].BeginTime" always be in 24 hour format or not necessarily?​

      Comment


        #4
        Hello rafaelcoisa,

        Thank you for your reply.

        This is not the same for all users worldwide; whether the format uses 24 hours or has AM/PM formatting will depend on the Windows date/time/region settings that the user has configured. NinjaTrader bases its formatting on the Windows settings for the device that NinjaTrader is running on.

        Feel free to reach out with any additional questions or concerns.

        Comment


          #5
          Just to confirm.
          "tradingHours.Sessions[i].BeginTime" also?
          For exemple, the output of tradingHours.Sessions[i].BeginTime could be something like this: 215 meaning 2:15:00 PM?​

          Comment


            #6
            Hello rafaelcoisa,

            Thank you for your reply.

            For TradingHours.Sessions[i].BeginTime, the value returned is an int value in a 24 hour format; I apologize I did not address that question specifically and only addressed the Trading Hours window formatting. For example, I have my Windows settings set to English (United States) which gives me AM/PM time on my system. The times in the Trading Hours window show AM/PM values, though when I print the TradingHours.Sessions[i].BeginTime it returns in 24-hour format. 2:15:00 PM would show up as 1415. For example, I have the following snippet of code in a test indicator:

            Code:
            Print(String.Format("All sessions are in {0}", Bars.TradingHours.TimeZoneInfo));​
            for (int i = 0; i < TradingHours.Sessions.Count; i++)
            {
            Print(String.Format("Session {0}: {1} at {2} to {3} at {4}.  Current Bar Time: {5}", i, TradingHours.Sessions[i].BeginDay, TradingHours.Sessions[i].BeginTime,
            TradingHours.Sessions[i].EndDay, TradingHours.Sessions[i].EndTime, Time[0]));
            }
            Print(String.Format("The application is currently operating in {0}", Core.Globals.GeneralOptions.TimeZoneInfo));
            ​
            Here is an example of the output for a chart using the CME US Index Futures ETH trading hours template:
            All sessions are in (UTC-06:00) Central Time (US & Canada)
            Session 0: Sunday at 1700 to Monday at 1600. Current Bar Time: 7/3/2023 11:14:00 AM
            Session 1: Monday at 1700 to Tuesday at 1600. Current Bar Time: 7/3/2023 11:14:00 AM
            Session 2: Tuesday at 1700 to Wednesday at 1600. Current Bar Time: 7/3/2023 11:14:00 AM
            Session 3: Wednesday at 1700 to Thursday at 1600. Current Bar Time: 7/3/2023 11:14:00 AM
            Session 4: Thursday at 1700 to Friday at 1600. Current Bar Time: 7/3/2023 11:14:00 AM
            The application is currently operating in (UTC-07:00) Mountain Time (US & Canada)​
            I hope this helps to clarify. Please let us know if we may be of further assistance.

            Comment


              #7
              Now it's clear.
              Thanks.​

              Comment


                #8
                Hey NinjaTrader_Emily,

                I figured out how to convert the time and do the comparison. Now, I have an NT problem.
                When I open a chart with "CME US Index Futures RTH" trading hours sometimes the first bar is 1 hour ahead in time.
                For example, from 01/01/2023 to 03/10/2023. All first bars are 1 hour ahead in time.
                Where is the problem here?

                Comment


                  #9
                  I open a chart like this:
                  Click image for larger version

Name:	0a1 dataserieswindows1.png
Views:	261
Size:	33.3 KB
ID:	1258872

                  I run this code to get the time of the first bar:
                  Click image for larger version

Name:	0a1 code1.png
Views:	260
Size:	35.7 KB
ID:	1258873

                  and this are the results:
                  Click image for larger version

Name:	0a1 firstbarwrongtime1.gif
Views:	262
Size:	751.0 KB
ID:	1258874

                  As you can see, at the beginning of the year the first bar has the wrong time. It should have been 10:30 and not 11:30.​

                  Comment


                    #10
                    Hello rafaelcoisa,

                    Thank you for your reply.

                    The difference in the time that you have described has to do with Daylight Savings Time. There was a time change to move forward an hour on the second Sunday of March, and the CME followed this time change. The time moves backward an hour on the first Sunday of November, for example on 11/6/2022 and 11/5/2023.

                    Please let us know if we may be of further assistance.

                    Comment

                    Latest Posts

                    Collapse

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