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

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by alifarahani, Today, 09:40 AM
    3 responses
    15 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by RookieTrader, Today, 09:37 AM
    4 responses
    17 views
    0 likes
    Last Post RookieTrader  
    Started by PaulMohn, Today, 12:36 PM
    0 responses
    4 views
    0 likes
    Last Post PaulMohn  
    Started by love2code2trade, 04-17-2024, 01:45 PM
    4 responses
    40 views
    0 likes
    Last Post love2code2trade  
    Started by junkone, Today, 11:37 AM
    3 responses
    23 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X