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

NT8 Session filtering

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

    NT8 Session filtering

    Hello

    I have trading hours applied to 10 Range chart.
    My custom indicator process every trade by OnMarketData() event.
    The problem is that data still are coming after session ended with CurrentBar = last bar on chart. As you can see my ladder bar collected all the data after end of RTH session. But I need to stop at session end.

    How can I manage this? My obvious wish is not to receive data outside Trading hours.


    #2
    Hello ren37, and thank you for your query.

    In order to detect when the session is over, you will need code such as this at the beginning of OnBarUpdate. I am providing an excerpt from the help guide :

    Originally posted by http://ninjatrader.com/support/helpGuides/nt7/totime.htm
    Code:
    [FONT=Courier New]protected override void OnBarUpdate()[/FONT]
    [FONT=Courier New]{
    [/FONT]
    [FONT=Courier New]    // Only trade between 7:45 AM and 1:45 PM
        if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= [B]134500[/B])
        {
             return;
        }[/FONT]
    
    
    [FONT=Courier New]    // more code goes here
    [/FONT]
    [FONT=Courier New]}
    [/FONT]

    With the bolded integer, change 13 to the hour of the day you would like to change, the 45 to the minute within the hour, and 00 to the seconds within the minute. For example, 4 PM would look like this : 160000 .


    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Do you really think it's a proper solution for Trading hours template like this?

      Code:
      Session 0: Sunday at 2350 to Monday at 630
      Session 1: Monday at 710 to Monday at 2000
      Session 2: Monday at 2350 to Tuesday at 630
      Session 3: Tuesday at 710 to Tuesday at 2000
      Session 4: Tuesday at 2350 to Wednesday at 630
      Session 5: Wednesday at 710 to Wednesday at 2000
      Session 6: Wednesday at 2350 to Thursday at 630
      Session 7: Thursday at 710 to Thursday at 2000
      Session 8: Thursday at 2350 to Friday at 630
      Session 9: Friday at 710 to Friday at 2000
      Why not to filter data serie inside NT core by TH template settings like NT does it on charts? Maybe NT8 has some option for this?

      Comment


        #4
        Hello Ren,

        If you would like, you can access the session's beginning and ending times using Bars.Session.GetNextBeginEnd, documented in the help guide here, https://ninjatrader.com/support/help...xtbeginend.htm .

        I am incorporating this into my code sample.

        Code:
        [FONT=Courier New]private [/FONT][FONT=Courier New][FONT=Courier New]DateTime sessionStart;
        private DateTime sessionStop;
        
        [/FONT]protected override void OnBarUpdate()[/FONT]
        [FONT=Courier New]{
            // [/FONT][FONT=Courier New]On the start of a new session, get the next begin or end session time
            if (Bars.FirstBarOfSession)
            {[/FONT]
        [FONT=Courier New][FONT=Courier New]        Bars.Session.GetNextBeginEnd(BarsArray[0], 0, out sessionStart, out sessionStop);[/FONT]
            }
            if (sessionStart == null || sessionStop == null)
            {
                return;
            }
        
        [/FONT] [FONT=Courier New]    // Only trade between session start and session stop
            if (ToTime(Time[0]) >= ToTime(sessionStart) && ToTime(Time[0]) <= ToTime(sessionStop))
            {
                 return;
            }[/FONT]
        
        
        [FONT=Courier New]     // more code goes here
        [/FONT]
        [FONT=Courier New]}
        [/FONT]
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Your code is for NT7.
          In NT8 there is no method to get session instance or session begin/end time by bar index, only by session index.

          Comment


            #6
            Hello Ren,
            My apologies.

            This is the same code sample modified for NT8.



            Code:
            [FONT=Courier New]private SessionIterator[COLOR=#ffffff] [/COLOR]sessionIterator;
            private [/FONT][FONT=Courier New][FONT=Courier New]DateTime sessionStart;
            private DateTime sessionStop;
            
            protected override void OnStateChange()
            {
                // more code here
                if (State == State.Historical)
                {
                    // more code here
                    sessionIterator = new SessionIterator(Bars);
                }
                // more code here
            }
            
            [/FONT]protected override void OnBarUpdate()[/FONT]
            [FONT=Courier New]{
                // [/FONT][FONT=Courier New]On the start of a new session, get the next begin or end session time
                if (Bars.FirstBarOfSession)
                {[/FONT]
            [FONT=Courier New]        // use the current bar time to calculate the next session
                    sessionIterator.GetNextSession(Time[0], true);
            
                    sessionStart = [/FONT]
            [FONT=Courier New]sessionIterator.ActualSessionBegin;[/FONT][FONT=Courier New][FONT=Courier New]
                    sessionStop = [/FONT]
            [FONT=Courier New]sessionIterator.ActualSessionEnd;
            [/FONT]     }
                if (sessionStart == null || sessionStop == null)
                {
                    return;
                }
            
            [/FONT] [FONT=Courier New]    // Only trade between session start and session stop
                if (ToTime(Time[0]) >= ToTime(sessionStart) && ToTime(Time[0]) <= ToTime(sessionStop))
                {
                     return;
                }[/FONT]
            
            
            [FONT=Courier New]     // more code goes here
            [/FONT]
            [FONT=Courier New]}
            [/FONT]
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Ty for SessionIterator class.
              I think this code is much simpler

              Code:
                      public override void AddVolume(TradeSide side, MarketDataEventArgs e)
                      {
                          // Check session time
                          if (!indicator.SessionIterator.IsInSession(e.Time, true, true))
                              return;
              Last edited by ren37; 06-04-2016, 04:10 AM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by cre8able, Today, 03:20 PM
              1 response
              9 views
              0 likes
              Last Post cre8able  
              Started by fiddich, Today, 05:25 PM
              0 responses
              3 views
              0 likes
              Last Post fiddich
              by fiddich
               
              Started by gemify, 11-11-2022, 11:52 AM
              6 responses
              804 views
              2 likes
              Last Post ultls
              by ultls
               
              Started by ScottWalsh, Today, 04:52 PM
              0 responses
              4 views
              0 likes
              Last Post ScottWalsh  
              Started by ScottWalsh, Today, 04:29 PM
              0 responses
              9 views
              0 likes
              Last Post ScottWalsh  
              Working...
              X