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

Initialization problem -- structs

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

    Initialization problem -- structs

    There must be something about initialization I do not understand.

    One of the variables in my variables region is a class. I use the class to store user parameters so that it is convenient to pass them to some functions as a group.
    Code:
    private AMA_Values        fastAMA;        // Initalize() needs to construct it
    I create the class in Initialize()
    Code:
    fastAMA = new AMA_Values();        // Self-initializing
    I refer to it from a property / parameter
    Code:
            public int FastKamaFast
            {
                get { return fastAMA._KamaFast; }
    //            set { fastAMA._KamaFast = Math.Max(1, value); }
                set {
                    Print("\tBefore setting KamaFast");
                    fastAMA._KamaFast = Math.Max(1, value);
                    Print("\tAfter setting KamaFast");
                    }
            }
    The line in between those two print statements throws an exception. (The print statements were put in after I got the exception to prove it was that line.)
    Failed to set property 'FastKamaFast' for indicator 'RwbTrend': Exception has been thrown by the target of an invocation.
    It obviously compiled successfully, or I would not be able to run it. I have put in some print statements after the object is created in Initialize() and I can read it just fine, so it really did get constructed.

    Any other ideas?

    --EV
    Last edited by ETFVoyageur; 01-19-2011, 10:42 PM.

    #2
    Well, I fixed the problem. I changed the declaration to be
    Code:
    private AMA_Values        fastAMA = new AMA_Values();        // Self-initializing
    So what is going on here? Before I made this fix, it acted as if it was referencing an uninitialized object and throwing an exception. Yet the Set() method did not get run until after Initialize() had run and successfully constructed the object.

    I don't know why initializing it directly in the declaration should have fixed the problem, but it did.

    Can someone please explain how an uninitialized object was getting accessed after I had constructed it in Initialize()?

    Thanks,
    EV
    Last edited by ETFVoyageur; 01-19-2011, 10:34 PM.

    Comment


      #3
      I am presuming that AMA_Values is a class?

      Comment


        #4
        Originally posted by koganam View Post
        I am presuming that AMA_Values is a class?
        Yes, it is. Contains nothing but public data members and a constructor that initializes them. It is just a convenient way to pass a bunch of parameters as a single argument.


        Code:
            // This is a struct to hold all of the one-off values that some of the adaptive moving averages need
            public class AMA_Values {
                public int            _KamaFast;
                public int            _KamaPeriod;
                public int            _KamaSlow;
            
                public int            _T3Count;
                public double        _T3vFactor;
            
                public int            _VmaPeriod;
                public int            _VmaVolatilityPeriod;
            
                public double        _ZLagEmaLength;
                public double        _ZLagEmaGainLimit;
            
                public double        _ZiCoefficient;
                public int            _ZiCycle;
                public int            _ZiPeriod;
                
                public AMA_Values()        // Class initializer, to supply default values
                {
                    _KamaFast = 2;
                    _KamaPeriod = 10;
                    _KamaSlow = 30;
                
                    _T3Count = 1;
                    _T3vFactor = 0.7;
                
                    _VmaPeriod = 9;
                    _VmaVolatilityPeriod = 9;
                
                    _ZLagEmaLength = 20;
                    _ZLagEmaGainLimit = 50;
                
                    _ZiCoefficient = 3;
                    _ZiCycle = 4;
                    _ZiPeriod = 21;
                }
            }

        Comment


          #5
          I think that in that case, in the Properties section, you have to specify it as a class, then once constructed, you can access its members in code. I seem to remember that you cannot return the members of the class directly in the Properties section. cf. How we setup enums in the properties section.
          Last edited by koganam; 01-20-2011, 12:33 AM.

          Comment


            #6
            Originally posted by koganam View Post
            I think that in that case, in the Properties section, you have to specify it as a class, then once constructed, you can access its members in code. I seem to remember that you cannot access the members of the class directly in the Properties section. cf. How we setup enums in the properties section.

            I'm not following you.

            Note that the code in the properties section can get to it just fine once it is constructed. It just acted as if it was accessing an unconstructed object when I constructed it in Initialize(). Properties access it fine if I construct it in the data declaration. The issue is not access -- it is time of construction. What I do not understand is how property code running after Initialize() completes could be accessing an object before it was constructed.

            --EV

            Comment


              #7
              I made a mistake there.

              "... cannot access the members of the class directly in the Properties section" should have read: "cannot return the individual members of the class directly in the Properties section"

              Comment


                #8
                It has no problem accessing the class. The only problem is what it takes to get the class constructed in time.

                Comment


                  #9
                  Originally posted by ETFVoyageur View Post
                  It has no problem accessing the class. The only problem is what it takes to get the class constructed in time.
                  You can manipulate the members of the object in the Properties declaration, but you must return the object that you declared, not just any or all members.

                  Comment


                    #10
                    I'm not following you.

                    The properties have no problem dealing with the class members. For example, the initial values get set just fine, so presumably Get() works.

                    Set() does not get an exception if I construct early enough.

                    The issue is why did Set() get an exception when I did not construct the object until Initialize() -- even though Print() output showed that Set() ran after Initialize() completed?

                    It's almost as if it somehow was working with an out of date reference ot the object.

                    --EV

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by gravdigaz6, Today, 11:40 PM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by MarianApalaghiei, Today, 10:49 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by XXtrader, Today, 11:30 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post XXtrader  
                    Started by love2code2trade, Yesterday, 01:45 PM
                    4 responses
                    28 views
                    0 likes
                    Last Post love2code2trade  
                    Started by funk10101, Today, 09:43 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post funk10101  
                    Working...
                    X