Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Syntax for two moving averages

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

    Syntax for two moving averages

    Hello,

    sorry, I have a new question.

    I want to build an indicator that has two moving averages; one move 10 bars ago on the current bar and the other not.

    Type:

    protected overridevoid Initialize()
    {
    Add(new Plot(Color.Red, "SMA"));
    Displacement = 0;
    Add(new Plot(Color.Green, "SMA"));
    Displacement = 10;
    }

    This syntax does not work. How do I do?

    Thank you for your answer.
    germano

    #2
    Hi germano, is this the only code you use for your Indicator? It misses the OnBarUpdate() where the values are calculated for the plots you created.

    You can just load two regular SMA indicators on the chart, both will offer the displacement parameter for setting it to your needs.

    Comment


      #3
      I tried even so, but does not work:

      publicclass SMA2 : Indicator
      {
      #region Variables
      privateint period = 28; // Default setting for SMA
      #endregion

      protectedoverridevoid Initialize()
      {
      Add(
      new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "SMA_1")); //Displacement = 0;
      Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "SMA_2")); //Displacement = 10;

      CalculateOnBarClose = true;
      Overlay =
      false;
      PriceTypeSupported =
      false;
      }

      protectedoverridevoid OnBarUpdate()
      {
      SMA_1.Set(SMA(period)[
      0]);
      SMA_2.Set(SMA(period)[
      10]);
      }

      Comment


        #4
        Did you notice errors in the log tab with this new code?

        Most likely related to this - http://www.ninjatrader-support2.com/...ead.php?t=3170

        Comment


          #5
          Thanks Bertrand.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          612 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          355 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          561 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          564 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X