Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SMA in indicator calculation not working

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

    SMA in indicator calculation not working

    I'm writting a correlation indicator which requires calculation of mean of n period bars.
    I tried SMA to create the average of n period bars as below:

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Spread"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "SMASpread"));
    Overlay = false;
    Add(FirstInstrument,PeriodType.Minute,1); // Closes[1] = BarsInProgressIndex()
    Add(SecondInstrument,PeriodType.Minute,1); // Closes[2] = BarsInProgressIndex()
    }

    double num =0;

    protected override void OnBarUpdate()
    {
    try
    {
    num += SMA(Closes[1],Period);
    }
    }

    Causes Error :

    Operator '+=' cannot be applied to operands of type 'double' and 'NinjaTrader.Indicator.SMA'
    Cannot implicitly convert type 'NinjaTrader.Indicator.SMA' to 'double'


    Why can't I use SMA to calculate the mean / average of serveral bars?



    Afshin

    #2
    Hello,

    SMA(Input,Period) returns a dataseries, you need to pick which element of it you are using with this operator.

    SMA(Input,Period)[0] for example.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      That was it - Thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      601 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      347 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
      559 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      558 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X