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 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.
    JesseNinjaTrader Customer Service

    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.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by geddyisodin, Today, 05:20 AM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by Max238, Today, 01:28 AM
        3 responses
        32 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by timko, Today, 06:45 AM
        2 responses
        13 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by habeebft, Today, 07:27 AM
        0 responses
        5 views
        0 likes
        Last Post habeebft  
        Started by Tim-c, Today, 03:54 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X