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

Sorting an Array

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

    Sorting an Array

    All,

    I'm looking to sort an array with 100 items in it, each of the items are doubles. Will the Array.Sort method work in NT?

    Thanks
    Shannon

    #2
    Hey Shansen,

    I would use an ArrayList type.

    you will have to add the following reference to the top of your code
    Code:
    using System.Collections;
    MSDN help is located here
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      mrlogic,

      Thanks for the reply.


      If you would be so kind, I'd like to improve my knowledge in coding within NT. From a little research, there are several differences between Arrays and Arraylists:
      1. An array can be of any data type and contain one data type while array list can contain any data type in the form of the object.
      2. With array you can not dynamically increase or decrease the size of array dynamically. You must the define the size of the array. You can change the size of the array with redim statement but still you have to define type. While with array list you can make list of any sizes. When a element or object is added to array list it size is automatically increase the capacity of the array list.
      3. once you delete the item in array it kept that one as empty like a[2]="" but in case of arraylist a[3]index occupies the position of a[2] and also if you try to insert a[2] value array will throw error if any items reside inside but in case of arraylist a[2] is inserted in same position but at the same time position of a[2] become a[3] if there is already an item exists.
      Given the size of the required array / arraylist is static and known at the outset, what are the benefits of using an arraylist over an array (e.g. efficiency of the sort method)?

      Any insight on your knowledge and experience would be appreciated
      Regards
      Shannon

      Comment


        #4
        In the case you present, the .Sort method is already coded for you. Additionally, future expansion is simpler since the ArrayList is a table of type object, which can hold any element type.
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          In addition to mrlogik's tips, you could check into this link here - http://dotnetperls.com/all-list
          BertrandNinjaTrader Customer Service

          Comment


            #6
            mrlogik & Bertrand,

            Thanks for your respective replies.

            Showing my novice programing stature, if an array is coded in NT as:

            Code:
            #region Variables
            	private double[] MyArray;
            #endregion
            
            protected override void Initialize()
            {
            	MyArray = new double[100];
            }
            What is the syntax to code an ArrayList?

            Thanks again
            Shannon
            Last edited by Shansen; 09-23-2009, 06:11 AM.

            Comment


              #7
              Shannon, don't have a snippet ready, but you can check the C# section of this link - http://msdn.microsoft.com/en-us/libr...arraylist.aspx
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Code:
                #region Variables
                private ArrayList() myArrayList;
                #endregion
                
                protected override void Initialize()
                {
                    myArrayList = new ArrayList();
                }
                mrlogik
                NinjaTrader Ecosystem Vendor - Purelogik Trading

                Comment


                  #9
                  mrlogik & Bertrand,

                  After more research, would the "List" class be a better direction to head? From what I've read it is more efficient and "type safe".

                  As always any insight would be appreciated.
                  Regards
                  Shannon
                  Last edited by Shansen; 09-26-2009, 05:17 AM.

                  Comment


                    #10
                    Shansen,

                    It really depends on the application, and what you're familiar with.

                    Do you have a specific design you need to fulfill, or is this more of a generic design?
                    mrlogik
                    NinjaTrader Ecosystem Vendor - Purelogik Trading

                    Comment


                      #11
                      mrlogik,

                      Funny you should ask what I'm familiar with. Both ArrayList and List are new to me.

                      I'm looking to identify where the CurrentBar's range is significantly above average (relative to recent bars).

                      With your help I've constructed an ArrayList, used it to store on a rolling basis the ranges of the last 100 bars (inserting the latest bar range at index 0, and removing index 101 in the ArrayList when the ArrayCount equals 101). This ArrayList is cloned to a new ArrayList, this new ArrayList is then sorted. The range listed at the 90th index of the sorted ArrayList is used to evaluate if the CurrentBar's range is in the top 10% of the most recent 100 bars.

                      As always any suggestions are welcome.
                      Regards
                      Shannon

                      Comment


                        #12
                        mrlogik,

                        It would appear I've hit a snag...

                        The method arraylist.sort() does not appear to work as expected?
                        Please find below a sample of the NinjaScript.

                        As always, any insight would be appreciated
                        Regards
                        Shannon

                        Code:
                        #region Using declarations
                        using System.Collections;
                        #endregion
                        
                        #region Variables
                          private ArrayList HistoricalRanges = new ArrayList();
                          private ArrayList SortedRanges = new ArrayList();	
                          private double AboveAverageBarRange;
                        #endregion
                        
                        protected override void OnBarUpdate()
                        {
                          HistoricalRanges.Insert(0, Range()[1]);
                          if (HistoricalRanges.Count > 100)
                            HistoricalRanges.RemoveAt(100);
                          SortedRanges = (ArrayList) HistoricalRanges.Clone();
                          Print ("");
                          Print (Time.ToString() + " Required range " + ((double) SortedRanges[Convert.ToInt32(Math.Min(SortedRanges.Count-1, 90))])); 
                          SortedRanges.Sort();			
                          Print (Time.ToString() + " Required range " + ((double) SortedRanges[Convert.ToInt32(Math.Min(SortedRanges.Count-1, 90))])); 	
                        }
                        Last edited by Shansen; 10-22-2009, 03:52 AM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by alifarahani, Today, 09:40 AM
                        6 responses
                        36 views
                        0 likes
                        Last Post alifarahani  
                        Started by Waxavi, Today, 02:10 AM
                        1 response
                        17 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        14 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