• 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

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ct_fx, Today, 05:14 AM
            0 responses
            1 view
            0 likes
            Last Post ct_fx
            by ct_fx
             
            Started by Beauregard, Today, 04:36 AM
            0 responses
            2 views
            0 likes
            Last Post Beauregard  
            Started by fx.practic, Today, 04:34 AM
            0 responses
            1 view
            0 likes
            Last Post fx.practic  
            Started by thomaspartyof10, Today, 02:33 AM
            0 responses
            7 views
            0 likes
            Last Post thomaspartyof10  
            Started by ArmKnuckle, Today, 01:59 AM
            0 responses
            12 views
            0 likes
            Last Post ArmKnuckle  
            Working...
            X