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

Generic collection SortedList

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

    Generic collection SortedList

    Hi,

    I just spent the better part of an hour figuring out why my code was failing and so I hope there is some lesson here for me.

    Does anyone know of any reason why I can't instantiate a SortedList in Initialize() or OnStartUp()?

    SortedList<int, myClass> mySortedList = new SortedList<int, myClass>() leaves me with a null mySortedList if I put the code anywhere except with the actual variable declaration.

    Is it used somehow/somewhere prior to Initialize()?

    Best Regards,
    Scott

    #2
    Hi Scott,

    Initialize() is reserved for setting script properties, adding series and instantiating objects like data series.

    ArrayLists pertain more to C# than general NinjaScript so unfortunately don't have a direct answer on why you can't instantiate in Initialize. There are a couple samples you can use for reference for where they are declared and populated.


    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Scott,

      Could you give a bit more detail, such as what code ran into the null?

      For example, is that pointer used in saving a configuration property, or did your own code encounter it?

      --EV

      Comment


        #4
        I am not 100% certain what you are asking but when I had the new SortedList... code in the Initialize() method (I tried OnStartUp as well), I kept getting an Object is not set to an instance... message.

        I managed to avoid having to go the VS debug route by about a zillion print statements. I was shocked to find it was the SortedList itself that was null (unitialized). I moved the new statement to the actual declaration and all was well.

        I am certain it is something basic I don't know about how NinjaTrader does things but it was a bit surprising. I can't think of any purely C# oriented reason it would matter when you instantiated it but I can't rule out something silly on my part.

        Best Regards,
        Scott

        Comment


          #5
          Originally posted by ScottB View Post
          I am not 100% certain what you are asking but when I had the new SortedList... code in the Initialize() method (I tried OnStartUp as well), I kept getting an Object is not set to an instance... message.

          I managed to avoid having to go the VS debug route by about a zillion print statements. I was shocked to find it was the SortedList itself that was null (unitialized). I moved the new statement to the actual declaration and all was well.

          I am certain it is something basic I don't know about how NinjaTrader does things but it was a bit surprising. I can't think of any purely C# oriented reason it would matter when you instantiated it but I can't rule out something silly on my part.

          Best Regards,
          Scott
          Where was that message coming from? What code was running that caused it?

          --EV

          Comment


            #6
            The actual error occurred when I tried to add an entry to the list; that is what was so surprising. Essentially I had a null reference to the SortedList. Once I initialized the SortedList when I first declared it, all was well.

            It was as though Initialize (and OnStartUp) couldn't initialize the SortedList. When stuff (stuff is such a great word) like that happens I always track down the exact reason so I will understand a bit more each time about how NinjaTrader internals work.

            For the record, I have been a professional trader/algo developer for almost 10 years now. I know C# very well and this is the first time I have run into anything like this (which is why it took me so long to suspect that being the root cause).

            Best Regards,
            Scott

            Comment


              #7
              Originally posted by ScottB View Post
              The actual error occurred when I tried to add an entry to the list; that is what was so surprising. Essentially I had a null reference to the SortedList. Once I initialized the SortedList when I first declared it, all was well.

              It was as though Initialize (and OnStartUp) couldn't initialize the SortedList. When stuff (stuff is such a great word) like that happens I always track down the exact reason so I will understand a bit more each time about how NinjaTrader internals work.

              For the record, I have been a professional trader/algo developer for almost 10 years now. I know C# very well and this is the first time I have run into anything like this (which is why it took me so long to suspect that being the root cause).

              Best Regards,
              Scott
              What code tried to add an entry to the list? The reason I am asking is that, like you, I try to track down anomalies so I can add to my knowledge of how the system actually works. I really doubt that creating the object in Initialize() fails -- the problem is more likely to be that can be too late.

              I had a similar problem awhile back which I traced to a property Set() method getting called before Initialize() had run. I solved it the same way you did -- I created my object with a data initializer.

              That kind of thing is the only explanation I can see for your case, too. One naturally assumes, and NT certainly never says otherwise, that Initialize() will be run after construction and before anything else is called. Some careful tracing tells me that is usually (but not always) true. My suspicion is that you encountered a case where it is not true.

              If you try to investigate this stuff, be sure you track which instance a method is being called on. There is common talk that "Initialize() is called many times". Well, sort of. What happens is that there are a lot of temp objects, and Initialize() is called on most of them. As far as I can tell, Initialize() is called at most once on any given instance.

              My bottom line has become that where possible I initialize all data with a data initializer rather than doing that in either Initialize() or OnStartUp(), because the data may be used before they get to run.

              -- EV
              Last edited by ETFVoyageur; 02-10-2011, 08:23 PM.

              Comment


                #8
                Ev, thanks that actually makes a lot of sense because the sorted list holds a class

                private SortedList<int, classStuff> classStuffList = new SortedList<int, classStuff>();


                My guess is there is something regarding using a class vs. a value type that caused the issue. The biggest thing for me is just when I think I am using straight out C#, I am reminded that under the covers, stuff happens.

                Funny, some stuff just never changes
                Last edited by ScottB; 02-10-2011, 08:15 PM.

                Comment


                  #9
                  The basic issue is that we need architectural information.

                  We need to know enough about how our object can be used that we can ensure objects we create will be created and initialized prior to actually being used. As you have discovered, that is not always as straight-forward as one would think.

                  I have suggested that NT provide such an architectural document and, to their credit, they agree it would be a good idea. Not sure when we shall see it, though..

                  --EV

                  Comment


                    #10
                    EV, I agree with the need for that kind of clarity but relatively few programmers ever get to that level of need.

                    Over the years I have worked with a lot of platform API's and I can say that short of working with stand alone FIX or an actual exchange API, NinjaTrader is about the best I have seen.

                    For me, they balance development speed with functionality. In the end if it makes you money, the rest will take care of itself.

                    Comment


                      #11
                      I agree that NT is very good.

                      One reason I want the documentation is the support issue. They tend to be pretty firmly of the persuasion that if it is not written up in their documentation it is not supported. Basic things like when we need to have objects constructed needs to be an agreed / supported sort of thing. The idea that the only way you and I can fix crashes is to do "unsupported" things is not appealing.

                      --EV

                      Comment


                        #12
                        Ah, the old unsupported trick. My favorite is FlattenEverything in unmanaged order mode or adding a form to a strategy.

                        Since you obviously know what you are talking about - have you tried running a TCP/IP connection in a background worker thread or possibly using named pipes via WCF (if on the same box)?

                        That is my next project, the goal being to run NinjaTrader as a black box in downtown Chicago with me sitting somewhere sunny controlling the strategy via a stand alone application with no latency or internet worries.

                        Scott

                        Comment


                          #13
                          No, I have not done anything like that. I am quite new to NT and C#, although I have programmed for a long time in C++.

                          --EV

                          Comment


                            #14
                            EV, I have programmed in everything from assembly language to C++ and I can tell you, C# is the best of all worlds if you don't need absolute control of absolutely everything.

                            It is easy to forget, we program to trade to make money as I said before, if it makes you money...

                            Scott

                            Comment


                              #15
                              Originally posted by ScottB View Post
                              EV, I have programmed in everything from assembly language to C++ and I can tell you, C# is the best of all worlds if you don't need absolute control of absolutely everything.

                              It is easy to forget, we program to trade to make money as I said before, if it makes you money...

                              Scott
                              Ah yes, the halcyon days of Assembler programming remind me of this piece of humor:

                              ref: http://www.cs.bgu.ac.il/~omri/Humor/shoot.html

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              38 views
                              0 likes
                              Last Post alifarahani  
                              Started by Waxavi, Today, 02:10 AM
                              1 response
                              18 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by Kaledus, Today, 01:29 PM
                              5 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by Waxavi, Today, 02:00 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by gentlebenthebear, Today, 01:30 AM
                              3 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X