Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to time events?

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

    How to time events?

    Hi NT,

    I'd like simply to calculate the time ( in secs ) between two events, but I'm not sure about which is the best, simple and reliable method.

    I've seen the typical example:
    DateTime.Now.Ticks - Time[0].Ticks

    Can I do the same but getting the seconds of difference? I'm not used to manage Time in C#, so I don't know what a tick means in time.

    Any help would be great


    Thanks in advance

    #2
    Hello,

    Thank you for the question.

    One way of doing this would be using DateTime.Subtract, here is a simple example that checks the amount of seconds between 1 bar ago and the Current computer time:


    Code:
    if(Historical) return;
    DateTime timeA = DateTime.Now;
    DateTime timeB = Time[1];
    
    int seconds = timeA.Subtract(timeB).Seconds;
    Print(seconds);
    Here is more information on DateTime and TimeSpan, TimeSpan is the object Subtract returns.

    Represents an instant in time, typically expressed as a date and time of day.



    I look forward to being of further assistance.

    Comment


      #3
      2 questions:

      - That difference in seconds would it be an integer value, right ? or would it be a Time structure data?

      - Let's suppose that we have a difference > 60 secs, your procedure would give a value > 60 or other form?

      Comment


        #4
        Hello,

        For a total time in seconds greater than 60 seconds, you could use the TotalSeconds property, for a complete list of options, please refer to the MSDN documentation on TimeSpan.

        here is a simple test you can do on your end to see that this produces 180 seconds for 3 minutes of a TimeSpan:

        Code:
        TimeSpan span = new TimeSpan(0, 3, 0);
        Print(span.TotalSeconds);
        The Property would be an int type, the span would be your TimeSpan object.

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        152 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        87 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        131 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        127 views
        1 like
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        106 views
        0 likes
        Last Post CarlTrading  
        Working...
        X