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

Simple calculation

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

    Simple calculation

    Hello,

    I'm trying to do a simple calculation on OnBarUpdate() but I keep getting errors. I have researched but I can't make it work.

    I have three different indicator lengths and I want to calculate the average. Something like this:

    This is declared in the class above the OnStateChange()
    private SMA SMA1;
    private SMA SMA2;
    private SMA SMA3;

    Then OnBarUpdate()
    SMA1 = SMA(5);
    SMA2 = SMA(20);
    SMA3 = SMA(50);

    AvgSMA=(SMA1+SMA2+SMA3)/3;
    double highsignal = 90;


    if (CrossAbove(AvgSMA, highsignal, 1))
    EnterLong("AALong");


    But I keep getting errors about converting double to a series and vice versa. I have tried to declare all doubles but the CrossAbove doesn't like it neither...

    What's the best way to accomplish this?

    Thanks for the help.

    #2
    the message is misleading, you need to use [0] for the actual calculation..
    example: ZerolagTEMA[0] = 2*tema1[0] - tema2[0];
    AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3
    I include an indicator from fattail to show where you suppose to call the EMAs
    Attached Files
    Last edited by nkhoi; 12-21-2017, 11:39 PM.

    Comment


      #3
      Originally posted by nkhoi View Post
      the message is misleading, you need to use [0] for the actual calculation..
      example: ZerolagTEMA[0] = 2*tema1[0] - tema2[0];
      AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3
      I include an indicator from fattail to show where you suppose to call the EMAs
      Thanks nkhoi. Unfortunately it still doesn't work...

      SMA1 = SMA(10);
      SMA2 = SMA(20);
      SMA3 = SMA3(30);

      AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3

      double highsignal = 90;
      double lowsignal = 10;

      if (CrossAbove(AvgSMA[0], highsignal, 1))
      EnterLong();

      if (CrossBelow(AvgSMA[0], lowsignal, 1))
      EnterShort();


      Throws "The best overloaded method match for 'NinjaTrader.Indicator.Indicator.SMA(NinjaTrader.N injaScript.ISeries<double>, int)' has some invalid arguments"

      plus a second error

      "Cannot implicitly convert type 'NinjaTrader.NinjaScript.Indicators.SMA' to 'double'"....

      I have declared the AvgSMA in the properties

      public Series<double> AvgSMA
      {
      get { return Values[0]; }
      }

      and the three SMA series on top right below the class definition...

      private SMA SMA1;
      private SMA SMA2;
      private SMA SMA3;

      Any other idea?
      Last edited by TexFly; 12-22-2017, 05:48 AM.

      Comment


        #4
        Hello TexFly,

        Thanks for your post.

        I will address your questions and code from the first post to avoid confusion.

        You have declared 3 private SMAs which will be data series.

        You would need to create them in the OnStateChange() method in State.DataLoaded like:

        else if (State== State.DataLoaded)
        {
        SMA1 = SMA(5);
        SMA2 = SMA(20);
        SMA3 = SMA(50);
        }
        Reference: Best Practices, state management practices: https://ninjatrader.com/support/help..._practices.htm See the specific section titled, "Setting up resources that rely on market data"

        In OnBarUpdate() you have:

        AvgSMA=(SMA1+SMA2+SMA3)/3; which creates a double called avgSMA

        The conditional statement: if (CrossAbove(AvgSMA, highsignal, 1)) will not work because AvgSMA and highsignal are both double values. AvgSMA would need to be a data series for this to work.

        With reference to the previous helpguide link, set up AvgSMA as a private data series just like the helpguide shows for the "MySeries" example.

        Then rework the OnBarUpdate() to show:

        AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3;

        Now the conditional statement will work.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Paul,

          Great explanation. It works now. Thanks!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ZenCortexCLICK, Today, 04:58 AM
          0 responses
          5 views
          0 likes
          Last Post ZenCortexCLICK  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          172 responses
          2,280 views
          0 likes
          Last Post sidlercom80  
          Started by Irukandji, Yesterday, 02:53 AM
          2 responses
          18 views
          0 likes
          Last Post Irukandji  
          Started by adeelshahzad, Today, 03:54 AM
          0 responses
          7 views
          0 likes
          Last Post adeelshahzad  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          13 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X