Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Another timezone question

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

    Another timezone question

    I am developing a strategy that takes a trade at a very specific time. Let's say 11:05 am Pacific Time.

    I have code as follows

    Code:
            
            private void TakeTrade()
            {
                TimeSpan timeToTakeTrade = new TimeSpan(11, 5, 0);
                DateTime Min5Time = Times[1][0];
                if (BarsInProgress == 1)    // 5 minute bars
                {
                    if (Min5Time.TimeOfDay.CompareTo(timeToTakeTrade) == 0)
                        ProcessTrade();
                }
                else if (BarsInProgress == 0)    // daily bars
                {
                    // do nothing
                }
            }
    This works on my computer in Oregon, however, I am going to distribute this to people all over the country and it will not be accurate if they are running it say in Florida which is Eastern Time.

    What do I need to do to set the timeToTakeTrade value correctly? Do I get the timezone from the user's computer or is there a better way?

    Thanks
    Last edited by ExactlyMyPoint; 02-18-2011, 02:57 PM.

    #2
    You could use DateTime.Now() from the framework to get the time directly from the user's computer

    Comment


      #3
      But I need to know the equivalent of 11:05 am PST in whatever timezone the person is running on, not the current time.

      Comment


        #4
        Ah got it, I misunderstood. You can use TimeZoneInfo.ConvertTime to convert a set DateTime between timezones pretty easily. Try this

        Code:
        DateTime myTradeTimer = new DateTime(2011, 02, 17, 11, 05, 00);
        TimeZoneInfo PacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
        TimeZoneInfo EasternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        DateTime inEST = TimeZoneInfo.ConvertTime(myTradeTimer, EasternTimeZone); // returns a DateTime in EST
        DateTime inPST = TimeZoneInfo.ConvertTime(myTradeTimer, PacificTimeZone); // returns a DateTime in PST
        myTradeTimer is just set to today, but you can ignore day of course and use Hour, Minute or whatever from inEST, inPST and so forth.
        Last edited by Dexter; 02-18-2011, 03:25 PM.

        Comment


          #5
          Thanks, Dexter. ToUniversalTime() can also be useful for this to convert all times into the same time zone. You could add / subtract from this to get to pacific.
          Converts the value of the current DateTime object to Coordinated Universal Time (UTC).
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Something occured to me at lunch! There was a minor logic error in my example, so it would give incorrect results because DateTime uses local timezone, converting to PST would push the date off of your local timezone. Try this, it will give correct time zones on any computer (using UTC):

            Code:
            DateTime tradeTime = new DateTime(2011, 2, 17, 19, 5, 00, DateTimeKind.Utc); // this is 11:05 PST in UTC
            DateTime inPST = TimeZoneInfo.ConvertTime(tradeTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
            DateTime inEST = TimeZoneInfo.ConvertTime(tradeTime, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
            DateTime inMST = TimeZoneInfo.ConvertTime(tradeTime, TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"));
            DateTime inCST = TimeZoneInfo.ConvertTime(tradeTime, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmoran13, Yesterday, 01:02 PM
            0 responses
            30 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            22 views
            0 likes
            Last Post PaulMohn  
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            160 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            95 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            148 views
            2 likes
            Last Post CaptainJack  
            Working...
            X