No announcement yet.

Partner 728x90


Way to capture Trade Time between two points (weekends)

  • Filter
  • Time
  • Show
Clear All
new posts

    Way to capture Trade Time between two points (weekends)


    I have a script I've been working with, it plots zigzags. One of the metrics I am collecting is the time between two points or bars.

    Currently, I'm using something like this:
    upClkTime = (int)(Time[CurrentBar-highBar].Subtract(Time[CurrentBar-dopenBar]).TotalMinutes);
    This works fine, but if there are swings that span over a weekend, or time where trading is not occurring, it will add in the amount of time from the weekend too.

    What I will probably do, is create a loop to count back through the bars in between the points and add it up over each bar and report that result. But that is not the most elegant solution.

    So I am wondering if there is a simpler way to do this, like a built-in NT method of collecting trading time that I am unaware of, before adding in my "brute-force" solution?


    Hi forrestang,

    There is not a pre-made NinjaScript function that will calculate the time between bars excluding time that the session is closed.

    This may be somewhat advanced to code.

    You can find the start and end times of a session using Bars.Session.GetNextBeginEnd(). However, this will not let you know if a particular day is missing from the session.

    You could also try moving backwards by day and using Bars.GetBar(DateTime) at the time of the opening of the session to see what bar is returned. If the date in question does not have a corresponding bar, then the next bar from oldest to newest is returned. In other words, it will return a bar from the next day which means there is no data for that day.

    This would need some interesting logic. If you want the script created for you I suggest you contact a professional NinjaScript Consultant.
    Chelsea B.NinjaTrader Customer Service


      Thanks for the suggestion. While I didn't do it quite the way you described, you did give me an idea for separating the sessions.

      So, I've given each day a number from 1 and upwards in sequence. That number is assigned to each bar.... so all the bars on the first day are 1, all the bars on the 2nd day are 2, and so on. That data series is named "sessionCountSeries."

      Basically given two bars that my swing starts and ends on, I start at the beginning... and if that bar is of the SAME session as the bar that precedes it, I add the time in total seconds to a variable. If it is not of the same session, it could be a weekend or NON-trading period, so don't add it to the variable.

      Then convert back to minutes at the end. The reason I am using seconds to store the variable is b/c I want to have my final output in a minute resolution for now. Obviously this can be changed depending on the requirements of the user.

      Attached is a picture that I think explains it. Pay attention to the highlighted areas. I am showing the example of the one swing, that spans across a weekend, and you can see the difference in times displayed. The larger number I have highlighted is calendar time, and the smaller number is the "Trading Time."

      You can see to verify the measurements of time on the Ruler tool, as I spanned it across the two individual segments and it adds up.

      //DrawLine("s2isUp"+CurrentBar,false, CurrentBar-lowBarPr, Low[CurrentBar-lowBarPr], CurrentBar-highBar, High[CurrentBar-highBar], Color.Green,DashStyle.Solid,3);
      int tempStartBar =CurrentBar-lowBarPr;	int tempEndBar = CurrentBar-highBar; upSwingDurTT = 0;
      for (int x = tempStartBar-1; x >= tempEndBar; x--)
      	if(sessionCountSeries[x] == sessionCountSeries[x+1])	{
      		upSwingDurTT = upSwingDurTT + ( (int)(Time[x].Subtract(Time[x+1]).TotalSeconds)   );
      }	//end For Loop
      //upSwingDurTT = (int)Math.Round((upSwingDurTT/60.0),0);
      upSwingDurTT = (int)(upSwingDurTT/60);
      Attached Files
      Last edited by forrestang; 03-26-2014, 10:48 AM.


      Latest Posts


      Topics Statistics Last Post
      Started by mostbp432, Today, 12:53 AM
      1 response
      Last Post NinjaTrader_PaulH  
      Started by itsthefriz, Yesterday, 09:16 PM
      1 response
      Last Post NinjaTrader_PaulH  
      Started by Stanfillirenfro, Today, 03:41 AM
      2 responses
      Last Post Stanfillirenfro  
      Started by trader3000a, Today, 04:16 AM
      1 response
      Last Post NinjaTrader_Marco  
      Started by schmidtbc16, Yesterday, 03:36 PM
      1 response
      Last Post NinjaTrader_ChelseaB