Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Time[] to calculate seconds between bars

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

    Using Time[] to calculate seconds between bars

    Hi there,

    I'm wondering out loud how to calculate the difference between two bars on a tick chart in terms of the amount of time they take to form (in seconds).

    Right now I'm using the following code, but I'm attempting (wrongly) to convert a built-in DateTime method into an int. Any advice?

    Code:
    int
    bar_Time,
    bar_TimePrev,
    bar_TimeDiff;
    
    bar_Time = Time[1];
    bar_TimePrev = Time[2];
    					
    if (bar_TimePrev != 0)
    	{	bar_TimeDiff = bar_TimePrev - bar_Time;	}
    else
    	{	bar_TimeDiff = bar_Time;	}
    If I can't use the Time function, I'll work within DateTime and try to get it working that way via the following thread:

    I need a function that can return the difference between the below two dates as 24. DateTime a = new DateTime(2008, 01, 02, 06, 30, 00); DateTime b = new DateTime(2008, 01, 03, 06, 30, 00);


    Thanks in advance!

    #2
    Hello,

    You should be able to use ToTime() to get an integer representation of the DateTime object that will include the seconds. You can then subtract the minutes and seconds from both bars to determine how much time has passed. For more information on using ToTime(), please see the link below:

    http://www.ninjatrader.com/support/h...tml?totime.htm

    Please let me know if I can assist further.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Doh!

      Hi Dave,

      The problem with referencing the Help Guide is that sometimes the examples don't quite illustrate how to use the function for certain situations.

      All the same, here's the corrected code. The problem with using ToTime is that it generates whole numbers. If I wanted to extract the minute/second from the Time[0] DateTime element, I'm not sure how to do that.

      Code:
      bar_Time = ToTime(Time[1]);
      bar_TimePrev = ToTime(Time[2]);
      					
      if (bar_TimePrev != 0)
      	{	bar_TimeDiff = bar_Time - bar_TimePrev;	}
      else
      	{	bar_TimeDiff = bar_Time;	}
      Basically the above gives me a whole number of '4066' if bar_Time = 90022 and bar_TimePrev = 85956.

      Can you be specific on how the code should be referenced to extract minute/second from a ToTime() extracted value?

      For reference, we can't do the following unless bar_Time is redefined as a DateTime element:
      Code:
      bar_Time = ToTime(Time[1].Minute);
      Last edited by Spiderbird; 03-28-2015, 12:06 AM.

      Comment


        #4
        Originally posted by Spiderbird View Post
        Hi Dave,

        The problem with referencing the Help Guide is that sometimes the examples don't quite illustrate how to use the function for certain situations.

        All the same, here's the corrected code. The problem with using ToTime is that it generates whole numbers. If I wanted to extract the minute/second from the Time[0] DateTime element, I'm not sure how to do that.

        Code:
        bar_Time = ToTime(Time[1]);
        bar_TimePrev = ToTime(Time[2]);
        					
        if (bar_TimePrev != 0)
        	{	bar_TimeDiff = bar_Time - bar_TimePrev;	}
        else
        	{	bar_TimeDiff = bar_Time;	}
        Basically the above gives me a whole number of '4066' if bar_Time = 90022 and bar_TimePrev = 85956.

        Can you be specific on how the code should be referenced to extract minute/second from a ToTime() extracted value?

        For reference, we can't do the following unless bar_Time is redefined as a DateTime element:
        Code:
        bar_Time = ToTime(Time[1].Minute);
        Because of the Modulo60 arithmetic involved in hours/minutes/seconds, you are better off subtracting DateTime structures to get a TimeSpan structure, from which you can extract the needed property.

        Here is an example off the top of my head. Let us know if I have made a mistake, please.
        Code:
        double dblTimeElapsed = (Time[0] - Time[1]).TotalSeconds;

        Comment


          #5
          Thanks!

          Hi Koganam!

          Good to see you again. Been a while since you've helped me out on my last coding problem.

          The code sample you provided looked great but it did generate an error. Still, with your help, I went ahead and ventured down the DateTime road and created a solution. Here's what it looks like:

          Code:
          DateTime bar_Time = new DateTime();
          DateTime bar_TimePrev = new DateTime();
          TimeSpan bar_TimeDiff;
          double bar_TimeDuration;
          
          bar_Time = Time[1];
          bar_TimePrev = Time[2];
          					
          if (bar_TimePrev != null)
          	{	bar_TimeDiff = (bar_Time - bar_TimePrev);	}
          					
          bar_TimeDuration = bar_TimeDiff.TotalSeconds;
          Thanks again to everyone who took time to respond.

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