• 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

WMA of variable

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

    WMA of variable

    I am compiling a indicator as follows:

    protected override void OnBarUpdate()
    {
    // Do not calculate if we do not have enough bars
    double Net=Close[0]-Close[1];
    double Absnet=Close[0];
    if (Net>=0)
    {Absnet=Net;
    }
    else if (Net<0)
    {Absnet=-1*Net;
    }
    double Ave1=WMA(Net,7)[0];
    double Aveabs1=WMA(Absnet,7)[0];
    double Ave2=WMA(Ave1,21)[0];
    double Aveabs2=WMA(Aveabs1,21)[0];
    double ERG0=Ave2/Aveabs2;
    double ERG=WMA(ERG0,7)[0];

    if(ERG[0]>=ERG[1])
    {BarColor=Color.Blue;
    }
    else if (ERG[0]<ERG[1])
    {BarColor=Color.Red;
    }

    But compiled failed due to WMA(Net,7)[0].
    My purpose is: if ERG[0]>ERG[1], Barcolor=blue, and ERG line's color is also blue, otherwise both bar and ERG line colors are red.

    Please help me to compile it.

    Thanks Charlie

    #2
    Originally posted by charlie721 View Post
    I am compiling a indicator as follows:

    protected override void OnBarUpdate()
    {
    // Do not calculate if we do not have enough bars
    double Net=Close[0]-Close[1];
    double Absnet=Close[0];
    if (Net>=0)
    {Absnet=Net;
    }
    else if (Net<0)
    {Absnet=-1*Net;
    }
    double Ave1=WMA(Net,7)[0];
    double Aveabs1=WMA(Absnet,7)[0];
    double Ave2=WMA(Ave1,21)[0];
    double Aveabs2=WMA(Aveabs1,21)[0];
    double ERG0=Ave2/Aveabs2;
    double ERG=WMA(ERG0,7)[0];

    if(ERG[0]>=ERG[1])
    {BarColor=Color.Blue;
    }
    else if (ERG[0]<ERG[1])
    {BarColor=Color.Red;
    }

    But compiled failed due to WMA(Net,7)[0].
    My purpose is: if ERG[0]>ERG[1], Barcolor=blue, and ERG line's color is also blue, otherwise both bar and ERG line colors are red.

    Please help me to compile it.

    Thanks Charlie
    You cannot average a double, which is only one value. You need to make Net into a DataSeries if you want to take an average of it. The NT Help clearly shows you how. Read, and code, the tutorials.

    ref: http://www.ninjatrader.com/support/h...ur_own_sma.htm

    Comment


      #3
      Hello charlie721,

      Thank you for posing to the forums.

      I have looked over the code you provided. It seems there are a few errors in the way you have defined the variables you are using.

      First off I see that you are checking a prior close or Close[1].

      when you are checking bars in the past you need to make sure they are they before checking them, so in this case on the first bar or bar 0, this checks for bar -1 which does not exist.

      To prevent this sort of index error from happening you need to include an if statement in the top of the OnBarUpdate to return if the current bar is less than x amount or:

      Code:
      if(CurrentBar < BarsRequired)
      			return;
      This just ensures there are enough bars for calculating.

      Next the way that you are using the outcome from calculations, currently you are using the double values like DataSeries which would not work.

      I have attached a script that demonstrates how to use DataSeries to hold the data while you process it through calculations.

      Please let me know if I may be of additional assistance.
      Attached Files
      JesseNinjaTrader Customer Service

      Comment


        #4
        Hi, Jesse:

        Thanks for your help and your script can be properly complied and the bar color can change accordingly.

        But I cannot plot the line based on ERG value using the following command:
        if(ERG[0]>= ERG[1])
        {
        BarColor=Color.Blue;
        }
        else if (ERG[0] < ERG[1])
        {
        BarColor=Color.Red;
        }
        Plot0.Set(ERG[0]);

        }
        }
        }

        What I want to do is just plot out ERG line in blue color if ERG[0]>=ERG[1] otherwise in red color.

        Please help me to script it.

        Thanks!

        Charlie

        Comment


          #5
          Hello,

          Thank you for the question.

          For adding a plot, if you are looking at the example I posted you would need to define a plot first in order to see it.

          In the Initialize method you will need to add the following line:
          Code:
          Add(new Plot(Color.Black, "ERGPlot"));
          This adds a new plot to the indicator. Additionally if you want this to plot in a new panel because the values are right around 0 you would need to change this as well in Initialize

          Code:
          Overlay				= false;
          The overlay setting defines if the indicator lines are plotted over the bars of the chart or not.

          Next you need to create a public definition for the Plot in order to call it by name. For this you would need to add the following to the Properties area of the script or just inside the indicators class.

          Code:
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries ERGPlot
          {
               get { return Values[0]; }
          }
          This creates a public DataSeries that can be accessed from inside or outside of the indicator.

          Now you need to supply the Plot with the calculated data and color the line as it is plotted. To do this you will need to add a few lines to the coloring part of the script that is already there and provide the Plot with data by adding the ERGPlot.Set(ERG[0]); line.

          Code:
          if(ERG[0]>= ERG[1])
          {
               BarColor=Color.Blue;
               PlotColors[0][0] = Color.Blue;
          }
          else if (ERG[0] < ERG[1])
          {
               BarColor=Color.Red;
               PlotColors[0][0] = Color.Red;
          }
          
               ERGPlot.Set(ERG[0]);
          I have attached the compiled example and here are some references that will help you.

          Here is a reference for PlotColors http://www.ninjatrader.com/support/h...plotcolors.htm

          I would also recommend taking a look at the stock indicators and how they are built. you can do this by clicking Tools -> Edit NinjaScript -> Indicator -> Select a indicator and press ok.
          This will allow you to see the source behind any of the indicators in NinjaTrader.

          Please let me know if I may be of additional assistance.
          Attached Files
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ralett, Today, 07:29 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Marco  
          Started by bboyle1234, Today, 10:46 PM
          0 responses
          1 view
          0 likes
          Last Post bboyle1234  
          Started by swcooke, Today, 04:29 PM
          0 responses
          4 views
          0 likes
          Last Post swcooke
          by swcooke
           
          Started by bboyle1234, Today, 04:27 PM
          0 responses
          2 views
          0 likes
          Last Post bboyle1234  
          Started by swcooke, Today, 03:31 PM
          2 responses
          11 views
          0 likes
          Last Post swcooke
          by swcooke
           
          Working...
          X