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

How to Access Sessions Ninjascript?

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

    How to Access Sessions Ninjascript?

    I'm trying to build an indicator that will use two different session (1. Regular & 2. Extended session). How do I access Session Ninjascript?

    #2
    Hello cryfgg,

    Thank you for your post.

    The primary input series for the indicator will use the Trading Hours (session) template for that data series. Otherwise, your indicator could call AddDataSeries() with syntax that allows you to specify tradingHoursName if you'd like to add additional data using different sessions. The following warning should be kept in mind:
    • "If your NinjaScript object is using AddDataSeries() allowing to specify a tradingHoursName, please keep in mind that: An indicator / strategy with multiple DataSeries of the same instrument will only process realtime OnBarUpdate() calls when a tick occurs in session of the trading hour template of all added series. Any ticks not processed will be queued and processed as a tick comes in for all subsequent DataSeries."
    For more information on working with multi-time frame & instruments scripts:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by cryfgg View Post
      I'm trying to build an indicator that will use two different session (1. Regular & 2. Extended session). How do I access Session Ninjascript?
      One way to deal with multiple sessions is to add an offset to the main ETH session start time like this:

      Code:
      // class wide variables
      private SessionIterator sessionIterator;
      private DateTime session_startTime, session_endTime;
      private DateTime session_endTime;
      
      protected override void OnStateChange()
      {
          if (State == State.SetDefaults)
          {
          }
          // initialize the sessionIterator variable
          else if (State == State.DataLoaded)
          {
              //stores the session object once bars are ready, but before OnBarUpdate is called
              sessionIterator = new SessionIterator(Bars);
          }
      }
      
      // In OnBarUpdate()
      
      // local variables in OBU
      bool _IsFirstBarOfSession;
      DateTime intraSession_startTime;
      double elapsedTime;
      
      _IsFirstBarOfSession = Bars.IsFirstBarOfSession;
      if (_IsFirstBarOfSession)
      {
          // use the current bar time to calculate the next session
          sessionIterator.GetNextSession(Time[0], true);
      
          // store the desired session information
          session_startTime    = sessionIterator.ActualSessionBegin;
          session_endTime    = sessionIterator.ActualSessionEnd;
          if (isDebugOn) Print( String.Format("session_startTime: {0}, session_endTime: {1}", session_startTime, session_endTime) );
      }
      
      elapsedTime = 930; // offset to add to the ETH session start time to identify the RTH session time
      intraSession_startTime = session_startTime.AddMinutes( elapsedTime );
      
      if ( Time[0] > intraSession_startTime && Time[0] <= session_endTime)
      {
          // handle RTH bars
      }​
      else
      {
          // handle ETH bars
      }
      Last edited by trendisyourfriend; 09-25-2023, 12:32 PM.

      Comment


        #4
        Originally posted by NinjaTrader_Emily View Post
        Hello cryfgg,

        Thank you for your post.

        The primary input series for the indicator will use the Trading Hours (session) template for that data series. Otherwise, your indicator could call AddDataSeries() with syntax that allows you to specify tradingHoursName if you'd like to add additional data using different sessions. The following warning should be kept in mind:
        • "If your NinjaScript object is using AddDataSeries() allowing to specify a tradingHoursName, please keep in mind that: An indicator / strategy with multiple DataSeries of the same instrument will only process realtime OnBarUpdate() calls when a tick occurs in session of the trading hour template of all added series. Any ticks not processed will be queued and processed as a tick comes in for all subsequent DataSeries."
        For more information on working with multi-time frame & instruments scripts:
        https://ninjatrader.com/support/help...nstruments.htm

        Please let us know if we may be of further assistance.
        I know how to add DataSeries. I'm trying to figure out how to add sessions. For example, I want a moving average indicator that calculates and draws the moving average based on the regular session (9:30AM to 4pm) as well as based on the extended session (6pm to 5pm). In other words, there will be two moving averages on my chart based on two different sessions.

        Comment


          #5
          Originally posted by cryfgg View Post

          I know how to add DataSeries. I'm trying to figure out how to add sessions. For example, I want a moving average indicator that calculates and draws the moving average based on the regular session (9:30AM to 4pm) as well as based on the extended session (6pm to 5pm). In other words, there will be two moving averages on my chart based on two different sessions.
          There are overloads available for AddDataSeries() that allow you to specify the tradingHoursName to select a Trading Hours template to be used for that series. You could then set your script to have two plots; one for the regular session series and another for the extended session series:


          Otherwise, you could add a moving average indicator to your chart twice, once based on one session and a second time for data from the other session. You could even follow the steps in this video from our YouTube channel about plotting indicators on a "hidden" data series:


          Please feel free to reach out with any additional questions or concerns.
          Emily C.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by AaronKoRn, Today, 09:49 PM
          0 responses
          6 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Today, 08:42 PM
          0 responses
          9 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Today, 07:51 PM
          0 responses
          10 views
          0 likes
          Last Post strategist007  
          Started by StockTrader88, 03-06-2021, 08:58 AM
          44 responses
          3,977 views
          3 likes
          Last Post jhudas88  
          Started by rbeckmann05, Today, 06:48 PM
          0 responses
          9 views
          0 likes
          Last Post rbeckmann05  
          Working...
          X