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

Any recommendations or "best practices" when using inherited class for indicators?

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

    Any recommendations or "best practices" when using inherited class for indicators?

    In NT indicators, I generally make inheritance: I am building all my indicators based on my main (base) class (because I have plenty of common functionality there):

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public abstract partial class MyBaseIndi: Indicator {
        ...
    
    /// then :
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class SampleXyz: MyBaseIndi {
         ...

    I haven't met similar inheritance examples in NT scripts. Can you point me if any exist there and if there are some gotchas to know when building such types of indis.

    ####update###
    Please, don't just say "no". It is very critical for us (some of them are even vendors), and just knowing "no" is not enough in this case. please elaborate a bit more on this topic, so this topic can even be used as a reference for future readers.


    So, my question:

    Why NT should matter at all about whether indicator is built with inherited class or not, how it breaks NT functionality? It is not a joke, and why NT sees us as newbies and not professionals? We are building the quality indicators and we have "base" libraries and methods that we use in our (or clients') indicator packages (as we use "base" class for each company indicators, and in that base class we combine the common methods and properties and etc... and then we inherit that base class to build all indicators).
    This is the standard way in programming across the globe, to have libraries and make the codes as professional as possible.
    and you say, NT doesn't support to work with professional manners, but with "plain" (without inherited) classes. That is quite disturbing.
    Please, elaborate in a very detailed manner, where the problem lies and why we shouldnt use and what if we used even the warning? please, don't say just two words.
    Last edited by ttodua; 06-02-2020, 06:38 AM.

    #2
    Hello ttodua,

    Inheritance with Indicators and Strategies is not allowed and will break NinjaTrader in some places.

    There is no best practices, because this is not supported by NinjaTrader.

    Use at your own risk.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Edit: my mistake was to declare the base class abstract

      Hi,

      The problem with extending indicator is that even if the extending class is abstract, NT will auto generate it's code for the class as soon as it sees the keywords : Indicator.

      Fortunately this cannot be said for strategies, having zero issues so far. Also partial classes for indicators is fine.

      IOC and auto-generating code seem to be the only options.

      Got an exception when copy and pasting indicators, because I had a constructor with parameters even if they have default values.
      Could not save indicator 'PartialSma:' There was an error reflecting type 'NinjaTrader.NinjaScript.Indicators.PartialSma'.

      But after adding the default constructor all is green again.

      Code:
          public class PeriodParams {
              public PeriodParams(int period = 14) {
                  Period = period;
              }
              public PeriodParams() { // has to be added
                  Period = 14;
              }
              public int Period{get; set;}
          }
      
          public partial class PartialSma {
              public PeriodParams periodParams = new PeriodParams();
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name = "Period", GroupName = "SMA", Order = 0)]
              public int Period
              { get{return periodParams.Period;} set{periodParams.Period = value;} }
          }
      Last edited by MojoJojo; 06-02-2020, 12:27 PM.

      Comment


        #4
        ChelseaB, i have updated my question (just added the comment in the bottom of that post). please, re-read if you can.
        Last edited by ttodua; 06-02-2020, 10:45 AM.

        Comment


          #5
          Edit: my mistake was to declare the base class abstract
          Unfortunately I didn't find a way for inheritance with indicators, the NT auto code generation didn't make it possible.

          Code generation with partial classes is adequate but should be used sparingly. Other than that stick with composition.

          The only way I can think of is maybe compiling the code outside of NT.
          Last edited by MojoJojo; 06-02-2020, 12:26 PM.

          Comment


            #6
            Mojo, place the base .cs file in "AddOns" folder and there doesnt work auto-generation.
            Last edited by ttodua; 06-02-2020, 01:34 PM.

            Comment


              #7
              Interesting and thanks for the info, sounds very useful and will give it a try.

              I declared my base class abstract out of habit and got an error because of the NT auto-gen code.
              Cannot create an instance of the abstract class or interface 'NinjaTrader.NinjaScript.Indicators.AbstractIndica tor

              One of the gotchas obviously is don't use an abstract class. Sadly most likely not much more about the other gotchas will be forthcoming.

              Some of the things I wanted to implement would only be possible with multiple inheritance and that is a no go anyway.
              Fortunately code generation scratches my itch really well.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by samish18, Yesterday, 08:31 AM
              4 responses
              14 views
              0 likes
              Last Post elirion
              by elirion
               
              Started by funk10101, Yesterday, 09:43 PM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by TheWhiteDragon, 01-21-2019, 12:44 PM
              5 responses
              551 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by rtwave, 04-12-2024, 09:30 AM
              5 responses
              37 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by funk10101, Today, 12:02 AM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Working...
              X