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

Way to capture Trade Time between two points (weekends)

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

    Way to capture Trade Time between two points (weekends)

    Howdy.

    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:
    Code:
    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?

    Thanks,
    Forrest

    #2
    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.
    http://www.ninjatrader.com/support/h...xtbeginend.htm

    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.
    http://www.ninjatrader.com/support/h...nt7/getbar.htm

    This would need some interesting logic. If you want the script created for you I suggest you contact a professional NinjaScript Consultant.
    http://www.ninjatrader.com/Ecosystem/NonBroker.php#81
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      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.

      Code:
      //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.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Waxavi, Today, 02:10 AM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by Kaledus, Today, 01:29 PM
      5 responses
      13 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by Waxavi, Today, 02:00 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by alifarahani, Today, 09:40 AM
      5 responses
      23 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by gentlebenthebear, Today, 01:30 AM
      3 responses
      17 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X