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

Announcement

Collapse
No announcement yet.

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 Johnny Santiago, Today, 12:30 PM
              7 responses
              35 views
              0 likes
              Last Post Johnny Santiago  
              Started by ttodua, Today, 12:26 PM
              3 responses
              8 views
              0 likes
              Last Post NinjaTrader_PatrickG  
              Started by dastaan, 06-26-2020, 04:33 AM
              6 responses
              113 views
              0 likes
              Last Post NinjaTrader_Jim  
              Started by RumHam, Today, 12:14 PM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Started by Lancer, Today, 12:12 PM
              4 responses
              19 views
              0 likes
              Last Post NinjaTrader_PatrickG  
              Working...
              X