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

Where should classes be instantiated in indicator code?

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

    Where should classes be instantiated in indicator code?

    Where should classes be instantiated in indicator code so that no more than one (1) instance is created? I guess the question also can be phrased; what part of indicator code is equivalent to a constructor? I noticed "if (State == State.Configured)" might trigger many instances. Can I make "my own" constructor without causing code conflicts? Any best practice?

    Code:
    //ALT 1:
    
    DateTime myTime;
    
    if (State == State.Configure)
    {
         myTime = new DateTime();
    }
    
    
    //ALT2:
    
    public MyIndicatorConstructor()
    {
        myTime = new DateTime();
    }

    Sorry if my search for the answere in the documentation might have been scarce.
    Last edited by FREEN; 08-13-2019, 10:05 AM.

    #2
    A follow up question;

    If I declare and instantiate in one row, where would be the right place to set "one time" properties of the class? Would this be best practice?


    Code:
    
     public class A190808Xaia : Indicator
     {
      #region Fields & Initialization
    
      private MyClass mc = new MyClass();
    
    
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
    
       }
       else if (State == State.Configure)
       {         
        mc.MyDouble = 2.0;
       }
      }
    
    
      more code....
     }

    Comment


      #3
      Hello FREEN,

      Thank you for your reply.

      I'd recommend setting those "one time" properties in either State == State.SetDefaults or State == State.Configure.

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

      Comment


        #4
        Thanks Kate! And declaration/instantiation should be done in one line in the #region Fields & Initialization? (Since instantiations elsewise might be triggered erratically and cause multiple instances?) Correct?

        Comment


          #5
          Hello FREEN,

          Thank you for your reply.

          You'd at least want to declare them there, and usually instantiate them as well.

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

          Comment


            #6
            Originally posted by FREEN View Post
            A follow up question;

            If I declare and instantiate in one row, where would be the right place to set "one time" properties of the class? Would this be best practice?


            Code:
            
            public class A190808Xaia : Indicator
            {
            #region Fields & Initialization
            
            private MyClass mc = new MyClass();
            
            
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            
            }
            else if (State == State.Configure)
            {
            mc.MyDouble = 2.0;
            }
            }
            
            
            more code....
            }
            If you are going to declare the class at class level, instantiate one time properties there.

            Code:
            private MyClass mc = new MyClass(){property1 = foo; property2 = bar; //etc};

            Comment


              #7
              I made this little dll in order to get clear on how NT instantiates:

              Code:
                  public class NTtestClass
                  {
                      public NTtestClass()
                      {
                          System.Windows.Forms.MessageBox.Show("This is constructor!");
                      }
                  }

              In the process of loading the indicator utilizing this class onto a chart, the MessageBox pops up (= the constructor is called) 4 times if instantiated in the class, 4 times in State.SetDefaults, but only once during State.Configure.

              After having unintendeded background processes started by multiple (4) firings of constructors it´s clear to me that instatiations should be made during
              State.Configure and no where else. Obviously same goes for event subscription.
              Last edited by FREEN; 09-04-2019, 03:51 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by StrongLikeBull, Yesterday, 04:05 PM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by Mestor, 03-10-2023, 01:50 AM
              14 responses
              375 views
              0 likes
              Last Post z.franck  
              Started by molecool, 10-09-2017, 10:48 AM
              5 responses
              1,621 views
              0 likes
              Last Post trader-ap  
              Started by The_Sec, Yesterday, 03:53 PM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by mmenigma, Yesterday, 03:25 PM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X