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 TheWhiteDragon, 01-21-2019, 12:44 PM
      5 responses
      551 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by rtwave, 04-12-2024, 09:30 AM
      5 responses
      37 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by funk10101, Today, 12:02 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by GLFX005, Today, 03:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by nandhumca, Yesterday, 03:41 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X