Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving average offset

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

    Moving average offset

    Hello!
    When it comes to upload an indicator on the chart, there is a possibility to choose on the properties the "Displacement". This allows to have indicator "in the future", that means when the bars are not yet printed on the chart.
    I am trying to implement the EMA with offset so that I would want to use positive as well as negative numbers for the offset. I am not seeying any function in Ninjatrader that could deal with this topic. Has someone ever experienced the code with offset in ninjatrader? Is there a way to solve this problem?
    I was thinking about something like this:
    HTML Code:
     myEMA = EMA(Close, 15)[0].Displacement(7)
    I want in my code to have the moving average ploted 7 bars in the furure from the CurrenBar,

    I would appreciate any help.

    #2
    Hello Stanfillirenfro,

    Thank you for your note.

    Displacement may be configured in State.SetDefaults as demonstrated on the help guide page here:
    https://ninjatrader.com/support/help...splacement.htm

    Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Displacement = 2; // Plots the indicator value from 2 bars ago on the current bar    
            AddPlot(Brushes.Orange, "SMA");
        }
    }
    This is a property that may be set to visually offset the indicator's values by a number of bars. If you want the EMA to plot 7 bars "in the future" then you would need to set the displacement to 7 in State.SetDefaults.

    Please let us know if we may be of further assistance.

    P.S. I apologize if you received multiple notifications about my replies! I was getting an error for each attempt to post and did not realize that the replies were loading successfully each time.
    Last edited by NinjaTrader_Emily; 10-17-2022, 07:39 AM.

    Comment


      #3
      Many thanks NinjaTrader_Emily for your reply. Nice!

      Many thanks again!

      Comment


        #4
        Hello NinjaTrader_Emily!
        Sorry to come back to you again.
        The code in post#2 works nice, but I am struggling to replicate it on more than two moving averages at the same time. Moreover, I wanted to save the value of the offset moving average in a variable and use it in other calculations without having to plot it.
        I have tried to have Displacement1, Displacement2 for two different moving avarages in State == State.SetDefaults, but it does not work.
        Is there a way to save these moving averages in variables for further use without having to plot them?
        How to have two displacements at the same time in State == State.SetDefaults for two moving avarages?

        Thanks in advance.

        Comment


          #5
          Hello Stanfillirenfro,

          Thank you for your reply.

          No worries about coming back! I am glad to help out. In your situation where you would like to save the value of the displaced moving average without having to plot it, I suggest saving the value from x barsAgo to a Series<double>. There is a reference sample that demonstrates how to use a Series or DataSeries object to store calculations:
          https://ninjatrader.com/support/help...taseries_o.htm

          For example, you could define a series (or multiple) at class level. Then, in State.DataLoaded, you can create each series. In OnBarUpdate() you use the [] method to save the values in the series.

          Code:
          public class MySampleIndicator: Indicator
          {
          private Series<double> displacedEMA1;
          private Series<double> displacedEMA2;
          
          protected override void OnStateChange()
          {
          else if (State == State.DataLoaded)
          {
          displacedEMA1 = new Series<double>(this);
          displacedEMA2 = new Series<double>(this);
          }
          }
          
          protected override void OnBarUpdate()
          {
          // displacedEMA1 for the current bar can be the equivalent of EMA(14) from 2 bars ago.  This is the same as a displacement of 2
          displacedEMA1[0] = EMA(14)[2];
          // displacedEMA2 for the current bar can be the equivalent of EMA(14) from 5 bars ago.  This is the same as a displacement of 5
          displacedEMA2[0] = EMA(14)[5];
          
          // you could not use OnBarUpdate logic referring to either of the displacedEMA series values for a barsAgo index and it will be the displaced value from that bar
          }

          For more information about Series<t>:
          https://ninjatrader.com/support/help...t8/seriest.htm

          Please let me know if I may be of further assistance.​
          Last edited by NinjaTrader_Emily; 10-17-2022, 02:57 PM.

          Comment


            #6
            Many thanks NinjaTrader_Emily for your help!
            I am aware on working with Series<T>. Many thanks for your advice on using it in the displacement. From your explanation, if I understand well,
            HTML Code:
            displacedEMA1 for the current bar can be the equivalent of EMA(14) from 2 bars ago. This is the same as a displacement of 2 displacedEMA1[0] = EMA(14)[2];​
            this code should be applied when the bars are printed already on the chart. Since Series<T> do not use negative values, It would not be possible to project it in the future. Correct me please if I am mistaking.
            I would want to have something such as
            HTML Code:
            displacedEMA1[0] = EMA(14)[-6];
            meaning that I want this value in the future.

            I would appreciate any advice!

            Comment


              #7
              Hello Stanfillirenfro,

              Thank you for your reply.

              With displacement, there are no future values being calculated. As described in the help guide, displacement is "An offset value that shifts the visually displayed value of an indicator."

              The example given for displacement that was already mentioned is as follows:
              Displacement = 2; // Plots the indicator value from 2 bars ago on the current bar

              This would be essentially the same as saying displacedEMA1[0] = EMA(14)[2];
              EMA(14)[2] is the same as saying the value of an EMA with a period of 14 from two bars ago.

              Displacement is only visual, and if you are wanting to calculate a displaced series without having to plot it, that is where the series comes in handy. It is simply shifting the value of an indicator over by a certain number of bars.

              Please let us know if we may be of further assistance.

              Comment


                #8
                Many thanks NinjaTrader_Emily for your help.

                It is solved.

                Many thanks again !

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                647 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X