Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Displacement in a strategy

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

    Displacement in a strategy

    Hi, is there a way to get the "Bar Displacement" function in the APZ indicator on a chart to work on the APZ indicator in a strategy?
    Thanks

    #2
    Hi Stem1,

    The Bar Displacement is standard to all the indicators, and simply refers to how you call on the data using an index value.

    More info at - http://www.ninjatrader.com/support/f...ead.php?t=4769

    So, if you want to access one bar back use, for example...
    Code:
    double smaValue = SMA(14)[1];
    instead of...
    Code:
    double smaValue = SMA(14)[0];
    TimNinjaTrader Customer Service

    Comment


      #3
      Pardon my ignorance, but I’ve been trying to code a Moving Average crossover using identical moving averages, only one of the moving averages is displaced n candles to the left or right.

      In the Condition Builder, if you try to build “SMA(14)[0] ‘crosses above’ SMA (14)[1]” (using “Bars Ago”) and both are “Plot On Chart = True”, only one SMA(14) is plotted… ie, no second MA! There doesn’t appear to be a “Displacement” option like there is in the “Visual” options in the indicator setting.

      Can this displacement be coded in the Wizard or will it need to be added to the unlocked code?”

      Comment


        #4
        Hi ES,

        Check your Log tab in the control center for errors, likely, you are running into the behavior described here: http://www.ninjatrader.com/support/f...ead.php?t=3170

        The article also details how to address the issue.
        TimNinjaTrader Customer Service

        Comment


          #5
          I'm having a similar issue as the previous poster. (ES)

          I can't seem to get a SMA of the same value displaced on a strategy. Am I to use "offset" in the strategy builder?

          For example, if 5 sma 0 bars ago crosses above the 5 sma 0 bars ago offset 2, then enter long. Is this the correct logic? Or should I be looking at something else?

          Thanks in advance.

          Comment


            #6
            Hi MiniDowTrader,

            Displacement is a visual shift offered only in indicators.

            In a strategy, you can work with similar concept with the "barsAgo" field when defining your conditions. The offset is used for addition /subtraction to the indicator value, so would not be used here.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Sorry Ryan. I'm just not getting it.

              I understand that the indicator is simply being shifted, forward, in my case, on my charts. And that this doesn't work the same way in code language for a strategy.

              But how do I use "bars ago" in the strategy builder to simulate displacing a moving average forward?

              Thanks!

              Comment


                #8
                Just put in a value for bars ago. A bars ago value = 1 is the same as seeing it displaced at 1 bar.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Can't get it to work on the Analyzer. Shows zero trades. Just displays the single 5 SMA.

                  Comment


                    #10
                    I'm happy to take a look at your strategy if you can post the file or code.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      I have the bars ago on the 2nd SMA set to "2" in this strategy.



                      {
                      #region Variables
                      // Wizard generated variables
                      private int myInput0 = 1; // Default setting for MyInput0
                      // User defined variables (add any user defined variables below)
                      #endregion

                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
                      Add(SMA(Weighted, 5));
                      Add(SMA(Weighted, 5));
                      Add(SMA(Weighted, 5));
                      Add(SMA(Weighted, 5));

                      CalculateOnBarClose = true;
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      // Condition set 1
                      if (CrossAbove(SMA(Weighted, 5), SMA(Weighted, 5), 1))
                      {
                      EnterLong(DefaultQuantity, "");
                      }

                      // Condition set 2
                      if (CrossBelow(SMA(Weighted, 5), SMA(Weighted, 5), 1))
                      {
                      EnterShort(DefaultQuantity, "");
                      }
                      }

                      Comment


                        #12
                        Thanks for posting that. Unfortunately you could not create this in the wizard, as the crossing expressions expect a "series" for both inputs used. Series do not allow for bars ago values.

                        There is another crossing overload available when working with code. See below how this could be made. The indexed [2] in the second SMA input would allow it to use the bars ago value for SMA.

                        Code:
                        if (CrossAbove(SMA(Weighted, 5), SMA(Weighted, 5)[COLOR="Blue"][2][/COLOR], 1))
                        {
                        EnterLong(DefaultQuantity, "");
                        }
                        
                        // Condition set 2
                        if (CrossBelow(SMA(Weighted, 5), SMA(Weighted, 5)[COLOR="blue"][2][/COLOR], 1))
                        {
                        EnterShort(DefaultQuantity, "");
                        }
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Ryan.

                          I changed the code but I'm not getting the same results on the Analyzer that I see live on my charts.

                          Time to move on.

                          Thanks again.

                          Comment


                            #14
                            Originally posted by MiniDowTrader View Post
                            I have the bars ago on the 2nd SMA set to "2" in this strategy.



                            {
                            #region Variables
                            // Wizard generated variables
                            private int myInput0 = 1; // Default setting for MyInput0
                            // User defined variables (add any user defined variables below)
                            #endregion

                            /// <summary>
                            /// This method is used to configure the strategy and is called once before any strategy method is called.
                            /// </summary>
                            protected override void Initialize()
                            {
                            Add(SMA(Weighted, 5));
                            Add(SMA(Weighted, 5));
                            Add(SMA(Weighted, 5));
                            Add(SMA(Weighted, 5));

                            CalculateOnBarClose = true;
                            }

                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            protected override void OnBarUpdate()
                            {
                            // Condition set 1
                            if (CrossAbove(SMA(Weighted, 5), SMA(Weighted, 5), 1))
                            {
                            EnterLong(DefaultQuantity, "");
                            }

                            // Condition set 2
                            if (CrossBelow(SMA(Weighted, 5), SMA(Weighted, 5), 1))
                            {
                            EnterShort(DefaultQuantity, "");
                            }
                            }
                            Emphasis mine in red.

                            Regardless the validity of your code variables, your conditions can never be met. You are asking for when a Plot crosses itself one bar ago.

                            Comment


                              #15
                              Would it be possible to create a new indicator that would account for the displacement? And then have the "5 SMA" cross the newly created "5 SMA displaced"?

                              Just throwing out ideas. I appreciate the help.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X