Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Preferred way to detect start of trading day open and close ( and for week too)?

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

    Preferred way to detect start of trading day open and close ( and for week too)?

    It was good if i have asked this question earlier, because it's long I am suffering to fix this simple question, while calculating current-day or week High/Low/close...

    What is best reliable or universal way to detect the start of new market day (i.e. whether it's 0930 or whatever for current symbol) or week-start?

    Because on some symbols the data is given exactly from mar****pen to marketclose (09:30-16:xx) , on some symbols, 24/7 hour data is given, for some symbols different hours data is given....

    I always used such code in scripts (where client manually sets the hour itself and code roughly detects the opening bar):

    Code:
    [Display(GroupName="Parameters",   Name="Monitoring Period start")]        
    public int PeriodStart      // set to 0830 by default, however user adjusts that
    {
                get; set;
    }
    [Display(GroupName="Parameters",   Name="Monitoring Period end")]        
    public int PeriodEnd      // set to16830 by default, however user adjusts that
    {
                get; set;
    }
    
    ...
    
    OnBarUpdate:
    ...
    
    bool IsMar****penBar=  IsFirstTickOfBar &&
          (Time[0] >= PeriodStart &&  
               // If normal transition into open-time, like from 08:29 to 08:30
              (  Time[1] < PeriodStart  
                        ||
               // If jumped over that time from previous day, like from 21:00 to 09:00
              Time[0].DayOfYear !=  Time[1].DayOfYear )
           );
    
    bool IsMarketAfterCloseBar=
          ( Time[1] < PeriodEnd   &&  
               // If normal transition into open-time, like from 16:29 to 16:30
              ( Time[0] >= PeriodEnd
                        ||
               // If jumped over that time into next day, like from 15:30 to 09:30(next day)
              Time[0].DayOfYear != Time[1].DayOfYear )
           );

    for Weekly Pivots, I reckon week start like this, but this is incorrect, because depending on timezone, it start on Sunday or Monday:

    Code:
    bool IsWeeklyOpenBar =  IsMar****pen &&
       Time[0].DayOfWeek  ==DayOfWeek.Monday;
    
    bool IsWeeklyAfterCloseBar = IsMarketAfterCloseBar &&
       Time[0].DayOfWeek  ==DayOfWeek.Friday;

    However, these codes are incorrect to detect daily/weekly market-start and market-close times (I use them to catch opening and closing prices).
    Help would be appreciated.
    Last edited by ttodua; 02-13-2019, 02:09 AM.

    #2
    Hello TazoTodua,

    Thanks for your post.

    There are the following Bars properties that can be used to identify the first bar of a session and the last bar of a session.

    Bars.IsFirstBarOfSession - https://ninjatrader.com/support/help...rofsession.htm

    Bars.IsLastBarOfSession - https://ninjatrader.com/support/help...rofsession.htm

    Other approaches can be done to using a SessionIterator to interact with a Bars object's Trading Hours template.

    SessionIterator - https://ninjatrader.com/support/help...oniterator.htm

    We do not have any helper methods for detecting a new trading week, but you can use a SessionIterator to get the date of a trading day relative to the exchange's timezone, so we could do something like the following to detect a new trading week.

    Code:
    // on new bars session, find the next trading session
    if (Bars.IsFirstBarOfSession)
    {
        // use the current bar time to calculate the next session
        sessionIterator.GetNextSession(Time[0], true);
    
        if (sessionIterator.ActualTradingDayExchange.DayOfWeek == DayOfWeek.Monday)
            Print("New trading week.");
    
        Print(Time[0].DayOfWeek + " The current exchange trading day is " + sessionIterator.ActualTradingDayExchange.DayOfWeek);
    }
    SessionIterator.ActualTradingDayExchange - https://ninjatrader.com/support/help...ayexchange.htm

    Additional approaches could also be taken to see if the Trading Day cycles into a new week to signal a new week in your script. I.E. you don't see an iteration for monday, but you see an iteration for Tuesday and the last day was Friday.

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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    566 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    329 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
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X