Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Finding when market is closed

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

    Finding when market is closed

    I would like to know when the market is closed ( between 2 sessions ) from the code. ( from On Update method )

    I tried :
    if( Historical && CurrentBar == Bars.Count - 1)
    // Do stuff

    That doesn't work.


    Please help

    Thanks
    Last edited by blar58; 01-28-2016, 10:04 AM.

    #2
    Hello,
    I am currently looking into how this could be accomplished.
    Thank you fro your patience.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      OK I am waiting

      Comment


        #4
        Hello,
        Thank you for your patience.
        This would be accomplished by getting the DateTime of the previous session and the next session and then comparing these DateTime values to DateTime.Now using DateTime.Compare().
        I have provided an example below of how you could accomplish this:
        Code:
        #region Variables
        private int myInput0 = 1; // Default setting for MyInput0
        private DateTime previousSessionBegin;
        private DateTime previousSessionEnd;
        private DateTime nextSessionBegin;
        private DateTime sessionEnd;
        
        protected override void Initialize()
        {
               	CalculateOnBarClose = false;
        	Add(PeriodType.Day,1);
        }
        
        protected override void OnBarUpdate()
        {
        	if(CurrentBars[0] < 1 || CurrentBars[1] < 1)
        		return;
        	if(BarsInProgress == 0 && Bars.FirstBarOfSession)
        	{
        		Bars.Session.GetNextBeginEnd(BarsArray[1], 0, out previousSessionBegin, out previousSessionEnd);	
        	}
        				
        	if(DateTime.Compare(previousSessionEnd ,DateTime.Now) < 0 && DateTime.Compare(DateTime.Now, nextSessionBegin)< 0)
        	{
        		Print("Outside of session");
        	}
        }
        Before implementing this sort of logic I would recommend reading through the following documentation on some of the methods used to achieve this:
        Working with Multi Time Frame and Instruments: http://ninjatrader.com/support/helpG...nstruments.htm
        BarsArray: http://ninjatrader.com/support/helpG.../barsarray.htm
        BarsPeriod: http://ninjatrader.com/support/helpG...barsperiod.htm
        Bars.Session.GetNextBeginEnd: http://ninjatrader.com/support/helpG...xtbeginend.htm
        DateTime.Compare: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

        Please let me know if you have any questions on the example.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Do I need absolutely to add a day bar array ?

          Because my indicator is huge and I do not want to start playing with BarsInProgress throughout my code. The indicator works on minute chart. Can I find the next session from this minute bar array ?


          Thanks

          Comment


            #6
            You can just use the primary bars series. Please visit the following link for more information: http://ninjatrader.com/support/helpG...xtbeginend.htm

            Comment

            Latest Posts

            Collapse

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