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

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 WHICKED, Today, 12:45 PM
            2 responses
            18 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by GussJ, 03-04-2020, 03:11 PM
            15 responses
            3,276 views
            0 likes
            Last Post xiinteractive  
            Started by Tim-c, Today, 02:10 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Taddypole, Today, 02:47 PM
            0 responses
            5 views
            0 likes
            Last Post Taddypole  
            Started by chbruno, 04-24-2024, 04:10 PM
            4 responses
            51 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Working...
            X