Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time-frame lag in strategy

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

    Time-frame lag in strategy

    I'm trying to place entries with the trend. My definition of a trend in my code is when the long term rsi is pointing in a particular direction. basically when rsi on the longer timeframe is pointing in a particular direction.

    It doesn't appear to be able to detect when there is a change in direction. From the image included you can see that the send entry (short) should have been a long entry because at the time of entry the longer term rsi(150) was pointing up (thick grey line). instead I got a short. How can i make this strategy more responsive to current conditions without constant signal oscillations?

    important parts of the code shown below:

    Code:
          protected override void Initialize()
            {
                Add(PeriodType.Minute, chartOne); //BarsArray[1] - chart 1
                Add(PeriodType.Minute, chartTwo);            
                
                SetStopLoss("", CalculationMode.Ticks, 40, true);
                SetProfitTarget("", CalculationMode.Ticks, 80);
                
                CalculateOnBarClose = true;
            }
    
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                    return;
                
                // Condition set 1
                if (Position.MarketPosition == MarketPosition.Flat &&
                    CUMRSI(BarsArray[1],2,3)[0] < CUMRSI(BarsArray[1],2,3)[1] && 
                        CUMRSI(BarsArray[2],2,3)[0] < CUMRSI(BarsArray[2],2,3)[1] &&
                        CUMRSI(BarsArray[2],2,3)[0] > upper &&
                        CUMRSI(BarsArray[0],2,3)[0] > upper)
                {
                    EnterShort();
                }
    
                // Condition set 2
                if (Position.MarketPosition == MarketPosition.Flat &&
                    CUMRSI(BarsArray[1],2,3)[0] > CUMRSI(BarsArray[1],2,3)[1] && 
                    CUMRSI(BarsArray[2],2,3)[0] > CUMRSI(BarsArray[2],2,3)[1] &&
                    CUMRSI(BarsArray[2],2,3)[0] < lower &&
                    CUMRSI(BarsArray[0],2,3)[0] < lower)
                {
                    EnterLong();
                }
    
                // Condition set 3
                if (Position.MarketPosition == MarketPosition.Long
                    && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] > upper)
    
                {
                    ExitLong();
                    EnterShort();
                }
    
                // Condition set 4
                if (Position.MarketPosition == MarketPosition.Short
                    && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] < lower)
                {
                    ExitShort();
                    EnterLong();
                }
            }
    Attached Files
    Last edited by ShruggedAtlas; 05-07-2015, 01:38 PM.

    #2
    Hello,

    You may have better luck checking Rising() or Falling() on the RSI, if you only need a signal when it's moving up or down. More information on these methods can be found at the link below:

    http://www.ninjatrader.com/support/h...tml?rising.htm

    http://www.ninjatrader.com/support/h...ml?falling.htm

    Please let me know if you see the same issue after trying these. Also, it looks like your screenshot did not come through. If you still see the same issue, please attach a screenshot once more.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      yes I'm afraid I had troubles with screenshot. I've included it with my post. I can try working with rising and falling functions however i'm worried that i'll get oscillating signals when the longer term signal is in an ambiguous point. rising will trigger multiple times ...but then again I haven't tried it so I don't know for sure. I'll give it a whirl and post my results if I have trouble. Thx!

      UPDATE:

      I just tried rising and falling and I got the same results. I see on the chart the longer term RSI is clearly moving up (rising) yet the strategy entered a short position.
      Last edited by ShruggedAtlas; 05-07-2015, 01:54 PM.

      Comment


        #4
        Unfortunately, your screenshot did not come through again. Are you using the Attach feature in the forum? Can you also please show me a snippet of your code that shows your order-entry conditions, as well?
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Sorry about the pic - The basic strategy to trade in the direction of BarsArray[1] and wait for BA[2] to get to extreme along with BA[0].

          Code:
          protected override void OnBarUpdate()
                  {
                      if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                          return;
                      
                      // Condition set 1
                      if (Position.MarketPosition == MarketPosition.Flat &&
                          Falling(CUMRSI(BarsArray[1],2,3)) && 
                          CUMRSI(BarsArray[2],2,3)[0] > CUMRSI(BarsArray[2],2,3)[1] &&
                          CUMRSI(BarsArray[2],2,3)[0] > upper &&
                          CUMRSI(BarsArray[0],2,3)[0] > upper)
                      {
                          EnterShort();
                      }
          
                      // Condition set 2
                      if (Position.MarketPosition == MarketPosition.Flat &&
                          Rising(CUMRSI(BarsArray[1],2,3)) && 
                          CUMRSI(BarsArray[2],2,3)[0] < CUMRSI(BarsArray[2],2,3)[1] &&
                          CUMRSI(BarsArray[2],2,3)[0] < lower &&
                          CUMRSI(BarsArray[0],2,3)[0] < lower)
                      {
                          EnterLong();
                      }
          
                      // Condition set 3
                      if (Position.MarketPosition == MarketPosition.Long
                          && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] > upper)
          
                      {
                          ExitLong();
                          EnterShort();
                      }
          
                      // Condition set 4
                      if (Position.MarketPosition == MarketPosition.Short
                          && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] < lower)
                      {
                          ExitShort();
                          EnterLong();
                      }
                  }
          Attached Files
          Last edited by ShruggedAtlas; 05-07-2015, 02:12 PM.

          Comment


            #6
            Hello,

            Thank you, I see the screenshot. The only red flag that I can see is that this appears to be occurring immediately after a session break, if I'm seeing it correctly. We may need to set up a remote support call so that I can look around and see if something else is causing the issue. If that works for you, please send an email to platformsupport [at] ninjatrader [dot] com with a phone number and a time that will work for you. Please reference ticket # 1312546 in the email.

            My schedule is pretty packed today, unless we can chat around 3:30pm MST, but I will be free any time tomorrow between 7:45am and 3:30pm MST.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              I am convinced that the session breaks don't matter. Rising and Falling functions are not responsive enough within the context of the current BarsArray[1]. Rising and Falling appear to be considering close[0] and close[1] different from my expectation. Notice from the chart that signals are delayed by the length of BarsArray[1] which is 150 minutes. whenever there is a change in direction there is always this lag before signals are correct. How can I make Rising and Falling more responsive within the current BarsArray[1] which hasn't yet ended?

              I've adjusted my code to simplify.

              Code:
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                              return;
                          
                          // Condition set 1
                          if (Position.MarketPosition == MarketPosition.Flat &&
                              Falling(CUMRSI(BarsArray[1],2,3)) && 
                              CUMRSI(BarsArray[2],2,3)[0] > upper)// &&
                          {
                              EnterShort();
                          }
              
                          // Condition set 2
                          if (Position.MarketPosition == MarketPosition.Flat &&
                              Rising(CUMRSI(BarsArray[1],2,3)) && 
                              CUMRSI(BarsArray[2],2,3)[0] < lower)// &&
                          {
                              EnterLong();
                          }
              Attached Files

              Comment


                #8
                Hello,

                So essentially, orders are being triggered on the bar after the one you expect? Is this happening in real-time, or only in the historical orders? If it is occurring in the historical orders, you may need to add intra-bar granularity into your code, since all historical orders are processed as if CalculateOnBarClose = True.

                You can find more information about this concept in the reference sample below:

                http://www.ninjatrader.com/support/f...ead.php?t=6652
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  I don't know about real time but it is definately happening in historical. I've got the stategy running now...so I'll monitor how it behaves in real time. Thanks for the info...i'll check it out and see what I can come up with.

                  Comment


                    #10
                    I'm not sure I'm doing this right. I added 1 minute timeframe which is less than the primary bars as demonstrated by the example. I indicated within the EnterLong and EnterShort functions which timeframe to base the entry on and I still get the wrong signal. See code below and picture demonstrating the error.

                    Code:
                            protected override void OnBarUpdate()
                            {
                                if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
                                    return;
                                
                                // Condition set 1
                                if (Position.MarketPosition == MarketPosition.Flat &&
                                    Falling(CUMRSI(BarsArray[1],2,3)) && 
                                    CUMRSI(BarsArray[2],2,3)[0] > upper &&
                                    CUMRSI(BarsArray[0],2,3)[0] > upper)
                                {
                                    EnterShort(3,100, "Short");
                                }
                    
                                // Condition set 2
                                if (Position.MarketPosition == MarketPosition.Flat &&
                                    Rising(CUMRSI(BarsArray[1],2,3)) && 
                                    CUMRSI(BarsArray[2],2,3)[0] < lower &&
                                    CUMRSI(BarsArray[0],2,3)[0] < lower)
                                {
                                    EnterLong(3, 100, "Long");
                                }
                    
                                // Condition set 3
                                if (Position.MarketPosition == MarketPosition.Long
                                    && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] > upper)
                    
                                {
                                    ExitLong("Long");
                                    EnterShort("Short");
                                }
                    
                                // Condition set 4
                                if (Position.MarketPosition == MarketPosition.Short
                                    && CUMRSI(BarsArray[2],PeriodRsi, 3).Plot0[0] < lower)
                                {
                                    ExitShort("Short");
                                    EnterLong("Long");
                                }
                            }
                    Attached Files

                    Comment


                      #11
                      Hello,

                      That is interesting. Everything seems alright in your code to me. You may not be using CurrentBars the way you expect, depending on what you are trying to accomplish, but that should not be causing the issue. CurrentBars will return the total number of bars for all data series combined. If you would like to check the current bar of a specific data series, you can do this:

                      if (BarsInProgress == 0)
                      {
                      if (CurrentBar < 0) return;
                      }

                      if (BarsInProgress == 1)
                      {
                      if (CurrentBar < 0) return;
                      }

                      Regarding the main issue -- I will need to test this on my end to look further into what is occurring. Is the CUMRSI a free indicator that you can share with me to test? Or, will the same issue result if you use the pre-loaded RSI indicator, so that I can test with that one?
                      Dave I.NinjaTrader Product Management

                      Comment


                        #12
                        Here is my CUMRSI indicator. It's simply the sum of the last three RSI each with a period of only 2 and smoothing of 3.

                        double CUMRSI = sum(RSI(2,3), 3)[0]

                        In my strategy I am incorporating a 150 period CUMRSI to determine direction (I could possibly use something else like a MACD or a MA but then won't I have the same problem?) and using the interplay of the 30 and 5 period CUMRSI to determine precise entry and exit.


                        Code for CUMRSI indicator is as follows:

                        Code:
                        namespace NinjaTrader.Indicator
                        {
                            /// <summary>
                            /// Enter the description of your new custom indicator here
                            /// </summary>
                            [Description("Enter the description of your new custom indicator here")]
                            public class CUMRSI : Indicator
                            {
                                #region Variables
                                // Wizard generated variables
                                    private int period = 2; // Default setting for Period
                                    private int smooth = 3; // Default setting for Smooth
                                // User defined variables (add any user defined variables below)
                                #endregion
                        
                                /// <summary>
                                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                                /// </summary>
                                protected override void Initialize()
                                {
                                    Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "Plot0"));
                                    Overlay                = false;
                                }
                        
                                /// <summary>
                                /// Called on each bar update event (incoming tick)
                                /// </summary>
                                protected override void OnBarUpdate()
                                {
                                    // Use this method for calculating your indicator values. Assign a value to each
                                    // plot below by replacing 'Close[0]' with your own formula.
                                    double value = SUM(RSI(period, smooth), 3)[0];
                                    Plot0.Set(value / 3);
                                }
                        
                                #region Properties
                                [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                                [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                                public DataSeries Plot0
                                {
                                    get { return Values[0]; }
                                }
                        
                                [Description("")]
                                [GridCategory("Parameters")]
                                public int Period
                                {
                                    get { return period; }
                                    set { period = Math.Max(1, value); }
                                }
                        
                                [Description("")]
                                [GridCategory("Parameters")]
                                public int Smooth
                                {
                                    get { return smooth; }
                                    set { smooth = Math.Max(1, value); }
                                }
                                #endregion
                            }
                        }

                        Comment


                          #13
                          Thank you -- I will do a bit of testing and get back to you as soon as I have something to share.
                          Dave I.NinjaTrader Product Management

                          Comment


                            #14
                            Hello,

                            Thank you for your patience on this issue. I'm not able to get the code you provided to take any trades. It appears that the conditions are never evaluated as true in any setup that I use. Can you please send me a full export of both your strategy and the CUMRSI indicator (File > Utilities > Export NinjaScript), so that I can use the exact same code that you are using? Also, can you tell me the exact setup you are using on your chart when you run the strategy?

                            I look forward to your reply.
                            Dave I.NinjaTrader Product Management

                            Comment


                              #15
                              The delay is caused by the fact that the longerterm (CUMRSI 150) is moving throughout it's bar. At the beginning of the bar it is likely to be moving in the same direction as the previous bar but as price evolves and the lower time frame bars progress forward in time, the indicator actually is changing. This is why there is *up to* a full 150 minutes before the condition shows as correct. The strategy will enter trades "correctly" at the beginning of the 150 timeframe but they become incorrect later in the bar as it progresses.

                              Since i'm using an adapted RSI at 2 periods it can change more quickly than the standard 14 period RSI which is helpful in limiting the delay to less than 150 minutes however unless someone can come up with an alternate approach, this is something I don't think I can get around.

                              UPDATE: while investigating your earlier suggestion that the issue seems centered around the session break I experimented with applying the strategy and indicators to ETH rather than RTH and discovered that the indicator movement during after hours sessions does in fact impact this strategy. It will take more time to discover the degree to which this helps my situation. Thanks again for all your time on this!
                              Last edited by ShruggedAtlas; 05-15-2015, 11:00 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              596 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              343 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              556 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              554 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X