• 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.


No announcement yet.

Partner 728x90


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

  • 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):

    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.

    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.

    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


      Edit: my mistake was to declare the base class abstract


      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.

          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();
              [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.


        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.


          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.


            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.


              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.


              Latest Posts


              Topics Statistics Last Post
              Started by Tomass, Yesterday, 11:52 PM
              0 responses
              Last Post Tomass
              by Tomass
              Started by J_o_s, Yesterday, 11:31 PM
              0 responses
              Last Post J_o_s
              by J_o_s
              Started by kirkvan, Yesterday, 09:03 PM
              0 responses
              Last Post kirkvan
              by kirkvan
              Started by hedgeplay, Yesterday, 08:35 PM
              0 responses
              Last Post hedgeplay  
              Started by jaboo, Yesterday, 06:38 PM
              0 responses
              Last Post jaboo
              by jaboo