Announcement

Collapse
No announcement yet.

Sort Numerical Array values

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

  • Sort Numerical Array values

    I am running this study in a 150 tick chart for AB #F :

    function preMain()
    {
    setPriceStudy(true);
    }


    var PriceArray = new Array(21);

    function numericalsort(a, b){ return (a-b); }

    function main()
    {

    PriceArray[20] = high(0);

    for(i = 0; i <= 20; i++)

    PriceArray[i] = PriceArray[i + 1];

    PriceArray[20] = low(0);

    for(i = 0; i <= 20; i++)

    PriceArray[i] = PriceArray[i + 1];

    debugClear();
    debugPrintln("PriceArray: " + PriceArray);
    //var newprice = PriceArray.sort(numericalsort);
    // debugPrintln("newprice: " + newprice);

    }

    This puts the high and low price values of the last 10 price bars into an array named PriceArray. I want to determine the highest or lowest value in the array, so I added the function 'numericsort' to do this, and then had intended on reading the first or last index position in the array to get the value I want. My problem is that when I use the sort function to sort the values, it also CHANGES the price values in the array. If you run this with and without the debug line commented out, you'll see this change.

    Does anyone have a method for accomplishing what I need to do?

    Thanks

  • #2
    Hi werosen,

    this is because when you are implementing the .sort method, you are sorting that array. Try sorting a copy of the array as follows:


    PHP Code:
    var newprice hiLoSort(PriceArray);//replace your var newprice line with this

    //add this new function
    function hiLoSort(tmp){
      var 
    ntmp tmp.sort(numericalsort);//now you are sorting the original array, but a copy
      
    return  ntmp//returning the sorted array

    I hope this helps.

    Comment


    • #3
      I tried your method, but it still changed the values returned in the original array.

      Comment


      • #4
        I'm an idiot Steve. I just figured out the problem. I had the index values wrong for the array where I was adding a new value and shifting the array over, so my original sort code worked. Thanks for the reply and I'm sorry to have wasted your time.

        Thanks again.

        Warren

        Comment


        • #5
          Redux of Numerical Array Sort

          I thought this problem had been solved, but it turns out it hasn't been, so here it is again.

          I am running this study in a 150 tick chart for AB #F :

          function preMain()
          {
          setPriceStudy(true);
          }

          var PriceArray = new Array(20);

          function numericalsort(a, b){ return (a-b); }


          function main()
          {

          PriceArray.pop();
          PriceArray.unshift(high(-1));
          PriceArray.pop();
          PriceArray.unshift(low(-1));

          debugClear();
          debugPrintln("PriceArray: " + PriceArray);

          //var newprice = PriceArray.sort(numericalsort);
          // debugPrintln("newprice: " + newprice);

          }

          This puts the high and low price values of the last 10 price bars into an array named PriceArray. I want to determine the highest or lowest value in the array, so I added the function 'numericsort' to sort the array's elements into ascending order, and then had intended on reading the first or last element in the array to get the value I want. My problem is that when I use the sort function to sort the values, it also CHANGES the price values in the array. If you run this with and without the debug line commented out, you'll see this change.

          Does anyone have a method for accomplishing what I need to do?

          Thanks
          Last edited by werosen; 07-02-2005, 08:23 PM.

          Comment


          • #6
            Warren,

            I thought I had it figured out for you... but now I am not sure. I won't be able to work on for a bit due my present schedule, but I will take a look at it over the next couple of days.

            Comment


            • #7
              When I want to know the highest price I use the DochianChannel function.

              Comment


              • #8
                finding high and low

                if i get what your wanting, u want to find the highest and lowest values in an array.
                see if the attached script works for u.

                bob
                Attached Files

                Comment


                • #9
                  Warren,

                  The issue here is that when sorting, as I had previously indicated, you are sorting that object. The secret to sorting in your case is to make a copy and sort the copy. The problem became difficult because when passing an array object, as it is passed by reference. In effect, you are still sorting the same array object. To solve that, you have to somehow pass the array by value versus reference. In the attached code I did that and it works. I have attached the modified code as I mentioned and also added some steps to check for when all bars are loaded, when a newbar comes in, plus a barindex check.

                  Having gone through and I believe solved your problem, your method of creating an array and sorting it is overkill for what you want to do. As David recommended, the DochianChannel function is simpler and a much more appropriate method to perform what you are trying to do. That should be very simple for you to figure out as compared to the creation of arrays and sorting.

                  btw, while I was composing this, lucid78 also came up with an alternative solution which should work as well.

                  PHP Code:
                  function preMain() 
                  {
                      if (
                  getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload    
                        
                  debugClear();
                      }
                      if (
                  getBarState() == BARSTATE_NEWBAR) { 
                          
                  PriceArray.pop();
                          
                  PriceArray.unshift(high(-1));
                          
                  PriceArray.pop();
                          
                  PriceArray.unshift(low(-1));

                          if (
                  getCurrentBarIndex()>-10){
                              var 
                  newprice hiLoSort(PriceArray);//replace your var newprice line with this
                              //newprice = newprice.sort(numericalsort);

                              
                  debugPrintln("PriceArray["+getCurrentBarIndex()+"]: " PriceArray);

                              
                  debugPrintln("newprice["+getCurrentBarIndex()+"]: " newprice);
                              
                  debugPrintln("");
                          }
                      }



                  //add this new function
                  function hiLoSort(tmp){
                    var 
                  ntmp = new Array(tmp.length);//breaking the pass by reference tie
                    
                  for (var i=0;i<tmp.length;i++)ntmp[i]=tmp[i];//breaking the pass by reference tie
                    
                  return  ntmp.sort(numericalsort); //returning the sorted array

                  PHP Code:
                  (excerpt)

                  PriceArray[-1]: 1199.75,1200.25,1199.75,1200.25,1199.5,1200,1199.5,1200,1199.5,1199.75,1199.5,1200,1199.75,1200.25,1199.75,1200.25,1199.75,1200.25,1199.75,1200
                  newprice
                  [-1]: 1199.5,1199.5,1199.5,1199.5,1199.75,1199.75,1199.75,1199.75,1199.75,1199.75,1199.75,1200,1200,1200,1200,1200.25,1200.25,1200.25,1200.25,1200.25

                  PriceArray
                  [0]: 1199.75,1200.25,1199.75,1200.25,1199.75,1200.25,1199.5,1200,1199.5,1200,1199.5,1199.75,1199.5,1200,1199.75,1200.25,1199.75,1200.25,1199.75,1200.25
                  newprice
                  [0]: 1199.5,1199.5,1199.5,1199.5,1199.75,1199.75,1199.75,1199.75,1199.75,1199.75,1199.75,1200,1200,1200,1200.25,1200.25,1200.25,1200.25,1200.25,1200.25 
                  I hope this makes sense.

                  Comment


                  • #10
                    This seems so much easier to me...

                    var vDonchian = new DonchianStudy(20, 0);

                    function preMain() {
                    setPriceStudy(true);
                    setStudyTitle("");
                    setCursorLabelName("hi", 0);
                    setDefaultBarStyle(PS_SOLID, 0);
                    setDefaultBarFgColor(Color.red, 0);
                    setDefaultBarThickness(1, 0);
                    setPlotType(PLOTTYPE_LINE, 0);
                    }

                    function main() {
                    return new Array ( vDonchian.getValue(DonchianStudy.UPPER) , vDonchian.getValue(DonchianStudy.LOWER));

                    }

                    Comment


                    • #11
                      Thanks for the responses. The code I wrote was part of a much larger study, so simply getting the high-lows without the flexibility to filter the highs and lows with additional conditions doesn't do the trick. I decided to attack the issue with brute force and just do an element by element comparison in the array. Since the high and lows are used in different situations I broke them out int6 seperate arrays to do this, but obviously the high-low values could be tracked in a single array.

                      Here was my final solution in case it would be of assistance to anyone.

                      function preMain()
                      {
                      setPriceStudy(true);
                      }

                      var HighPriceArray = new Array(10);

                      var LowPriceArray = new Array(10);

                      HighPrice = 0;
                      LowPrice = 0;


                      function main()
                      {

                      if (getBarState() == BARSTATE_NEWBAR)
                      {
                      HighPriceArray[10] = high(-1);

                      for(i = 0; i <= 9; i++)

                      HighPriceArray[i] = HighPriceArray[i + 1];

                      if (HighPriceArray[0] > HighPriceArray[1])
                      {HighPrice = HighPriceArray[0]}
                      else {HighPrice = HighPriceArray[1]}
                      if (HighPrice < HighPriceArray[2])
                      {HighPrice = HighPriceArray[2]}
                      if (HighPrice < HighPriceArray[3])
                      {HighPrice = HighPriceArray[3]}
                      if (HighPrice < HighPriceArray[4])
                      {HighPrice = HighPriceArray[4]}
                      if (HighPrice < HighPriceArray[5])
                      {HighPrice = HighPriceArray[5]}
                      if (HighPrice < HighPriceArray[6])
                      {HighPrice = HighPriceArray[6]}
                      if (HighPrice < HighPriceArray[7])
                      {HighPrice = HighPriceArray[7]}
                      if (HighPrice < HighPriceArray[8])
                      {HighPrice = HighPriceArray[8]}
                      if (HighPrice < HighPriceArray[9])
                      {HighPrice = HighPriceArray[9]}

                      LowPriceArray[10] = low(-1);

                      for(i = 0; i <= 9; i++)

                      LowPriceArray[i] = LowPriceArray[i + 1];

                      if (LowPriceArray[0] < LowPriceArray[1])
                      {LowPrice = LowPriceArray[0]}
                      else {LowPrice = LowPriceArray[1]}
                      if (LowPrice > LowPriceArray[2])
                      {LowPrice = LowPriceArray[2]}
                      if (LowPrice > LowPriceArray[3])
                      {LowPrice = LowPriceArray[3]}
                      if (LowPrice > LowPriceArray[4])
                      {LowPrice = LowPriceArray[4]}
                      if (LowPrice > LowPriceArray[5])
                      {LowPrice = LowPriceArray[5]}
                      if (LowPrice > LowPriceArray[6])
                      {LowPrice = LowPriceArray[6]}
                      if (LowPrice > LowPriceArray[7])
                      {LowPrice = LowPriceArray[7]}
                      if (LowPrice > LowPriceArray[8])
                      {LowPrice = LowPriceArray[8]}
                      if (LowPrice > LowPriceArray[9])
                      {LowPrice = LowPriceArray[9]}
                      }
                      }

                      Comment


                      • #12
                        Warren
                        FWIW all the following from your code

                        PHP Code:
                        for(0<= 9i++)

                        HighPriceArray[i] = HighPriceArray[1]; 

                        if (
                        HighPriceArray[0] > HighPriceArray[1])
                        {
                        HighPrice HighPriceArray[0]}
                        else {
                        HighPrice HighPriceArray[1]}
                        if (
                        HighPrice HighPriceArray[2])
                        {
                        HighPrice HighPriceArray[2]}
                        if (
                        HighPrice HighPriceArray[3])
                        {
                        HighPrice HighPriceArray[3]}
                        if (
                        HighPrice HighPriceArray[4])
                        {
                        HighPrice HighPriceArray[4]}
                        if (
                        HighPrice HighPriceArray[5])
                        {
                        HighPrice HighPriceArray[5]}
                        if (
                        HighPrice HighPriceArray[6])
                        {
                        HighPrice HighPriceArray[6]}
                        if (
                        HighPrice HighPriceArray[7])
                        {
                        HighPrice HighPriceArray[7]}
                        if (
                        HighPrice HighPriceArray[8])
                        {
                        HighPrice HighPriceArray[8]}
                        if (
                        HighPrice HighPriceArray[9])
                        {
                        HighPrice HighPriceArray[9]}

                        for(
                        0<= 9i++)

                        LowPriceArray[i] = LowPriceArray[1];

                        if (
                        LowPriceArray[0] < LowPriceArray[1])
                        {
                        LowPrice LowPriceArray[0]}
                        else {
                        LowPrice LowPriceArray[1]}
                        if (
                        LowPrice LowPriceArray[2])
                        {
                        LowPrice LowPriceArray[2]}
                        if (
                        LowPrice LowPriceArray[3])
                        {
                        LowPrice LowPriceArray[3]}
                        if (
                        LowPrice LowPriceArray[4])
                        {
                        LowPrice LowPriceArray[4]}
                        if (
                        LowPrice LowPriceArray[5])
                        {
                        LowPrice LowPriceArray[5]}
                        if (
                        LowPrice LowPriceArray[6])
                        {
                        LowPrice LowPriceArray[6]}
                        if (
                        LowPrice LowPriceArray[7])
                        {
                        LowPrice LowPriceArray[7]}
                        if (
                        LowPrice LowPriceArray[8])
                        {
                        LowPrice LowPriceArray[8]}
                        if (
                        LowPrice LowPriceArray[9])
                        {
                        LowPrice LowPriceArray[9]}
                        }

                        can be be condensed to the following

                        PHP Code:
                        for(0<= 9i++) {
                            if(
                        == 0) {
                                
                        HighPrice HighPriceArray[i];
                                
                        LowPrice =  LowPriceArray[i];
                            } else {
                                
                        HighPrice Math.max(HighPriceHighPriceArray[i]);
                                
                        LowPrice Math.min(LowPriceLowPriceArray[i]);
                            }   

                        The result is exactly the same
                        Alex

                        Comment


                        • #13
                          Sorting..

                          "Sorting" can be a difficult task for some people to understand... The trick is to built a multiple loop system that uses a "container" variable to facilitate sorting...

                          Here is an example....

                          Let's say I had an array of...

                          MyArray = (45,30,3,7,14,65,22,25,21,43,76,1);

                          and I wanted to get this sorted (from lowest to highest #)..

                          First, we need a multi-loop variable "MultiX" that will allow us to loop thru the array ONCE for every item in the array.

                          Second, I would need a variable "MasterX" that is used to loop through the array. This variable is our starting location for the sorting routine...

                          Next, I need a secondary variable "SecondX" while is used to loop through the array (from a starting point of "MasterX") to compare the array values.

                          Next, I need a container variable "ContainerValue" (to facilitate the sorting routine)..

                          so, here we go..

                          PHP Code:

                          var MyArray = new Array(45,30,3,7,14,65,22,25,21,43,76,1);


                          function 
                          preMain() {
                              
                          setPriceStudy(true);
                              
                          setStudyTitle("Sort Example");
                          }

                          function 
                          main() {

                              
                          drawTextPixel(4,41" Diaplay Array "+"@URL=EFS:DispArray" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull12"Display");
                              
                          drawTextPixel(4,24" ASC Sort "+"@URL=EFS:ASCSort" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull12"ASC");
                              
                          drawTextPixel(4,4" DESC Sort "+"@URL=EFS:DESCSort" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull12"DESC");

                            
                           
                          // DispArray();


                            //SortArray("ASC");
                          }


                          function 
                          ASCSort() {
                           
                          SortArray("ASC");
                           
                          DispArray();
                          }

                          function 
                          DESCSort() {
                           
                          SortArray("DESC");
                           
                          DispArray();
                          }


                          function 
                          SortArray(type) {
                            
                          //  type : "ASC" = Ascending  /  "DESC" = Descending

                          var MultiX null;
                          var 
                          MasterX null;
                          var 
                          SecondX null;
                          var 
                          ContainerValue null;

                           for (
                          MultiX 0MultiX <= (MyArray.length-1); MultiX++) {
                            for (
                          MasterX 0MasterX <= (MyArray.length-2); MasterX++) {
                             for (
                          SecondX MasterX SecondX <= (MyArray.length-2); SecondX ++) {

                              if (
                          type == "ASC") {
                               if (
                          MyArray[SecondX] > MyArray[SecondX+1]) {
                                 
                          ContainerValue MyArray[SecondX];
                                 
                          MyArray[SecondX] = MyArray[SecondX+1];
                                 
                          MyArray[SecondX+1] = ContainerValue;
                               }
                              } 
                          //  end if ASC sort
                              
                          if (type == "DESC") {
                               if (
                          MyArray[SecondX] < MyArray[SecondX+1]) {
                                 
                          ContainerValue MyArray[SecondX];
                                 
                          MyArray[SecondX] = MyArray[SecondX+1];
                                 
                          MyArray[SecondX+1] = ContainerValue;
                               }
                              } 
                          //  end if ASC sort


                             
                          }  //  End SecondX Loop
                            
                          }  //  End MasterX Loop
                           
                          }  //  End MultiX Loop


                          //  End Function


                          function DispArray() {

                          var 
                          null;
                           
                          debugPrintln(" -- [ Begin Array Display ] -------------------------------------------- ");
                           for (
                          0<= (MyArray.length-1); X++) {
                               
                          debugPrintln(" ["+X+"] : "+MyArray[X]);
                           }  
                          //  End MasterX Loop
                           
                          debugPrintln(" -- [ End Array Display ] -------------------------------------------- ");
                          //  End Function 
                          Example code is attached..

                          B
                          Attached Files
                          Brad Matheny
                          eSignal Solution Provider since 2000

                          Comment


                          • #14
                            Man, there is some really powerful code in this topic page. I wish to thank all who have contributed here and I think the best way is to see if I can add anything usefull from what I've learned.

                            Alex condensed werosen's formula with a "for" loop statement that is very nice which begins with:
                            for(i = 0; i <= 9; i++)
                            I think that if I were to change anything in that loop statement it would be to make it read as:
                            for(i = 0; i <10; i++)
                            You say "potato", I say "potahto".....

                            Doji3333 wrote some code that I've learned much from and helped me to understand exactly what I was looking for and then some. Thank you, Doji, very much. The lessons given, particularly in understanding the logic behind the code, far outway any changes I might now suggest.

                            I would consider making a 2 small changes in the for statement in the function DispArray().
                            1. I'd denote the "X" variable as an "i" variable instead. This just makes sense because what we are referring to with the variable in the code is the index number in the array so it just reads easier to me that way.
                            2. And like above, I would eliminate the -1 in the length portion of your for statement.
                            So:

                            var i = null;
                            debugPrintln(" -- [ Begin Array Display ] -------------------------------------------- ");
                            for (i = 0; i < MyArray.length; i++) {
                            debugPrintln(" ["+i+"] : "+MyArray[i]);


                            Again with the you say "potato" and I say "potahto" theme. Really no big deal.

                            The last change I would make to this code would be to incorporate the numerical sort statement that werosen used in his code in place of the function SortArray(type) used. Perhaps I'm missing something but I think it accomplishes the same thing.
                            So........


                            PHP Code:
                            var myArray= new Array(45,30,3,7,14,65,22,25,21,43,76,1);

                            function 
                            preMain() {
                                
                            setPriceStudy(true);
                                
                            setStudyTitle("Sort Example");
                            }

                            function 
                            main() {

                                
                            drawTextPixel(4,61" Display Array "+"@URL=EFS:DispArray" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull10"Display");
                                
                            drawTextPixel(4,44" ASC Sort "+"@URL=EFS:ASCSort" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull10"ASC");
                                
                            drawTextPixel(4,27" DESC Sort "+"@URL=EFS:DESCSort" Color.blackColor.RGB(0xE00xE00xE0), Text.FRAME Text.ONTOP Text.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull10"DESC");
                              
                            }

                            function 
                            ASCSort() {            // ASSIGNS DISPLAYED "ASC Sort" BUTTON, 
                                
                            SortArray("ASC");           // ON CHART, TO ASCENDING SORT FUNCTION.
                                
                            DispArray();
                            }

                            function 
                            DESCSort() {           // ASSIGNS DISPLAYED "DESC Sort" BUTTON, 
                             
                            SortArray("DESC");             // ON CHART, TO DESCENDING SORT FUNCTION.
                             
                            DispArray();
                            }

                            function 
                            DispArray() {          // ASIGNS DISPLAYED "Display Array" BUTTON,
                                                            // ON CHART, TO DISPLAY THE ARRAY FUNCTION.                     
                                
                            var null;                                                                               
                                
                            debugPrintln(" -- [ Begin Array Display ] -------------------------------------------- ");  
                                for (
                            0myArray.lengthi++) {                                                 
                                    
                            debugPrintln(" ["+i+"] : "+myArray[i]);                                                  
                                }                                                                                           
                                
                            debugPrintln(" -- [ End Array Display ] -------------------------------------------- ");    
                            }                                                                                               
                                
                            function 
                            SortArray(type) {      //  type : "ASC" = Ascending  /  "DESC" = Descending
                                
                                
                            if (type == "ASC") {        //Sort numerically and ascending:   
                                    
                            myArray.sort(function(a,b) {return b});         //Array now becomes [1, 3, 7, 14, 21, 22, 25, 30, 43, 45, 65, 76]    
                                
                            }
                                
                                if (
                            type == "DESC") {       //Sort numerically and descending:       
                                    
                            myArray.sort(function(a,b) {return a});         //Array now becomes [76, 65, 45, 43, 30, 25, 22, 21, 14, 7, 3, 1]        
                                
                            }

                            Comment


                            • #15
                              You are very welcome. I know I have posted some examples of a sorting feature for arrays "years ago". In the old days, this was called a "bubble sort".

                              I've been very busy with my own projects and other client's projects, but if you have any questions, feel free to ask or PM.

                              I love it when an eSignal user finds the resources they need on this forum. That's why we all try to help. Good luck.
                              Brad Matheny
                              eSignal Solution Provider since 2000

                              Comment

                              Working...
                              X