Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the next coming bar time?

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

    How to get the next coming bar time?

    For example, in OnBarUpdate(), when CurrentBar==Bars.Count -1 (means the last bar), I need get the next coming bar time, how to realize it?

    I can't just add Bars period to current bar time then consider it as next bar time, that's not precise, there are some special cases to deal, for example on 1min chart, the last bar is the exchange session close bar, then the next coming bar will be in next session and it may be several hours or days after.

    Maybe it can be done by SessionIterator and/or TradingHours, but I don't know how...
    I tried to use SessionIterator to find out a way, but then I found some problems here so the way was blocked:
    http://ninjatrader.com/support/forum...ad.php?t=97046

    So, any good idea to get the next coming bar time??

    #2
    Originally posted by jjhou View Post
    ...
    I can't just add Bars period to current bar time then consider it as next bar time, that's not precise, there are some special cases to deal, for example on 1min chart, the last bar is the exchange session close bar, then the next coming bar will be in next session and it may be several hours or days after.
    ...

    So, any good idea to get the next coming bar time??
    That is exactly how you do it.

    You calculate that time, then compare it to the session end time. If your calculated end time is after the session end time, then you know that the next bar is going to be the first bar of the next session. NT allows you to query that.

    Are you sure that you used the SessionIterator correctly?

    ref: http://ninjatrader.com/support/forum...11&postcount=1
    Last edited by koganam; 03-03-2017, 01:08 PM.

    Comment


      #3
      Hello,

      Thank you for the post.

      This would be correct, generally, the SessionIterator can be used to perform various session related tasks such as looking up the next sessions information.

      If you were previously trying something that was not working, it would be helpful to see what you were trying to better understand what may have been happening. You can review the sample in the help guide for more details on how to utilize the SessionIterator. http://ninjatrader.com/support/helpG...essioniterator

      Please let me know if I may be of further assistance.

      Comment


        #4
        Thank you koganam,
        Your idea still has a little problem: I doubt sometimes you can't get the first bar's close time of next session accurately.

        Take forex future 4h chart for example, normally the session begins at 17:00. You can get the first bar's open time through GetTradingDayBeginLocal, normally it will be 17:00, so you add 4 hours to it and get the close time 21:00;
        But let's assume there's a special holiday break until 18:00, so that the next session delays to begin at 18:00, so GetTradingDayBeginLocal may return 18:00 (I haven't test that, just doubt ), then add 4 hours will be 22:00, it's wrong close time.

        Well, you may say the GetTradingDayBeginLocal will always return 17:00 no matter if there's session delay open, OK, let's assume another case: the next session delays to begin at 21:30 because the holiday break. Then the algorithm above will still get 21:00, but its wrong obviously.

        So, I think maybe TradingHours class should also be considered together to resolve it. what's your good suggestion?

        By the way, for the bug I mentioned at http://ninjatrader.com/support/forum...ad.php?t=97046, it's about IsInSession() method, it has serious problems and NinjaTrader_PatrickH has confirmed it, you should be very cautious if you used that method. You can view/test the indicator I wrote, it's very simple.
        Last edited by jjhou; 03-06-2017, 01:36 AM.

        Comment


          #5
          please see my reply to koganam and give me some suggestion

          Comment


            #6
            Hello jjhou,

            Thank you for your patience.

            You can actually use the SessionIterator to pull the next session's begin time and it will account for partial holidays.

            In the example below you must note that if you tried to pull this on the Bars.IsLastBarOfSession it would be too late as the bar would not be marked as the close of the session until the open of the next.

            I suggest the use of Bars.IsFirstBarOfSession and passing a DateTime.AddDays(1).

            For example:
            Code:
            		private SessionIterator sessionIterator;
            		protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				Description									= @"";
            				Name										= "FindNextBarTimeCheckForHoliday";
            				Calculate									= Calculate.OnBarClose;
            				IsOverlay									= true;
            				DisplayInDataBox							= true;
            				DrawOnPricePanel							= true;
            				DrawHorizontalGridLines						= true;
            				DrawVerticalGridLines						= true;
            				PaintPriceMarkers							= true;
            				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
            				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
            				//See Help Guide for additional information.
            				IsSuspendedWhileInactive					= true;
            			}
            			else if (State == State.Historical)
            			{
            				sessionIterator = new SessionIterator(Bars);
            			}
            		}
            
            		protected override void OnBarUpdate()
            		{
            			if (Bars.IsFirstBarOfSession)
            			{
            				Print(Time[0]);
            				sessionIterator.GetNextSession(Time[0].AddDays(1), false);
            
            				Print("The next session start time is " + sessionIterator.ActualSessionBegin);
            			}
            		}

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            581 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            338 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            552 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X