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

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 usazencort, Today, 01:16 AM
          0 responses
          1 view
          0 likes
          Last Post usazencort  
          Started by kaywai, 09-01-2023, 08:44 PM
          5 responses
          603 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          6 responses
          22 views
          0 likes
          Last Post xiinteractive  
          Started by Pattontje, Yesterday, 02:10 PM
          2 responses
          21 views
          0 likes
          Last Post Pattontje  
          Started by flybuzz, 04-21-2024, 04:07 PM
          17 responses
          230 views
          0 likes
          Last Post TradingLoss  
          Working...
          X