Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with custom indicator

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

    Problem with custom indicator

    Good morning,
    scuse for my poor english, but I'm french.
    I cwant to create a moving average of a custom indicator and I don't arrived.
    In a first time I created a function for calculate my custom indicator:
    private double FctMyslope()
    {
    doubleMyslope=Slope(EMA(Typical,Lenght),1,0)+Slope (EMA(Typical,Lenght),5,0)+Slope(EMA(Typical,Lenght ),8,0);
    return(Myslope);
    }
    And after I would want to have an EMA of "Myslope". I wrote this but not work
    double test=FctMyslope();
    double MovSlope=EMA(test,20)[0];

    Could you help me please?

    Thank's a lot

    #2
    Hello neo-13,
    Welcome to the forum and I am happy to assist you.

    The best overload to calculate the EMA is the below overload
    EMA(IDataSeries input, int period)
    http://www.ninjatrader.com/support/h...onential_e.htm

    You have assigned a double instead of a data sereis to calculate the EMA and thus you are getting the scenario. Please try adding the slope values to a DataSeries and then assign the data series to calculate the EMA.
    http://www.ninjatrader.com/support/h...ries_class.htm

    //in variable
    Code:
    DataSeries ds;
    //in Initialize
    Code:
    ds = new DataSeries(this);

    Code:
    private double FctMyslope()
    {
    doubleMyslope=Slope(EMA(Typical,Lenght),1,0)+Slope (EMA(Typical,Lenght),5,0)+Slope(EMA(Typical,Lenght ),8,0);
    ds.Set(MySlope);
    return(Myslope);
    }
    Code:
    //and call it in the OnBarUpdate as
    double test=FctMyslope();
    double MovSlope=EMA(ds,20)[0];
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thank's a lot for your answer, but it don't work.
      My code with your explainations:
      public class MySlopeMM : Indicator
      {
      #region Variables
      // Wizard generated variables
      private int lenght = 20; // Default setting for Lenght
      private int lenghtMov = 8; // Default setting for LenghtMov
      private DataSeries myDataSeries;
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.MediumBlue), PlotStyle.Line, "Slope"));
      Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "SlopeMov"));
      Add(new Line(Color.FromKnownColor(KnownColor.InfoText), 0, "Mid"));
      myDataSeries = new DataSeries(this);
      Overlay = false;
      }
      private double FctMyslope()
      {
      double Myslope=Slope(EMA(Typical,Lenght),1,0)+Slope(EMA(T ypical,Lenght),5,0)+Slope(EMA(Typical,Lenght),8,0) ;
      myDataSeries.Set(Myslope);
      return(Myslope);
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.
      double MovSlope=EMA(myDataSeries,LenghtMov)[0];

      Slope.Set(FctMyslope());
      Slopemov.Set(MovSlope);
      }

      #region Properties
      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Slope
      {
      get { return Values[0]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Slopemov
      {
      get { return Values[1]; }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int Lenght
      {
      get { return lenght; }
      set { lenght = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int LenghtMov
      {
      get { return lenghtMov; }
      set { lenghtMov = Math.Max(1, value); }
      }
      #endregion
      }

      Comment


        #4
        Hello neo-13,
        If I try applying the indicator then I am getting the following error in the log files

        1/3/13 8:10:43 AM Default Error on setting indicator plot for indicator 'MySlopeMM'. Value outside of valid range.

        Please make sure the values (for calculating the EMA) is a valid double.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Good morning and thank's again for your answer,
          I have the same error log, but I don't understand the origin of the error.

          Thank's

          Comment


            #6
            Hello neo-13,
            The error is coming while calculating the EMA of the slope.

            The error is coming from line 59 in the attached code (the code is commented out).
            Attached Files
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Anyone could help me on plotting only the values on the right side of the chart but I don't want that there will be a plotted line or dash on the chart? For example I have an indicator that will plot the EMA value on the right side of the right side but I don't want that the plotstyle which is a line will also plot on the chart. Only the values will be plotted on the right side.

              Comment


                #8
                Hello edward_bell,
                Thanks for your note.
                Unfortunately there are no native way to do it. However if you set the color of the Plots same as the color of the background then you can make the lines disappear, while the values still appearing in the Y Axis pane.
                Attached Files
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Joydeep View Post
                  Hello edward_bell,
                  Thanks for your note.
                  Unfortunately there are no native way to do it. However if you set the color of the Plots same as the color of the background then you can make the lines disappear, while the values still appearing in the Y Axis pane.
                  Thanks NinjaTrader_Joydeep, BTW how to get the background color of the chart?

                  Comment


                    #10
                    Hello edward_bell,
                    Please follow the below step to change the background color of the chart.
                    • Right click on the chart
                    • In the context menu click on Properties
                    • In the Chart Properties dialog set the field Color for background to an appropriate color.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Joydeep View Post
                      Hello edward_bell,
                      Please follow the below step to change the background color of the chart.
                      • Right click on the chart
                      • In the context menu click on Properties
                      • In the Chart Properties dialog set the field Color for background to an appropriate color.
                      I mean how to get it through the code? I wanna set directly the plot to the backcolor of the chart.

                      Comment


                        #12
                        Hello edward_bell,
                        You can use the below unsupported code to do it.
                        Code:
                        ChartControl.BackColor;
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Good morning,
                          sorry but I don't understand:
                          You say me the problem come from this :Slopemov.Set(EMA(Slope1, LenghtMov)[0]);
                          But where is the problem?

                          Thank's.

                          Comment


                            #14
                            Originally posted by neo-13 View Post
                            Good morning,
                            sorry but I don't understand:
                            You say me the problem come from this :Slopemov.Set(EMA(Slope1, LenghtMov)[0]);
                            But where is the problem?

                            Thank's.
                            Your method here:
                            Code:
                            
                            privatedouble FctMyslope()
                            {
                            double Myslope=Slope(EMA(Typical,Lenght),1,0)+Slope(EMA(Typical,Lenght),5,0)+Slope(EMA(Typical,Lenght),8,0) ;
                            myDataSeries.Set(Myslope);
                            return(Myslope);
                            }
                            
                            is recursive. In the method, you are calling a Plot from the indicator itself, Slope, and you are calling it without an index. That will produce unpredictable results.

                            Add some tracking to your OBU event handler, by modifying it like this (The added Print() statements):
                            Code:
                            
                            protectedoverridevoid OnBarUpdate()
                            {
                            // Use this method for calculating your indicator values. Assign a value to each
                            // plot below by replacing 'Close[0]' with your own formula.
                            double MovSlope=EMA(myDataSeries,LenghtMov)[0];
                            Print(null); 
                            Print("CurrentBar: " + CurrentBar);
                            Print("CP 1");
                            Print("FctMyslope(): " + FctMyslope());
                            Print("myDataSeries Value: " + myDataSeries[0]);
                            Print("MovSlope: " + MovSlope);
                            Slope.Set(FctMyslope());
                            Slopemov.Set(MovSlope);
                            }
                            
                            Look in the Output window, and you will see that at some point, something, most likely, MovSlope becomes invalid as a number.

                            You need to handle your recursion correctly. Did you really intend to write a recursive method at all?

                            Comment


                              #15
                              Thank's for your answer.
                              About your question:
                              Did you really intend to write a recursive method at all?
                              No, I just want to calculate a sum of slope and his moving average.

                              Sorry but If I use your code:
                              double MovSlope=EMA(myDataSeries,LenghtMov)[0];
                              Print(null);
                              Print("CurrentBar: " + CurrentBar);
                              Print("CP 1");
                              Print("FctMyslope(): " + FctMyslope());
                              Print("myDataSeries Value: " + myDataSeries[0]);
                              Print("MovSlope: " + MovSlope);
                              Slope.Set(FctMyslope());
                              Slopemov.Set(MovSlope);

                              I have an error.

                              Thank's

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Oracletrades, Today, 09:10 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Oracletrades  
                              Started by AlyssaLindsey, Today, 09:02 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post AlyssaLindsey  
                              Started by SteveAU, Today, 08:35 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post SteveAU
                              by SteveAU
                               
                              Started by Lloyd143, Today, 05:24 PM
                              1 response
                              19 views
                              0 likes
                              Last Post NinjaTrader_ShawnB  
                              Started by Hagar, Today, 04:38 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ShawnB  
                              Working...
                              X