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

Indicator constructor overload

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

    Indicator constructor overload

    Hi Everyone,

    I have a custom indicator class with many fields that must be initialised when instantiating (and that first custom indicator is often instantiated from other indicators).

    Default field values are often good, only for, say, 1 out of 5 cases I would need to specify fields explicitly when instantiating. So I have several overloaded constructors, but Ninja's wrapper does not seem to recognize any of them them: it always generates #region Ninjascript for only two "default" constructors which require all parameters. IntelliSense does not show any of my overloads either. Of course, I would like my overloads to work. It does not make sense to have 20 parameters when an empty constructor would be just fine.

    I have seen a post with a similar Q recommending to use #region Variables and [Description("YourDescription")] [GridCategory("Parameters")], which I do use/have. I also tried removing all non-properties from the variables region and it did not help.

    So how can I get overloaded constructors to work? Are there some Ninja-specific requirements here?

    Thanks for any thoughts on this.

    #2
    Hello roman_ch, and thank you for your question.

    Please note, first, that any line starting with a # is a preprocessor command. The preprocessor runs before any of your actual code is read and compiled at all. Often preprocessor commands re-write your code, adding code and symbols on the line with the preprocessor command. Therefore, everything pertaining to a #region ... #endregion block has to be treated as though it does not exist to the rest of your code.

    In your case, since you would not like to use the NinjaScript Properties Region, I would highly recommend instantiating all your fields using Copy Constructors. You can then create instances of your innermost generic objects in your outer copy constructors, borrow the instance's fields, and add the differences in afterward. Finally, you can copy the code block where you borrow fields into every other child's code verbatim. I am including a link to the publicly available MSDN documentation on C# copy constructors.

    Learn how to write a copy constructor in C# that takes an instance of class and returns a new instance with the values of the input.


    If you would prefer duplicating code between children, I am also including a publicly available link to the MSDN documentation on globals scope in C#

    The C# namespace alias qualifier `::` is used to access a member of an aliased namespace. The `::` operator is often used with the `global` alias, an alias for the global namespace


    You may also be interested in the (My) Documents\NinjaTrader 7\bin\Custom\Indicator\UserDefinedMethods.cs and (My) Documents\NinjaTrader 7\bin\Custom\Strategy\UserDefinedMethods.cs files, which compile global symbols into every Indicator and Strategy in NinjaTrader 7.

    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,

      Many thanks for your prompt answer, but I am not sure I full understand your line of advice. Please see below my Qs/comments.

      (1)
      ... Therefore, everything pertaining to a #region ... #endregion block has to be treated as though it does not exist to the rest of your code.
      - I am sorry, but I believe it is common that the variables region is accessed directly. Even in NinjaTrader's factory indicators OnBarUpdate() works with variables without using properties. Please check eg RSSquared, for any of its sum variables. Perhaps, you meant something else, but I am not sure. Could you please clarify?
      (2)
      ... In your case, since you would not like to use the NinjaScript Properties Region
      I did not say that I am not willing to use the Properties region. Of course, I have the Properties region. Please can you explain how can I use it to instantiate one custom indicator with default values from another (ie, going back to my Q, w/out typing out all 20 parameters/properties each time)? As I mentioned, NinjaScript does not seem to allow me to use any overloaded constructors at all.
      (3)
      I would highly recommend instantiating all your fields using Copy Constructors
      . I will try this, thanks, could be a workaround if overloaded constructors are not available. But it is an unusual and confusing style. I would really like to know if / how I can use overloaded constructors, sort of "normal" C sharp style? To be clear, my current code for overloaded constructors would be perfectly fine in C sharp outside NinjaScript.

      Thanks again.

      Comment


        #4
        Hello roman_ch,

        I would be happy to clarify what I meant with my remarks.

        Originally posted by roman_ch View Post
        (1) - I am sorry, but I believe it is common that the variables region is accessed directly. Even in NinjaTrader's factory indicators OnBarUpdate() works with variables without using properties. Please check eg RSSquared, for any of its sum variables. Perhaps, you meant something else, but I am not sure. Could you please clarify?
        You are correct in that these variables can be used within your code. My phrasing was actually fairly awkward, and I do apologize for that. I just meant that these lines of code

        #region Properties
        #endregion

        (and just those two) are going to be reached before any C# code is reached, and will modify the code between them in unpredictable ways. As such, it is best to treat code in this region as if it were in a static context.

        I am hoping that phrasing is more clear, if it is, it will make my suggestion in the next section make more sense.

        (3) . I will try this, thanks, could be a workaround if overloaded constructors are not available. But it is an unusual and confusing style. I would really like to know if / how I can use overloaded constructors, sort of "normal" C sharp style? To be clear, my current code for overloaded constructors would be perfectly fine in C sharp outside NinjaScript.
        Thanks again.
        I am preparing a small code sample which I hope will serve your purposes.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello again,

          Please examine the provided code samples and let me know if they answer all your questions. If they do not, would it be possible after reviewing them and having a better idea of what I thought your intentions were, for you to clarify your meaning?
          Attached Files
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Jessica,

            Thanks for the files. I took some time to make sure I review your examples properly: but they only show child - parent inheritance relationship. There are no user defined overloaded constructors, which is what my Q is about (the only constructors you have are generated through Ninja wrapper). Please see below some code with user defined overloaded constructors
            class Test
            {
            public int x, y, z;
            // Default constructor:
            public Test()
            { x = 0; y = 0; z = 0; }
            //Overloaded constructor 1
            public Test (int par1)
            { x = par1; y = par1+1; z = par2+2;}
            //Overloaded constructor 2
            public Test (int par1, int par2)
            {x = par1; y = par2; z = par1+par2;}
            //Overloaded constructor 3
            public Test (int par1, int par2, int par3)
            {x = par1; y = par2; z = par3;}
            }

            Now, you could instantiate Test as follows:
            Test myTest1 = new Test (1);
            Test myTest2 = new Test (1, 2);

            I have a longer description in my first post, but by way of recap, the problem I am having that I do provide an overloaded constructor for an indicator class, but Ninja does not "see" it and does not generate a wrapper in the #region Ninjascript section. Consequently, I cannot call any overloaded constructors in my code.

            I have also mentioned that there was a similar discussion. Just in case, here is the link: http://ninjatrader.com/support/forum...ad.php?t=60182. To be clear, the solution mentioned there does not work for me.

            It would quite helpful to know how I can use overloaded constructors for indicators within Ninjascript.

            Perhaps, you could also discuss this with your colleagues and let me know? I would very much appreciate that.
            Last edited by roman_ch; 05-25-2016, 12:55 PM.

            Comment


              #7
              Hello roman_ch,

              I am including a link to another forums page which other users with this question have found helpful. Please let me know if this does not resolve your query, or if there are any other questions I may answer.


              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by roman_ch View Post
                Jessica,

                Thanks for the files. I took some time to make sure I review your examples properly: but they only show child - parent inheritance relationship. There are no user defined overloaded constructors, which is what my Q is about (the only constructors you have are generated through Ninja wrapper). Please see below some code with user defined overloaded constructors
                class Test
                {
                public int x, y, z;
                // Default constructor:
                public Test()
                { x = 0; y = 0; z = 0; }
                //Overloaded constructor 1
                public Test (int par1)
                { x = par1; y = par1+1; z = par2+2;}
                //Overloaded constructor 2
                public Test (int par1, int par2)
                {x = par1; y = par2; z = par1+par2;}
                //Overloaded constructor 3
                public Test (int par1, int par2, int par3)
                {x = par1; y = par2; z = par3;}
                }

                Now, you could instantiate Test as follows:
                Test myTest1 = new Test (1);
                Test myTest2 = new Test (1, 2);

                I have a longer description in my first post, but by way of recap, the problem I am having that I do provide an overloaded constructor for an indicator class, but Ninja does not "see" it and does not generate a wrapper in the #region Ninjascript section. Consequently, I cannot call any overloaded constructors in my code.

                I have also mentioned that there was a similar discussion. Just in case, here is the link: http://ninjatrader.com/support/forum...ad.php?t=60182. To be clear, the solution mentioned there does not work for me.

                It would quite helpful to know how I can use overloaded constructors for indicators within Ninjascript.

                Perhaps, you could also discuss this with your colleagues and let me know? I would very much appreciate that.
                The reason that you do not need to use "new" when you construct an object for which NT has generated the code is precisely because the NT wrappers are generating a copy of the object, using the properties exposed in the manner that NT wants, and making said object a member of the IndicatorBase.

                Read the code: you will see how the framework is doing it.

                NT does not generate and add an object to the IndicatorBase class, using any instance constructor in your code, so if you want to use that instance constructor, then you have to use the proper syntax for creating an object from scratch. The "new" keyword will be required when you create the object.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Kensonprib, 04-28-2021, 10:11 AM
                5 responses
                191 views
                0 likes
                Last Post Hasadafa  
                Started by GussJ, 03-04-2020, 03:11 PM
                11 responses
                3,230 views
                0 likes
                Last Post xiinteractive  
                Started by andrewtrades, Today, 04:57 PM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by chbruno, Today, 04:10 PM
                0 responses
                7 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Started by josh18955, 03-25-2023, 11:16 AM
                6 responses
                441 views
                0 likes
                Last Post Delerium  
                Working...
                X