• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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, 10: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, 04: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 percma, Today, 03:44 AM
          0 responses
          1 view
          0 likes
          Last Post percma
          by percma
           
          Started by mrzz93, Today, 03:06 AM
          0 responses
          6 views
          0 likes
          Last Post mrzz93
          by mrzz93
           
          Started by HighTrade, Yesterday, 06:37 PM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChrisSch  
          Started by BarzTrading, Yesterday, 09:30 PM
          0 responses
          1 view
          0 likes
          Last Post BarzTrading  
          Started by waverider, Yesterday, 05:37 PM
          0 responses
          5 views
          0 likes
          Last Post waverider  
          Working...
          X