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

Subtracting minutes from ToTime Integer

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

    Subtracting minutes from ToTime Integer

    Hi,

    I have sometime like this in my code:

    Code:
    int startTime = 93000
    int endTime = 155000
    
    if (ToTime(Time[0])<startTime || ToTime(Time[0])>endTime - [minutes])
       stopEntry = true;
    The [minutes] part is where I'm having issues with. How can code it so that I can specify the number of minutes to subtract from endTime? If I'd like it to end 50 minutes before endTime, simply coding

    Code:
    endTime - 5000
    does not really work in all situations (i.e. if endTime was 160000, then 160000 - 5000 would be 155000, or 3:50pm instead of 3:10pm).

    Any help would be much appreciated. Thanks.

    -Nick

    #2
    Update:

    This is what I managed to knock out:


    Code:
    myDateTime = DateTime.ParseExact(endTime.ToString(), "HHmmss", CultureInfo.InvariantCulture);
    myDateTime = myDateTime.AddMinutes(-20);
    				
    string format = myDateTime.ToString("HHmmss");
    int endTimeFinal = Int32.Parse(format);
    It's brutal. It's ugly. It's probably a lot longer than it needs to be. But it works.

    Any suggestions on how I can "elegant-ize" it?

    Thanks!

    Comment


      #3
      Update #2:

      Ahh, the wonders of stackoverflow.

      Replaced:

      Code:
      string format = myDateTime.ToString("HHmmss");
      int endTimeFinal = Int32.Parse(format);
      with

      Code:
      int endTimeFinal = int.Parse(myDateTime.ToString("HHmmss"));
      So far, so good!

      Comment


        #4
        Timespan?

        Use TimeSpan structs. TimeSpan represents a period of time and has many helpful methods.

        Comment


          #5
          Originally posted by nicbizz View Post
          Update:

          This is what I managed to knock out:


          Code:
          myDateTime = DateTime.ParseExact(endTime.ToString(), "HHmmss", CultureInfo.InvariantCulture);
          myDateTime = myDateTime.AddMinutes(-20);
          				
          string format = myDateTime.ToString("HHmmss");
          int endTimeFinal = Int32.Parse(format);
          It's brutal. It's ugly. It's probably a lot longer than it needs to be. But it works.

          Any suggestions on how I can "elegant-ize" it?

          Thanks!
          You evidently know how to code with DataTime objects. Why not just use them directly? After all, Time[n] objects are already DateTime objects.

          Not to put a fine point on it, but that is far more elegant than all this trying to pretend "times" are integer objects.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Aviram Y, Today, 05:29 AM
          4 responses
          11 views
          0 likes
          Last Post Aviram Y  
          Started by algospoke, 04-17-2024, 06:40 PM
          3 responses
          27 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by bmartz, 03-12-2024, 06:12 AM
          3 responses
          31 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by gentlebenthebear, Today, 01:30 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by cls71, Today, 04:45 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X