Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Determine Market Open from Addon

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

    Determine Market Open from Addon

    Is there a way to determine if the Market is open from an Addon?

    I know there is SessionIterator but it requires a Bars object. I see it offers a TradingHours constructor as well.

    I tried passing in a TradingHours template as obtained from an instrument through Instrument.MasterInstrument.TradingHours. This seems to retrieve data for the the last 6 years going from 2015 through to 2021. What happens if I want/need more historical information going back further then 2015?


    #2
    Hi ntbone, thanks for your question.

    The trading hours for the BarsRequest can be set like this:
    barsRequest.TradingHours = NinjaTrader.Data.TradingHours.Get("CME US Index Futures RTH");

    The BarsRequest does return a Bars object to use, so one can use the Bars.IsFirstBarOfSessionByIndex() method. Here is what I did in the Addon Framework example to demonstrate:

    Code:
    private void PrintBarsRequest(NinjaTrader.Data.BarsRequest bars)
    {
    outputBox.Text = "REQUESTED BARS: " + bars.Bars.Count;
    
    SessionIterator sessionIterator = new SessionIterator(bars.Bars);
    
    // Process returned bars here. Note: The last returned bar may be a currently in-progress bar
    for (int i = 0; i < bars.Bars.Count; i++)
    {
    if(bars.Bars.IsFirstBarOfSessionByIndex(i))
    {
    sessionIterator.GetNextSession(bars.Bars.GetTime(i ), true);
    
    // store the desired session information
    DateTime tradingDay = sessionIterator.ActualTradingDayExchange;
    DateTime beginTime = sessionIterator.ActualSessionBegin;
    DateTime endTime = sessionIterator.ActualSessionEnd;
    
    outputBox.AppendText(string.Format("The Current Trading Day {0} starts at {1} and ends at {2}{3}",
    tradingDay.ToShortDateString(), beginTime, endTime, Environment.NewLine));
    }
    
    outputBox.AppendText(string.Format("{0}Time: {1}{0}Open: {2}{0}High: {3}{0}Low: {4}{0}Close: {5}{0}Volume: {6}{0}",
    Environment.NewLine,
    bars.Bars.GetTime(i),
    bars.Bars.GetOpen(i),
    bars.Bars.GetHigh(i),
    bars.Bars.GetLow(i),
    bars.Bars.GetClose(i),
    bars.Bars.GetVolume(i)
    ));
    }
    
    }

    Comment


      #3
      I am not needing to do BarsRequest though either. Having to do a BarsRequest just to get the trading ours just to get an SessionIterator seems like quite a bit of overhead.

      The following does the job and does not require me doing a BarsRequest to get it.

      Code:
      var sessionIterator = new SessionIterator(TradingHours.Get("US Equities RTH"));
      Looking at what is contained in the Trading Hours templates either through the NinjaTrader UI or through code shows that the trading hours data only extends as far back as 2015. What happens if I try to go back earlier? A brief test shows that it doesn't seem to correctly calculate trading hours for holidays prior to 2015.

      Comment


        #4
        Hi ntbone, thanks for your reply.

        That override is not documented so I can not say it should be used (if it's not documented it is possible it's not meant to be used at all). We have holiday info from the release of NinjaTrader 8, unfortunately, the prior holiday info is not kept on our servers and a custom trading hours template would need to be created that holds dates further than 2015.

        Kind regards,
        -ChrisL

        Comment


          #5
          Understood. I noticed the it was flagged to not even show up as browesable. Feature request to make this an official API as having to do a bars request just to get the trading session is undesirable.

          In my situation I want to know what time the US markets open next as the add-on has to do some things at market open. It doesn't need market data for anything it does.

          Comment


            #6
            Hi ntbone,

            I'll submit a feature request for this. I found this publicly available API that can be used to get trading hours data:


            You can also parse the template data with an XML reader, the templates are held in Documents\NinjaTrader 8\templates\TradingHours.

            Best regards,
            -ChrisL

            Comment


              #7
              Hi ntbone,

              The feature tracking ID is SFT-5283

              Please check the release notes upon future updates to check for added features with the ID:
              https://ninjatrader.com/support/help...ease_notes.htm

              Best regards,
              -ChrisL

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              646 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              367 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X