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

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)


    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.


    //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);
    [B]ds.Set(MySlope);[/B]
    return(Myslope);
    }
    Code:
    //and call it in the OnBarUpdate as
    double test=FctMyslope();
    [B]double MovSlope=EMA(ds,20)[0];[/B]
    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:
                            [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]
                            private[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] FctMyslope()[/COLOR]
                            {
                            [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] Myslope=Slope(EMA(Typical,Lenght),[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000],[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000])+Slope(EMA(Typical,Lenght),[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]5[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000],[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000])+Slope(EMA(Typical,Lenght),[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]8[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000],[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]) ;[/COLOR]
                            myDataSeries.Set(Myslope);
                            [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]return[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000](Myslope);[/COLOR]
                            }
                            [/FONT]
                            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:
                            [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]
                            protected[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]override[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] [/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] OnBarUpdate()[/COLOR]
                            {
                            [/FONT][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000]// Use this method for calculating your indicator values. Assign a value to each
                            // plot below by replacing 'Close[0]' with your own formula.
                            [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] MovSlope=EMA(myDataSeries,LenghtMov)[[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]];[/COLOR]
                            Print([/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]null[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]); 
                            Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"CurrentBar: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + CurrentBar);
                            Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"CP 1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
                            Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"FctMyslope(): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + FctMyslope());
                            Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"myDataSeries Value: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + myDataSeries[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]]);
                            Print([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"MovSlope: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + MovSlope);
                            Slope.Set(FctMyslope());
                            Slopemov.Set(MovSlope);
                            }
                            [/FONT]
                            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 CortexZenUSA, Today, 12:53 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,265 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X