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

instantiate classes

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

    instantiate classes

    Hello, everyone,

    with regard to NT, some questions arise.

    Why is it possible in NT to call a class and use it like a method. Why doesn't it need to be instantiated. When does a class need to be instantiated? in C# always unless it is static.

    Example:

    protected override void OnBarUpdate()
    {
    if(MAX(High, 10)
    dosomething
    }


    Thank you for your answers.

    #2
    Originally posted by sane1111 View Post
    Why is it possible in NT to call a class and use it like a method.
    This is all supplied by the automatically generated 'magic code' seen
    at the bottom of each indicator.

    Studying the magic code in NT7 was much more straightforward, the
    newer NT8 magic code techniques have definitely hidden more details.

    Comment


      #3
      C# allows a method name to be same name as a class name.
      Yep, that's not a loophole, that's a design feature built directly
      into the C# language specification.

      That is,
      the basis of the 'magic' is the auto generated code that does this,

      Code:
      public EMA EMA(int period)
      {
          return EMA(Input, period);
      }
      
      public EMA EMA(ISeries<double> input, int period)
      {
          ....
      }
      To wit --

      You (or in this case, the NT developers) define a class
      named EMA, which uses 'Indicator' as it's base class.
      This is the 'user' code.

      The auto generated code defines methods named EMA,
      which are (by design) the same name as the class name
      you defined. The auto generated code is created by the
      NinjaScript compiler.

      The method named 'EMA' is returning an object reference
      to a class named 'EMA' -- don't get the class and the method
      confused -- being able to read the code well is extremely
      critical here, context is key.

      Note that two methods named 'EMA' are defined,
      this is an example of method overloading.

      The magic code is all very cool stuff, and all very
      undocumented.

      And, yes, the EMA method essentially performs a 'new'
      to instantiate an object of class EMA, but checks the cache
      of previous EMA objects and returns a matching reference
      if the arguments were identical.

      That is, the EMA method first checks the cache, and if a
      matching reference is not found, it calls new to create a
      new reference, adds that to the cache, and then returns
      that reference.

      The return reference must be properly 'marinated' by adding
      it to various internal lists -- for ex, the object's methods
      such as OnStateChange, OnBarUpdate, et al, must be
      called by the NinjaTrader internals at the appropriate
      times. Again, all very undocumented.

      The auto generated magic code definitely exploits the
      object oriented power of the C# language.
      Last edited by bltdavid; 06-13-2022, 02:03 PM.

      Comment


        #4
        Hello sane1111,

        Thank you for your post.

        bltdavid has pretty much nailed it here, so there's not a lot I'd add. Basically yes, it's the magic code at the bottom that gets generated by NinjaTrader when you compile that's making that magic happen, which is not documented.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by TheWhiteDragon, 01-21-2019, 12:44 PM
        4 responses
        542 views
        0 likes
        Last Post PaulMohn  
        Started by GLFX005, Today, 03:23 AM
        0 responses
        3 views
        0 likes
        Last Post GLFX005
        by GLFX005
         
        Started by XXtrader, Yesterday, 11:30 PM
        2 responses
        11 views
        0 likes
        Last Post XXtrader  
        Started by Waxavi, Today, 02:10 AM
        0 responses
        7 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Started by TradeForge, Today, 02:09 AM
        0 responses
        14 views
        0 likes
        Last Post TradeForge  
        Working...
        X