Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Remaining time for the current bar

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

    Remaining time for the current bar

    Hello,

    I'm using Calculate.OnEachTick. However, I only want to do my processing during the last 5 or 10 seconds of the current bar. My goal is place an order essentially as the bar closes but before the next bar begins. I'm using time based bars (5-minute).

    What is the easiest way to determine the remaining time in the current bar?

    I could use a BarTimer indicator in my code, but I'm not seeing any method or property that returns number of seconds remaining (or counting up). Further, I couldn't find any webpage or website with a BarTime API. And it isn't listed here: https://ninjatrader.com/support/help...indicators.htm

    Thanks!
    Matt


    #2
    Hi Matt, thanks for your post.

    You would need to look at the source code for BarTimer.cs and implement the same timer in your indicator to get the same functionality. There is also the PercentComplete method that could prove useful.

    Please let me know if you have any further questions on this.

    Comment


      #3
      Hi Chris,

      Thank you for the quick reply. Reviewing that code, I came up with the following method in my strategy.

      Code:
      [FONT=courier new]private int getSecondsRemaining() {
         // Code/logic taken from BarTimer.cs - Ninja Trader indicator code
         DateTime now = (Cbi.Connection.PlaybackConnection != null ? Cbi.Connection.PlaybackConnection.Now : Core.Globals.Now);
         if (now.Millisecond > 0) now = Core.Globals.MinDate.AddSeconds((long)Math.Floor(now.Subtract(Core.Globals.MinDate).TotalSeconds));
      
         TimeSpan barTimeLeft = Bars.GetTime(Bars.Count - 1).Subtract(now);
         return barTimeLeft.Seconds;
      }[/FONT]
      Hopefully that will work. I'll try to remember to come back here to confirm.

      Thanks!
      Matt

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      47 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      33 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      51 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X