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

How do you subtract one time from another?

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

    How do you subtract one time from another?

    I want to subtract one time from another and display the difference in minutes.

    Time1 = 14:30:00

    Time2 = 13:00:00

    TimeLeft = (Time1 - Time2);

    TimeLeft = 90

    What is the syntax for that? I'm not sure how to initialize the DateTime structure for a specific time.

    Thanks,
    R.C.

    #2
    Hello,

    "TimeLeft" would be a TimeSpan structure:



    Code:
    			DateTime Time1 = new DateTime(143000);
    
    			DateTime Time2 = new DateTime(130000);
    
    			TimeSpan TimeLeft = (Time1 - Time2);
    
    			Print(TimeLeft.ToString());
    MatthewNinjaTrader Product Management

    Comment


      #3
      Couple notes:
      1. Initializing a DateTime like DateTime Time1 = new DateTime(143000); is actually initializing the datetime with ticks which equal 100 nanoseconds. There are 10 million ticks per second, so this is not the same as 14:30:00.
      2. timeLeft.ToString() will not display the difference in minutes but will instead give the time in hh:mm:ss format. You will want to use timeLeft.TotalMinutes.ToString().


      Also you can optionally use TimeSpan objects instead of DateTime here:

      Code:
      TimeSpan time1 = new TimeSpan(14, 30, 0);
      TimeSpan time2 = new TimeSpan(13, 0, 0);
       
      TimeSpan timeLeft = time2 - time1;
      Print(Convert.ToString(timeLeft.TotalMinutes));

      Comment


        #4
        Thanks for the responses. This is what I'm seeing

        DateTime _090000 = new DateTime(090000);

        TimeSpan TimeLeft = Time[0] - _090000;

        Print(Time[0] + " " + TimeLeft.TotalMinutes.ToString() + "Minutes Left Until Open Outcry");

        This is what I'm getting ..

        9/5/2012 8:41:04 AM 1058040521.06652 Minutes Left Until Open Outcry

        I expected to see 19 Minutes. Do you see the syntax error?

        Thanks!

        Comment


          #5
          Originally posted by rcsingleton View Post
          Thanks for the responses. This is what I'm seeing

          DateTime _090000 = new DateTime(090000);

          TimeSpan TimeLeft = Time[0] - _090000;

          Print(Time[0] + " " + TimeLeft.TotalMinutes.ToString() + "Minutes Left Until Open Outcry");

          This is what I'm getting ..

          9/5/2012 8:41:04 AM 1058040521.06652 Minutes Left Until Open Outcry

          I expected to see 19 Minutes. Do you see the syntax error?

          Thanks!
          DateTime(090000) is not the same as DateTime(09,00,00).

          CodeButterfly, already in this thread, explained the difference and why you would get incorrect results if you use your syntax.

          Comment


            #6
            If you're comparing to Time[0], which is a DateTmie structure, you will need to fullfill all the required overloads.

            DateTime _090000 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 0, 0);

            This will give you the current date, and then allow you to specify 9:00AM

            Then you can subtract your 09:00AM DateTime from the Time of the current bar:

            TimeSpan TimeLeft = _090000 - Time[0];

            Print(Time[0] + " " + TimeLeft.TotalMinutes.ToString() + "Minutes Left Until Open Outcry");
            MatthewNinjaTrader Product Management

            Comment


              #7
              Compile Error

              Thanks Matthew for the response.

              I'm getting the following compiler error on your syntax:

              DateTime _090000 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 0, 0);

              Error Message:

              An object reference is required for the non-static field, method, or property 'Ninja.Trader.Indicator.IndicatorBase.Time.get'

              Any ideas?

              Comment


                #8
                I got it working..

                DateTime _090000 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 0, 0);
                DateTime _timeNow = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, Time[0].Second);

                System.TimeSpan _diff = _timeNow.Subtract(_090000);
                Print(Time[0] + " " + _diff.ToString() + " Minutes Left Until Open Outcry");

                Thanks to everyone!

                Comment


                  #9
                  On a style note, usually you want to reserve underscore prefixed variables for member variables (fields) not local variables (e.g. variables you define inside your OnBarUpdate() method).

                  The way I would modify this would be (untested):

                  DateTime mar****penTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 0, 0);
                  TimeSpan timeRemainingUntilOpenOutcry = mar****penTime - Time[0];
                  Print(Time[0] + " " + timeRemainingUntilOpenOutcry.TotalMinutes.ToString () + " Minute(s) Left Until Open Outcry");

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, Yesterday, 06:40 PM
                  2 responses
                  20 views
                  0 likes
                  Last Post algospoke  
                  Started by ghoul, Today, 06:02 PM
                  3 responses
                  14 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by jeronymite, 04-12-2024, 04:26 PM
                  3 responses
                  45 views
                  0 likes
                  Last Post jeronymite  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  7 responses
                  21 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  10 responses
                  181 views
                  0 likes
                  Last Post jeronymite  
                  Working...
                  X