Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How can one reliably get "BarStartTime" for any time-based bar in NinjaScript?

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

    How can one reliably get "BarStartTime" for any time-based bar in NinjaScript?

    Hello NinjaTraders,

    Is there a truly reliable/robust way to determine the BarStartTime for any kind of time-based bar (intraday, daily, or weekly) in NinjaScript?

    Here's what I've learned the hard way so far:

    1. One can't just subtract the Bar Period (say 15 minutes for a 15-minute bar) from the BarEndTime of the bar, because for a 15-minute bar that opens 5 minutes to the session close, the BarEndTime of the bar in NinjaScript is actually the session close, which is only 5 minutes from the 15-minute bar's BarStartTime in that case.

    2. One can't just assume that the BarStartTime of the bar is the Bar End Time of the prior bar for a few reasons, one of which being for 1-minute bars in a thinly traded market, a minute may go by without a trade, so the end time of the last price bar would be 2 minutes from BarEndTime of the current 1-minute PriceBar.

    I can't find a property or a method exposed in the NinjaScript API to get the BarStartTime for a bar. What am I missing?

    Thank you very much in advance,

    EquityTrader

    #2
    Originally posted by EquityTrader View Post
    Hello NinjaTraders,

    Is there a truly reliable/robust way to determine the BarStartTime for any kind of time-based bar (intraday, daily, or weekly) in NinjaScript?

    Here's what I've learned the hard way so far:

    1. One can't just subtract the Bar Period (say 15 minutes for a 15-minute bar) from the BarEndTime of the bar, because for a 15-minute bar that opens 5 minutes to the session close, the BarEndTime of the bar in NinjaScript is actually the session close, which is only 5 minutes from the 15-minute bar's BarStartTime in that case.

    2. One can't just assume that the BarStartTime of the bar is the Bar End Time of the prior bar for a few reasons, one of which being for 1-minute bars in a thinly traded market, a minute may go by without a trade, so the end time of the last price bar would be 2 minutes from BarEndTime of the current 1-minute PriceBar.

    I can't find a property or a method exposed in the NinjaScript API to get the BarStartTime for a bar. What am I missing?

    Thank you very much in advance,

    EquityTrader
    You will have to create a custom timer event to trigger on FirstTickOfBar, and record the time for you to read.

    Comment


      #3
      koganam,

      Thank you very much for the quick response and a viable method to solve this problem in a live trading environment as bars are being built in real time.

      My sincere apologies, because I forgot to mention that I am trying to write code to accomplish this in a NinjaTrader back-test environment where I will be back-testing over bars and don't even have the individual trade ticks to determine if a tick is FirstTickOfBar.

      Now that we have a valuable solution for a live environment, I will rephrase my question for the NinjaTrader community to include the "back-testing on only bars without ticks" criterion:

      Is there a truly reliable/robust way to determine the BarStartTime for any kind of time-based bar (intraday, daily, or weekly) in NinjaScript from within a back-test that only has access to PriceBars and doesn't have access to individual trade ticks?

      Thank you very much in advance.

      EquityTrader

      Comment


        #4
        Originally posted by EquityTrader View Post
        koganam,

        Thank you very much for the quick response and a viable method to solve this problem in a live trading environment as bars are being built in real time.

        My sincere apologies, because I forgot to mention that I am trying to write code to accomplish this in a NinjaTrader back-test environment where I will be back-testing over bars and don't even have the individual trade ticks to determine if a tick is FirstTickOfBar.

        Now that we have a valuable solution for a live environment, I will rephrase my question for the NinjaTrader community to include the "back-testing on only bars without ticks" criterion:

        Is there a truly reliable/robust way to determine the BarStartTime for any kind of time-based bar (intraday, daily, or weekly) in NinjaScript from within a back-test that only has access to PriceBars and doesn't have access to individual trade ticks?

        Thank you very much in advance.

        EquityTrader
        There are two reasons why I am not sure that I quite understand the issue here, one mathematical, the other philosophical.

        First the mathematical. NT natively stamps bars with their end time, so the start time of the bar is identically the "Time[0] - barTimeInterval", period! Whatever happened before the bar is of absolutely no relevance. The bar exists or it does not.

        Then the philosophical. The choice to trade a thin market is entirely yours and I am not questioning your choice. However, regardless of the depth of market, we only make money if the price moves; IOW, if a tick comes in. We do not make money from the mere passage of time. (Please, do not think about Theta burn on Options: that is a different kettle of fish). So, if in a time interval, there is no trade, then it means that the price has not changed. Of what relevance is it as to when that unchanged price last changed? No money has been made anyway, and in any case you can directly query when that price was last registered by a bar update. Am I just confused?

        Comment


          #5
          koganam,

          Thank you for trying to help.

          Due to an interesting (and I think very good) behavior of NinjaTrader's intraday bar end time calculation logic, I have to respectfully disagree with your comment:

          "First the mathematical. NT natively stamps bars with their end time, so the start time of the bar is identically the "Time[0] - barTimeInterval", period!".
          In my original post I described the problem with the "BarStartTime = BarEndTime - BarPeriod" approach due to the way that NinjaTrader determines intraday bar end times, when I said:

          "1. One can't just subtract the Bar Period (say 15 minutes for a 15-minute bar) from the BarEndTime of the bar, because for a 15-minute bar that opens 5 minutes to the session close, the BarEndTime of the bar in NinjaScript is actually the session close, which is only 5 minutes from the 15-minute bar's BarStartTime in that case."
          Although I could calculate bar start times using the following 5 pieces of data...

          1. Session Start Time
          2. Session End Time
          3. Bar Period
          4. The knowledge that NinjaTrader (thankfully) starts bars at the Session Start Time instead of starting bars on the first minute that is <= Session Start Time but >= the hour of the Session Start Time such that 60 % (The Minutes portion of Session Start Time) == 0 (if 60 % BarPeriod == 0) or the first multiple of BarPeriod (in minutes) from the start of the hour that is <= Session Start Time (if 60 % BarPeriod != 0).
          5. The knowledge regarding the intraday bar end time for bars that start less than BarPeriod minutes from the end of the session.

          ... I ended my original post with
          "I can't find a property or a method exposed in the NinjaScript API to get the BarStartTime for a bar. What am I missing?"
          ... because I was hoping for a solution to my problem that not only saves me programming time, but would also be computationally more performant... If NinjaTrader already calculated BarStartTime and made it available in a property that doesn't show up in Intellisense, or perhaps provides a method that calculates this, presumably the NinjaTrader method of getting BarStartTime would be more performant than the method that I would write using the 5 pieces of information above.

          As for the philosophical points you raised in your last paragraph, suffice it to say that I have my own reasons for wanting an easy and performant way to access an intraday bar's start time and I would rather keep my reasons to myself.

          Thanks again for trying to help. If there isn't an easy way to do what I wanted to do, then don't worry about it. I will just write my own method to calculate bar start time.

          Best regards,

          EquityTrader

          Comment


            #6
            Originally posted by EquityTrader View Post
            koganam,

            Thank you for trying to help.

            Due to an interesting (and I think very good) behavior of NinjaTrader's intraday bar end time calculation logic, I have to respectfully disagree with your comment: ...

            In my original post I described the problem with the "BarStartTime = BarEndTime - BarPeriod" approach due to the way that NinjaTrader determines intraday bar end times, when I said:
            "1. One can't just subtract the Bar Period (say 15 minutes for a 15-minute bar) from the BarEndTime of the bar, because for a 15-minute bar that opens 5 minutes to the session close, the BarEndTime of the bar in NinjaScript is actually the session close, which is only 5 minutes from the 15-minute bar's BarStartTime in that case."
            Thank you for responding at all. You did make those statements in your original post, and I even noted and agreed with them (esp. as regards the "good behavior" of how NT handles the End-of-Session bar). I think too much hopium must have addled my brain, and sent me into idiot mode. Mea culpa.

            Comment


              #7
              Perhaps this doesn't fit your needs.... then again it might. What about using something like

              if(FirstTickOfBar)
              {
              //set a custom data series to time
              }

              Obviously, for live strategies this requires COBC = false and for historical you'd need tick data and a secondary data series. However, you could accurately create a series of bar open times.

              Comment


                #8
                Coolmoss and koganam,

                Thank you both for the solutions (including in koganam's first post) utilizing "FirstTickOfBar" to determine the first real trade tick of the bar. This is definitely going to be useful to me and others reading this forum thread.

                For my particular needs it appears I will need to write a fancy "GetBarStartTime()" method based on the five pieces of info I mentioned in my previous post, because what I am looking for is the determinant BarStartTime for a given BarEndTime, regardless of what time during the bar the first trade occurred. I definitely realize the problems of using this method to trade; for instance, does a bar really "start" at its theoretical start time or does it start when the first tick of the bar occurs, but for my specific needs, to integrate NinjaTrader BarEndTimes with the BarStartTimes that are used in other trading platforms and in the data files from some data vendors, I will go ahead and write the method to get the BarStartTime that might be described as "The earliest timestamp for a trade that would cause its inclusion into the bar with EndTime == NinjaTrader's BarEndTime".

                Thanks again to both of you for the valuable information, and happy trading.

                EquityTrader

                Comment

                Latest Posts

                Collapse

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