Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 geddyisodin, Today, 05:20 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by Max238, Today, 01:28 AM
      3 responses
      32 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by timko, Today, 06:45 AM
      2 responses
      13 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by habeebft, Today, 07:27 AM
      0 responses
      5 views
      0 likes
      Last Post habeebft  
      Started by Tim-c, Today, 03:54 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X