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

Bars GetBar and GetTime Not Symmetric?

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

    Bars GetBar and GetTime Not Symmetric?

    Hi:

    Is there a reason that GetBar and GetTime are not behaving symmetrically? This code:

    Code:
    		protected override void OnStartUp() {
    			DateTime nextBeginTime = Bars.Session.NextBeginTime;
    			int nextBeginTimeBar = Bars.GetBar(nextBeginTime);
    			DateTime nextBeginTimeBarTime = Bars.GetTime(nextBeginTimeBar);
    			Print("nextBeginTime = " + nextBeginTime);
    			Print("nextBeginTimeBar = " + nextBeginTimeBar);
    			Print("nextBeginTimeBarTime = " + nextBeginTimeBarTime);
    		}
    Prints this output:

    nextBeginTime = 9/4/2014 6:00:00 PM
    nextBeginTimeBar = 2173
    nextBeginTimeBarTime = 9/4/2014 6:05:00 PM

    #2
    Hi steevcoco,

    The session begins at 6:00. The first bar closes at 6:05 on a 5 minute chart.

    Are you using a 5 minute chart?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes: but I thought GetTimeEnd would return 6:05; while GetTime would return 6:00.

      Can you explain more about the specific time stamps? My use case is in counting back sessions to find the first bar of a session in the past ...

      More specifically: I can find the first bar of a session reliably (using IsFirstBarOfSession for instance). But then if I try to pass out the DateTime so I can match that to another Bar series, this discrepancy is breaking that -- the bar returned when I use that DateTime on another series is the bar after the first bar ...
      Last edited by steevcoco; 09-19-2014, 04:00 PM.

      Comment


        #4
        Hello,

        Are you referring to Bars.GetBar and not GetTime (I am unfamiliar with GetTime).

        The Bars.Session.NextBeginTime; returns the begin time of the session.

        Bars.GetBar will return the time of the bar. There will not be a bar that closes when the session opens. The first bar will close after the amount of ticks or minutes has past for the bar to close. So a 5 minute bar will return 6:05.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Bars has two methods: GetTime(index) and GetTimeEnd(index).

          What I do is find the first bar of a prior session (as I said, that is reliable). Then I want to find a bar in a different Bar series that is also the first bar of the same session. So I was expecting GetTime to return a time that could be used for that. I thought that GetTimeEnd would return the end of the bar (and therefore GetTime would return a time that would be included in that bar).

          Could I reliably change that to this strategy:

          Find the first bar of the prior session. Get the Time (or perhaps the EndTime) of the PRIOR bar. Then use that time to find the first bar of that session in another Bar series?

          This seems like it would work: the time returned is the close of the bar (correct?) -- which is technically exclusive, right? In other words, on a 5-minute chart, a tick at exactly 6:05 would land in the second bar, correct? If the session starts at 6:00, then ticks stamped from 6:00 through 6:04:59 would be in the first bar ...

          It seems that the way the methods are working, I could do it as I just said: the time returned for the previous bar will return the next bar from GetBar. I just wonder if that is the expected use; so it will be reliable ... ?

          Comment


            #6
            I think I should add that technically, the behavior does not seem symmetric: the bar index returned by the DateTime IS the first bar; but then getting a bar using that same time returns a DIFFERENT bar. So that is surprising.

            Comment


              #7
              Hello steevcoco,

              Thank you for your response.

              The first bar of the session's timestamp will be when that bar closes not when it opens. This is to be expected.

              Comment


                #8
                Thanks a lot Patrick. The help has been much appreciated. I don't mean to post too quickly, but I am learning things and moving forward with the input.

                For the benefit of the "manual" again, this turned out to be my strategy for finding sessions:

                Relating to the timestamp, and say in particular, the 24/7 template: it so happens that the session End time there would be midnight. But also, the Begin time is midnight. So to be sure I am getting the correct session when searching for a session:

                If there is a reference bar, start by getting the Time of the PRIOR bar -- that's the close of that bar -- and then add one tick (barTime.AddTicks(1L)). Now you have a time sure to be in your reference bar. In this way, you can then invoke Session.GetNextBeginEnd, and the session returned is sure to contain your reference.

                Then again, when you need to get a Bar ID for a session Begin, do the same tick-add on the Begin date to be sure you get the bar in the forward session: Bars.GetBar(begin.AddTicks(1L)). If you need prior sessions, you can use the same logic, and subtract a tick; to be sure you are across the session boundary and do not get tripped by using a time that is the same for the Begin and End of a session -- or in general, when you know you have a Session or Bar time boundary, to be sure you alias into the forward or prior session before you get your actual bar.

                Here is a snippet from code I use to find the first bar of the first session in a calendar month -- it is not complete, but illustrates the technique:


                Code:
                // ... Here, the reference point is barID, and it has been checked to be > 0:
                DateTime barTimeMidnight = bars.GetTime(barID - 1).AddTicks(1L);
                barTimeMidnight = new DateTime(
                		barTimeMidnight.Year, barTimeMidnight.Month, barTimeMidnight.Day).AddTicks(1L);
                barTimeMidnight = barTimeMidnight.AddDays(barTimeMidnight.Day - 1);
                DateTime begin;
                DateTime end;
                bars.Session.GetNextBeginEnd(barTimeMidnight, out begin, out end);
                return bars.GetBar(begin.AddTicks(1L));

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by alifarahani, Today, 09:40 AM
                6 responses
                30 views
                0 likes
                Last Post alifarahani  
                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 gentlebenthebear, Today, 01:30 AM
                3 responses
                17 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X