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

Array question...

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

    Array question...

    How do I do initialize an array dynamically? I don't know how many placeholders I'll need.

    private double[] myArray;

    myArray = new double[??];

    Also:
    I need to re-init the array on new bar:
    I need a suggestion on best way to do this. Below is not working.

    protected override void OnBarUpdate()
    {
    Print(Bars.PercentComplete*100);
    if (Bars.PercentComplete*100 <= 0)
    {
    Print("First tick");
    myHits = new double[20];
    i = 0;
    }


    myHits[i] = Close[0];
    i++;

    for (int j=0;j<myHits.Length;j++)
    {
    Print("myHits["+j+"] = "+myHits[j]);
    }
    Plot0.Set(Close[0]);
    }
    Last edited by funk101; 05-17-2007, 03:57 PM. Reason: Add second part of question

    #2
    You can't.

    Information on arrays - http://msdn2.microsoft.com/en-us/lib...tem.array.aspx

    You can check out an ArrayList which is a dynamic array - http://msdn2.microsoft.com/en-us/lib...arraylist.aspx
    RayNinjaTrader Customer Service

    Comment


      #3
      I should add. If you go down the path of using an ArrayList, here are some pointers -

      - Make sure you override the Dispose() method and set the variable holding the ArrayList to null
      - ArrayList hold objects of type object meaning, you can stuff anything you want in them, this means when you reference data being held by the ArrayList you need to cast it to the correct data type.

      Code:
      private ArrayList al = new ArrayList();
      al.Add("NinjaTrader");
      Print((string) al[0]);
      See the following for information on the Dispose() method - http://www.ninjatrader-support.com/H...6/Dispose.html
      RayNinjaTrader Customer Service

      Comment


        #4
        Hmm, that would be nice...

        I'll check it. Please see this new post about re-initing() below. I added another question.

        Comment


          #5
          You can create a new array increase in size and copy the content of the old one into the new one, see the DOC reference I sent you.

          If you need a way to store data per bar meaning, one array element for each bar on a chart I suggest using our DataSeries class.

          RayNinjaTrader Customer Service

          Comment


            #6
            Can I just do this...

            if (Bars.PercentComplete*100 >= 98)
            {
            Print("First tick");
            myHits = new ArrayList();

            i = 0;
            }

            Comment


              #7
              Somethings not right, getting system out of memory errors...

              Ok, what I want to do is when the market keeps testing a resistance, record how many times it hits resistance. Then on next bar, wipe clean the ArrayList(), and start again. So, I don't think I want to have arrays of arrays, just per bar, and reinit().

              Comment


                #8
                Then an array is not the right approach. All you need is a variable that increments.

                Code:
                private int hitTest = 0;
                
                Within OnBarUpdate()
                
                if (FirsTickOfBar)
                    hitTest = 0;
                else
                    hitTest++; // This increments the value by 1
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Um, well...

                  Yeah that's cool, I thought 'bout that too, and I have that in place, however, I could have some control over what I want to do with the info if I store it in an array, the storing is fine, however, I can't get it to reinit() when if (FirstTickOfBar) {} is called:

                  #region variables
                  private double[] myHits;
                  #endregion

                  protected override void OnBarUpdate()
                  {
                  if (FirstTickOfBar)
                  {
                  i = 0;
                  hitTest = 0;
                  Print("First bar");
                  myHits = new double[50];
                  }
                  else
                  {
                  hitTest++;
                  myHits[i] = Close[0];
                  Print("My hits["+i+"] = "+myHits[i]);
                  i++;
                  }
                  }

                  Comment


                    #10
                    Understood. The MSDN reference I provided on arrays is your best bet to figure out what is going wrong.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      uh...

                      Yeah, one would think However that MSDN site can be pretty unfriendly. For instance, I've been swimming in that array section for about an hour, and still can't figure out why that won't reinit;
                      Oh well.

                      Comment


                        #12
                        k, I figured it out...just for the record...

                        I was declaring an array of 50. Well, it needed to write passed that. So, when it did, on next bar app would fail. I upped the value to 5000 just to see and it works fine. So, I guess I should now figure out the ArrayList() thingy, since it truly needs a dynamic place holder.

                        Comment


                          #13
                          ArrayLists() rule!

                          Yeah, that's the ticket. I like how it assigns 4 chunks at a time. Then I Dispose() with myList.Clear(); Thanks for the pointer.(no programming pun intended)

                          Comment


                            #14
                            Great.

                            Don't forget to dispose of the object by overriding the Dispose() method of the indicator re: Post #3.
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              You might also try a Generic List, from what I've been reading it has better performance than ArrayList.




                              Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


                              Just another consideration, I'm just now playing around with them, so I don't know from experience yet.

                              VT

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by terofs, Yesterday, 04:18 PM
                              1 response
                              21 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by CommonWhale, Today, 09:55 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Gerik, Today, 09:40 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by RookieTrader, Today, 09:37 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post RookieTrader  
                              Started by alifarahani, Today, 09:40 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X