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 popecapllc, 08-09-2023, 07:42 PM
            8 responses
            1,339 views
            0 likes
            Last Post Johng22
            by Johng22
             
            Started by ETFVoyageur, 04-30-2024, 02:04 PM
            11 responses
            100 views
            0 likes
            Last Post ETFVoyageur  
            Started by bubblegum, 03-18-2024, 10:41 AM
            3 responses
            44 views
            0 likes
            Last Post vjsworld  
            Started by JamesK1, Today, 02:48 PM
            1 response
            12 views
            0 likes
            Last Post JamesK1
            by JamesK1
             
            Started by llanqui, Today, 03:51 PM
            0 responses
            10 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Working...
            X