Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Elapsed time on a historical bar

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

    Elapsed time on a historical bar

    I am trying to calculate the number of ticks over a given number of seconds within a bar on a rolling basis, i.e. if the period is 30 seconds, then it would initially count ticks between 0 and 30 seconds, then between 1 and 31 seconds , then between 2 and 32 seconds, etc..
    One idea was to store the values of Tick Count at 1 second intervals and then calculate the difference between the counts at the relevant intervals, or alternatively to add a 1 tick Data series and then use the time stamps to get the bar index on the 1 tick series at each of the two time stamps. At the start of the bar, obviously I can use the bar’s time stamp and add 30 seconds to that time to give the end of the period, but how then do I increment the start/end time stamps each second? In real time I can use DateTime.Now to determine when one second has elapsed, but how can I determine that one second has elapsed on a historical bar? I may well be missing something obvious, so my apologies if this is a dumb question.
    Thanks in advance for any help.


    #2
    Welcome to the forums Scotty33!

    We only have the timestamp of the bar in a historical context, so I may suggest the following:
    1. Create a Multi Time Frame script that adds a 1 second data series
    2. Create an array with 30 elements. I suggest creating the array in OnStateChange under State.DataLoaded when the script starts.
    3. Whenever the 1 second bar closes, assign TickCount to a specific index in your array and increment your index
    4. When your index reaches 30, set it back to 0 so you start assigning values back at the first element in your array.
    Whenever you want to get the total number of ticks for the last 30 seconds, you can loop through this array and add each element together.

    Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

    A publicly available resource on using arrays can be found here - https://www.tutorialspoint.com/csharp/csharp_arrays.htm

    We look forward to assisting.

    Comment


      #3
      Thanks for your reply, Jim.
      One thing I'm not clear about - your solution is based upon using the tick count of the 1-second series bars, but how do I reference that Tick Count?

      I tried this:

      if (BarsInProgress == 1)
      {
      if (CurrentBar > LastSecIndex) // one second bar has closed
      {
      LastSecIndex = CurrentBar;
      BarTicks[SecIndex] = Bars.TickCount; // store tick count

      but that always seems to return a value of 1 for the tick count.
      Thanks for any help.

      Comment


        #4
        Hello Scotty33,

        We will need to use Tick Replay and have Calculate set to OnEachTick in order to use TickCount.

        Please see documentation note below.

        TickCount - https://ninjatrader.com/support/help...?tickcount.htm

        Tick Replay - https://ninjatrader.com/support/help...ick_replay.htm

        Developing for Tick Replay - https://ninjatrader.com/support/help...ick_replay.htm

        We look forward to assisting.

        Comment


          #5
          Thanks for your help, Jim. Seems to be working now.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          602 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          347 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          559 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          559 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X