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

Count sessions and return if more then 3 from current

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

    Count sessions and return if more then 3 from current

    Count sessions and return if more then 3 from current

    Hello everyone, I am using the code with the condition on which the ray is drawn. the task is to set the ray rendering (condition true) if the ray was drawn more than 3 sessions ago. how you can count sessions and return if the condition is met in a session that was greater than the current one by more than 3. thank you very much in advance.

    ps / in my code I use SessionIterator
    Last edited by memonolog; 01-31-2021, 05:23 PM.

    #2
    Hello memonolog,

    The easiest way would be to have an int counter set to 0 when the object is drawn, that is incremented when Bars.IsFirstBarOfSession is true. This would count number of sessions since the object was drawn.


    With a sessionIterator, you could use .AddDays() to a datetime and check each day to see if the next session start matches the current session start.


    I think it would likely be less difficult but still advanced to directly loop through the TradingHours.Sessions collection.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello memonolog,

      The easiest way would be to have an int counter set to 0 when the object is drawn, that is incremented when Bars.IsFirstBarOfSession is true. This would count number of sessions since the object was drawn.
      https://ninjatrader.com/support/help...rofsession.htm

      With a sessionIterator, you could use .AddDays() to a datetime and check each day to see if the next session start matches the current session start.
      https://ninjatrader.com/support/help...extsession.htm

      I think it would likely be less difficult but still advanced to directly loop through the TradingHours.Sessions collection.
      https://ninjatrader.com/support/help...s_sessions.htm
      at least one live code example is implemented somewhere in indicators? like the last one?

      for now i found such a solution, but this is not really what I need, I just need to limit the work if 3 sessions are more than the current one

      if (Time[0] < DateTime.Now.AddDays(-MaxDaysToLoad)) // if current bar being processed is older than 3 days ago return
      return;
      Last edited by memonolog; 02-01-2021, 12:54 AM.

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello memonolog,

        The easiest way would be to have an int counter set to 0 when the object is drawn, that is incremented when Bars.IsFirstBarOfSession is true. This would count number of sessions since the object was drawn.
        https://ninjatrader.com/support/help...rofsession.htm

        With a sessionIterator, you could use .AddDays() to a datetime and check each day to see if the next session start matches the current session start.
        https://ninjatrader.com/support/help...extsession.htm

        I think it would likely be less difficult but still advanced to directly loop through the TradingHours.Sessions collection.
        https://ninjatrader.com/support/help...s_sessions.htm
        what will be the correct syntax for the analogue of this code

        Code:
        if (Time[0] < DateTime.Now.AddDays(-MaxDaysToLoad)) // if current bar being processed is older than 3 days ago return
        return;
        But using With a sessionIterator??

        Code:
        sessionIterator.GetNextSession(Times[1][1], true);
        
        // store the desired session information
        DateTime tradingDay = sessionIterator.ActualTradingDayExchange;
        DateTime beginTime = sessionIterator.ActualSessionBegin;
        DateTime endTime = sessionIterator.ActualSessionEnd;
        
        if (Time[0] > endTime.AddDays(-1))
        is that right?
        Last edited by memonolog; 02-05-2021, 03:30 AM.

        Comment


          #5
          Hello memonolog,

          Thanks for your reply.

          Perhaps a simpler approach, as Chelsea suggested, may help here.

          Create an integer variable to count the sessions.

          On the first bar of the session, increment the counter.

          For example: private int sessionCounter = 0;

          if (Bars.IsFirstBarOfSession)
          {
          sessionCounter +=; // incremnt counter
          }

          if (sessionCounter == 3)
          {
          // do something
          }
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_PaulH View Post
            Hello memonolog,

            Thanks for your reply.

            Perhaps a simpler approach, as Chelsea suggested, may help here.

            Create an integer variable to count the sessions.

            On the first bar of the session, increment the counter.

            For example: private int sessionCounter = 0;

            if (Bars.IsFirstBarOfSession)
            {
            sessionCounter +=; // incremnt counter
            }

            if (sessionCounter == 3)
            {
            // do something
            }
            if (Bars.IsFirstBarOfSession)
            {
            sessionCounter +=; // incremnt counter
            }

            I get error here

            Comment


              #7
              Hello memonolog,

              Thanks for your reply.

              Sorry for my typing error, please try:

              if (Bars.IsFirstBarOfSession)
              {
              sessionCounter += 1; // increment counter
              }
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_PaulH View Post
                Hello memonolog,

                Thanks for your reply.

                Sorry for my typing error, please try:

                if (Bars.IsFirstBarOfSession)
                {
                sessionCounter += 1; // increment counter
                }
                I tried it, it doesn't work, the code is executed from all sessions loaded on the chart,

                but I need to have a return (the code did not work from previous sessions if these sessions are older than the current one by more than 3)

                I have code

                Code:
                 if (Time[0] < DateTime.Now.AddDays(-2)) // if current bar being processed is older than 3 days ago return
                return;
                this code works, but it has a problem with weekends, since it counts days and not sessions
                are there other solutions to solve the problem?
                Last edited by memonolog; 02-13-2021, 09:39 AM.

                Comment


                  #9
                  Hello memonolog,

                  Thanks for your reply.

                  If your code works but not on weekends then a simple solution is to check to see if the day is Monday and if to then subtract additional days.

                  if (Time[0].DayOfWeek == DayOfWeek.Monday)
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you memonolog for this code:

                    Code:
                    if (Time[0] < DateTime.Now.AddDays(-MaxDaysToLoad)) // if current bar being processed is older than 3 days ago return
                    return;
                    This make great improvements to load the indicator in data series with a large numbers of days, loading only last 7 days of the indicator needed to make analysis and give context!
                    I will not need to use 2 data series to improve the loading of the indicators anymore!
                    THANK YOU!!!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by truepenny, Today, 03:59 AM
                    1 response
                    10 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by truepenny, Today, 03:45 AM
                    2 responses
                    16 views
                    0 likes
                    Last Post truepenny  
                    Started by dcriador, Today, 01:43 AM
                    2 responses
                    15 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by kujista, Today, 12:39 AM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by tony_28217, Yesterday, 07:04 PM
                    1 response
                    15 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Working...
                    X