Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to limit the data used in indicator by time

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

    How to limit the data used in indicator by time

    Hi,

    I'm trying to make some indicators that only calculate with data between 9:30 am - 4pm, so the indicator doesn't get affected by the data before 9:30.
    Any hint on how I should approach this?

    I noticed in strategies MACD is being initialized like this:

    Code:
    MACD1 = MACD(Close, Convert.ToInt32(OneFast), Convert.ToInt32(OneSlow), Convert.ToInt32(OneSmooth));
    Is there a way for me to limit the `Close` data series so it only contains data within a specific time frame? or should I limit when the indicator start calculating.

    Thanks in advance
    Last edited by madmen; 03-15-2020, 03:51 PM.

    #2
    in your onstatechange you need to have couple variable :

    StartSession = DateTime.Parse("09:00", System.Globalization.CultureInfo.InvariantCulture) ;
    EndSession = DateTime.Parse("16:00", System.Globalization.CultureInfo.InvariantCulture) ;


    in on bar update


    if (Times[0][0].TimeOfDay < EndSession.TimeOfDay && Times[0][0].TimeOfDay > StartSession.TimeOfDay)
    {

    return;
    }


    in your region add :

    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="Start Session", Order=6, GroupName="Parameters")]
    public DateTime StartSession
    { get; set; }

    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="End Session", Order=7, GroupName="Parameters")]
    public DateTime EndSession
    { get; set; }


    hope that helps

    Comment


      #3
      Hello madmen,

      This would be a time filter where a condition can require the time (or date) to be greater than one time and less than another.

      madams212121 has the right information for you for an example time filter.

      Below is a public link to the Strategy Builder 301 course which demonstrates.
      https://www.youtube.com/watch?v=HCyt...tu.be&t=43m30s

      As well as a link to the official reference sample.
      https://ninjatrader.com/support/help...to_limit_t.htm
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Very useful advice by Chelsea and madams.

        A different approach, and in case this hasn't been covered by Chelsea's references, is to add a second data series with a different trading hours definition, and initialize your MACD with it.
        https://ninjatrader.com/support/help...dataseries.htm
        Code:
        AddDataSeries(string instrumentName, BarsPeriod barsPeriod, string tradingHoursName);
        Code:
         MACD1 = MACD(Closes[1], Convert.ToInt32(OneFast), Convert.ToInt32(OneSlow), Convert.ToInt32(OneSmooth));
        The reason is that while a time filter will limit OnBarUpdate to calculate during the time of day you want, it will not stop new entries being added to any data series.

        Make sure to restrict OnBarUpdate to the data series you need for your calculations
        E.g. for original data series only
        Code:
        protected override void OnBarUpdate()
        {
            // Ignore bar update events for the supplementary - Bars object added above
            if (BarsInProgress == 1)
                return;
        Last edited by MojoJojo; 03-15-2020, 11:55 PM.

        Comment


          #5
          Thanks for the replies guys, I will try them now

          Comment


            #6
            madams212121's method worked, except he is ignoring the time interval between StartSession and EndSession. If you want it to calculate for the interval only just modify the code with

            Code:
                        if ( Times[0][0].TimeOfDay < StartSession.TimeOfDay || Times[0][0].TimeOfDay > EndSession.TimeOfDay)
                        {
            
                        return;
                        }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            637 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            366 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            107 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            569 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            571 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X