Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Inheriting another indicator

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

    Inheriting another indicator

    I have a question concerning the new code structure with NT 8.

    In NinjaTrader 7 I had an indicator that was inheriting another indicator.

    Example :

    public class RelatedIndicator : MainIndicator {}

    Tell me if my thinking is right but I don't have to call OnStateChange()
    OnBarUpdate() and OnRender() from RelatedIndicator ?

    But I can call RelatedIndicator methods from MainIndicator OnStateChange()
    OnBarUpdate() and OnRender() methods.

    Just to make sure that my thinking is right.
    Thanks

    #2
    Hello,

    Yes, both of these things can still be done in NinjaTrader 8. I've just tested this out on my end to be sure, and the child does run the parent's OnBarUpdate(), etc., and the parent can access the child's public properties.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Dave - you should try to instantiate the derived indicator in a strategy. When you do you get a compiler error that goes something like, "'Indicator' is a type but is used like a variable."

      You can't even instantiate your new indicator unless you create your own constructor and use the 'new' keyword.

      Note: You do get to benefit from the virtual chain so all the virtual methods still behave the way you'd expect an inherited class to behave so at least there is still that. In other words, the derived indicator still works on a chart, but you cannot use it in a strategy - at least not until they fix whatever ails the compiler, or come up with a workaround. (I tried creating my own constructor in the derived indicator and that tried to work - it went right up the virtual chain for each of the classes - but I couldn't get my indicator to plot in the strategy.)

      Comment


        #4
        Originally posted by traderpards View Post
        instantiate the derived indicator in a strategy.
        I've worked up a solution to accomplish this, and I've attached three scripts to this post to show how it works (one parent indicator, one child indicator, and one strategy hosting the child indicator).

        There are a few keys to keep in mind with this particular method, and there may be other ways to accomplish this, as well. The first key, to address the "XYZ is a type, but is used like a variable" error, is to use a fully qualified name for your child class's inheritance:

        Code:
        public class testChild : NinjaTrader.NinjaScript.Indicators.testParent
        This will allow the NinjaScript Editor to add it's auto-generated code including the constructors, which will negate that compile error.

        The second key (this is the part that may be doable in a different way) is to create a public property in the parent indicator, exposing the current value of that indicator's plot.

        Lastly, I added a second plot within the child indicator, which I set based on the property within the parent indicator. With all this in place, you can instatiate the child indicator in a strategy, and you should see both plots on your chart.
        Attached Files
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Works like a champ...
          From what I had, all I had to do was say:
          Code:
          	public class testChild : NinjaTrader.NinjaScript.Indicators.testParent
          Once you do that, everything works like it dd for Version 7. I probably would never have thought of that. Now I'm sure that this will behave in every way one would expect virtual methods from a derived class to behave, now that you can actually create an instance of it.

          Thanks!

          PS. Can you please tell Jesse "Nevermind?" I think he's working on this also unless you already told him.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by traderqz, Today, 12:06 AM
          2 responses
          3 views
          0 likes
          Last Post traderqz  
          Started by RideMe, 04-07-2024, 04:54 PM
          5 responses
          28 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by f.saeidi, Today, 08:13 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by DavidHP, Today, 07:56 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by kujista, Today, 06:23 AM
          3 responses
          11 views
          0 likes
          Last Post kujista
          by kujista
           
          Working...
          X