Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limiting indicator to certain hours of the day

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

    Limiting indicator to certain hours of the day

    I use an automated system to trade between 9.30 am and 4 pm. The indicator that I use (let's say a moving average (MA) of 5 min bars for simplicity) is calculated from the previous days close across to the next open.

    How do I start the clculation of the MA from the open without including the previous day's data? I.e. I want the MA to be calculated fresh at the start of my trading period (9.30 am or any time I specify) without being distorted by following on from the previous session (previous day).

    If needed, I can upload an image to illustrate what I mean...

    Thanks,
    Trevor

    #2
    Hello,

    You may want to do one of these:

    1) start your strategy some number of bars after the start of the session begins time. For that you will want to use Bars.BarsSinceSession:


    In other words you will need to hold off your strategy until you have your EMA Period number of bars completed in the session.

    2) start your strategy on a designated time. For this you will want to use ToTime(Time[0]). This sample will help:


    If these do not work for your situation let us know more about your strategy constraints and I will assist further.
    DenNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Ben. I attempted to get this code to work to limit trading from 9.30 am to 4 pm:

      ToTime(Time[0]) >= 93000 && ToTime(Time[0]) <= 160000


      I inserted this into the conditions for a strategy to go long (see below), but there are still long trades being entered outside of this time range. What I am doing wrong??

      // Condition set
      if (CrossAbove(SMA(MAfast), SMA(MAslow), 1)
      && Slope(Stochastics(
      7, 14, 3).D, 10, 0) > 0
      && Close[0] > SMA(MAslow)[0]
      && Slope(SMA(
      14), 10, 0) > 0
      && Stochastics(7, 14, 3).D[0] < 80

      && ToTime(Time[
      0]) >= 93000 && ToTime(Time[0]) <= 160000)
      {
      EnterLong(DefaultQuantity,
      "");
      }

      Comment


        #4
        Hi Trevor,

        Do you have any other order entries in your code? Are you backtesting or in realtime?

        Remember during backtesting the order you submit does not kick in until the next bar because the signal bar is already closed so is untradeable. This could be a reason as to why the trade is falling outside of the range.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          Do you have any other order entries in your code?
          Yes, but I have included the same time limit code for each entry.

          Originally posted by NinjaTrader_Josh View Post
          Are you backtesting or in realtime?
          Backtesting (won't use in RT until backtested)

          Originally posted by NinjaTrader_Josh View Post
          Remember during backtesting the order you submit does not kick in until the next bar because the signal bar is already closed so is untradeable. This could be a reason as to why the trade is falling outside of the range.
          I kind of understand this, but what would be a workaround? Is there no way to implement this for backtesting?? Perhaps instead I could limit the indicators (signal) to this time range? Where would the code go to do this -- somewhere where the indicators are drawn? (see below)

          {
          #region Variablesprivatedouble stop = 20; // Default setting for Stop
          privateint mAslow = 20; // Default setting for MAslow
          privateint mAfast = 12; // Default setting for MAfast
          #endregion
          ///<summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          ///</summary>
          protectedoverridevoid Initialize()



          {
          Add(SMA(MAfast));
          Add(SMA(MAslow));
          Add(Stochastics(
          7, 14, 3));
          Add(SMA(MAfast));
          Add(SMA(MAslow));
          Add(SMA(MAfast));
          Add(SMA(MAslow));
          Add(SMA(MAfast));
          Add(SMA(MAslow));
          SetStopLoss(
          "", CalculationMode.Ticks, Stop, false);
          CalculateOnBarClose =
          true;

          }

          Comment


            #6
            Hello,


            Just to check, you are only having order places AFTER the trading times, and not before, correct?

            If this is the case, this is a limitation to backtesting generally. There is not much you can do other than move up your trading end time by one bar so that the last possible signal can only occur one bar before the last bar, i.e. before the end time.

            You can also try changing the time frame for the available data on the charts.
            DenNinjaTrader Customer Service

            Comment


              #7
              OK, so after a lot of time trying different things, including pulling out my hair, I have a workable solution:

              1. Set the session to start 2 h prior to time at which trading will commence. In my case, 8h00.

              2. Include the following in the conditions for the strategy to execute a tade:

              && Bars.BarsSinceSession > 24

              I use 5 min ticks, so this means that trading will only commence 2 h after the session start; in my case, at 10h00.

              Thus, my indicators have had time (2 h) to "catch up" over the gap in data between days (between close of session and start of new session) before trades are placed. As long as the time delay (24 bars) is greater than the maximum lag in anindicator (e.g. a moving average), this will avoid inaccuracies due to plotting indicators contiguously between sessions.


              Comment


                #8
                Hello,

                Good job! It sounds like you have it all sorted out now.
                DenNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by quantismo, 04-17-2024, 05:13 PM
                5 responses
                32 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by proptrade13, Today, 11:06 AM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by love2code2trade, 04-17-2024, 01:45 PM
                4 responses
                34 views
                0 likes
                Last Post love2code2trade  
                Started by cls71, Today, 04:45 AM
                2 responses
                10 views
                0 likes
                Last Post eDanny
                by eDanny
                 
                Started by kulwinder73, Today, 10:31 AM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_Erick  
                Working...
                X