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 limit calculations in current session?

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

    How to limit calculations in current session?

    Hey, guys.

    I'm trying to limit the OBU calculations to the current session.
    To limit to the present day I know how to do:
    Code:
    if(Time[0].Year == DateTime.Now.Year && Time[0].Month == DateTime.Now.Month && Time[0].Day == DateTime.Now.Day)
                {
                    canstart = true;
                }
    
                if(!canstart)
                    return;​
    Reading the user guide it seems to me that I have to use "SessionIterator".
    But how to do it?

    I've tried all sorts of things with this:​
    Code:
                if (Bars.IsFirstBarOfSession)
                {
                    sessionIterator.GetNextSession(Time[0], true);
    
                    year = sessionIterator.ActualSessionBegin.Year;
                    month = sessionIterator.ActualSessionBegin.Month;
                    day = sessionIterator.ActualSessionBegin.Day;
                    hour = sessionIterator.ActualSessionBegin.Hour;
                    minute = sessionIterator.ActualSessionBegin.Minute;
                    second  = sessionIterator.ActualSessionBegin.Second;
    
                    barsAgo = CurrentBar - Bars.GetBar(new DateTime(year, month, day, hour, minute, second));
    
                    //sessionIterator.IsInSession(Time[0], true, true);
                 }​
    But, nothing worked.​​​

    #2
    Hello rafaelcoisa,

    The SessionIterator and its method would only let you know if the bar is within a session but not that it is part of the most recent session. checking the date like you are would be a valid way to know if the bar time is part of the current days session. You are comparing against the PC clock which would be todays date. The SessionIterator is mainly used to get start/end times for sessions however that works in historical data as well so there is not a way to isolate a single session using that.

    JesseNinjaTrader Customer Service

    Comment


      #3
      That's what I'm trying to do, @NinjaTrader_Jesse.
      Isolate a single session, the current session, and calculate from its first bar.​

      Comment


        #4
        Hello rafaelcoisa,

        To know if the session being processed is the session that matches the current pc time would require making a date condition like you have posted in post 1. You could compare the PC clock which is DateTime.Now against Time[0] which is the bar time. That would let you know if the bar being processed has the same date from today. That will only work for instruments that have sessions spanning during 1 day, if the instrument trades overnight you wouldn't be able to use the PC clock to check if you are in the current session.

        When your script processes it starts with the oldest data in the range you selected and processes each bar until you reach Realtime. Reaching realtime may be in the middle of the days session so you would only be able to use Realtime to know you are within the active trading session but not when it started.

        You can know how many bars it has been since the new trading day started, that can be combined with a condition checking for realtime to find how many bars ago the session of today started: https://ninjatrader.com/support/help...tsub=barssince

        if(State == State.Realtime) {Print(Bars.BarsSinceNewTradingDay); }


        The easiest way to approach the problem would be to not try and isolate the current trading day but allow the script to process every session in the same way. By doing that you could use IsFirstBarOfSession to know which bar is the first bar of the trading session to start doing a calculation. By the time the script reaches the first bar of the session and then moves into realtime it will be the current trading session and the script will have calculated whatever you needed from the first bar of the current session at that point.



        JesseNinjaTrader Customer Service

        Comment


          #5
          Hey @NinjaTrader_Jesse,

          In fact, there's a way to do it with "sessionIterator".

          But, what I had in mind when I asked how to limit the calculations etc was that option that some indicators have, for example, the fixed volume profile that goes on the left or right of the chart.

          Calculate for:
          current session
          current day
          current week
          current year

          Do you have any samples, tips, documentation on how to do this?

          Last edited by rafaelcoisa; 12-16-2022, 07:44 PM.

          Comment


            #6
            Hello rafaelcoisa,

            If there is an indicator that has those options and you have it imported you could look at the code for that item in the NinjaScript editor. Those are otherwise not something specific that would be documented in the help guide.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by realblubb, Today, 09:28 AM
            0 responses
            1 view
            0 likes
            Last Post realblubb  
            Started by AaronKoRn, Yesterday, 09:49 PM
            1 response
            18 views
            0 likes
            Last Post Rikazkhan007  
            Started by ageeholdings, Today, 07:43 AM
            0 responses
            12 views
            0 likes
            Last Post ageeholdings  
            Started by pibrew, Today, 06:37 AM
            0 responses
            4 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            14 views
            0 likes
            Last Post bltdavid  
            Working...
            X