Free Code to Fix TradeStation’s 2D Sort

During a recent project, I needed to sort a 2D array so I used the built-in 2D array sort which is included within TradeStation. The documentation for TradeStation’s 2D Sort is as follows:

Sort2DArray(PriceArray, Size1, Size2, HiLo)

At first glance this appeared to be a relatively simple function. The parameters were straightforward as you simply provided PriceArray – the name of the array to sort – the first and second dimension sizes to sort upon (as represented by Size1 and Size2, respectively) and HiLo which is either a 1 to sort descending or a -1 to sort ascending.

Upon using this function, the order of values of the two-dimensional array specified by PriceArray are changed. The function itself returns 1.

To perform a descending sort of a user-declared 2D array, one might do the following:

Array: myArray[30,4](0);
... assign the values to the array ...
Value1 = Sort2DArray(myArray, 30, 4, 1);

I thought this would be easy but it was not. I could not get my code to work, no matter how hard I tried. I discussed this issue with my friend, Sam Tennis (who is also a TradeStation expert available on this site). He confirmed to me that the sort was not working as described. First the array is defined as column then row in the function (that is it references the Y axis before the X axis) not row then column (X, Y) co-ordinate system that is used everywhere else. In addition, I found that my array was also getting corrupted.

Sam was nice enough to supply a new sorting function to me. I am allowed to share this function as free TradeStation code on this site so long as you (and I) don’t remove Sam’s copyright information. This function is even more expandable as you can sort both 2D and 3D arrays just by changing the parameter.

The function is available as free TradeStation code by filling out the form on the right (not only can you get this code but you can get a plethora of other code as well)!

For an example of how to use skt_2D_Sort in your own indicator, check out the following example indicator:

*Consts: Size1 (05),
        Size2 (02) ;
Arrays: Array_In [Size1, Size2] (0.00) ;
Inputs: Sort_Rows               (05),  // How many of the Rows are in use (to be sorted)?
        Base_Val                (00),  // Use the zero element or not?  Input 00 or 01
        Sort_Col                (01),  // which column holds the sort value
        Sort_Dir               (+01) ; // +01 for Descending, -01 for Ascending
Dummy  = skt_2D_Sort (Array_In, Sort_Rows, Base_Val, Sort_Col, Sort_Dir);

Inputs: My_Index (01),  // Find Strategy #1
        My_Rank  (01) ; // Find most profitable

Consts: Max_Rows (05),
        Max_Cols (02),
        sub_Val  (01),  // the key value to be sorted
        sub_Ndx  (02) ; // the index back to original source
Arrays: myArray [max_Rows, Max_Cols] (0) ;
Vars  : Dummy    (00),
        Loop     (00),
        Str      (""),
        Index    (00),
        Rank     (00) ;

if  Barnumber <= 01 and BarStatus (01)  = 02 then begin

myArray [01,01]=1.5;
myArray [01,02]=1;
myArray [02,01]=5.5;
myArray [02,02]=5;
myArray [03,01]=2.5;
myArray [03,02]=2;
myArray [04,01]=3.5;
myArray [04,02]=3;
myArray [05,01]=4.5;
myArray [05,02]=4;

if  LastBarOnChart then begin
// Print (Date:6:0, ", ", Time:5:0) ;
 // Str  = "" ;
 // for Loop  = 01 to Max_Rows begin
//  Str  = Str + "#" + NumToStr (Loop, 00) + " " + 
// NumToStr (myArray [Loop, 01], 02) + ", " + 
// NumToStr (myArray [Loop, 02], 00) + NewLine ;
//  end ;
//  Print ("Pre :", NewLine, Str) ;

//  Value1  = MAR_OrdinalRank (myArray, 05, Ranking) ;
  Dummy  = skt_2D_Sort    (myArray, 05, 01, 01, +01 {, TRUE}) ;
  Index  = MAR_Find_Index (myArray, my_Index) ;
  Rank   = MAR_Find_Rank  (myArray, my_Rank) ;

  Plot1 (Index, "Index", Default, Default, Default) ;
  Plot2 ( Rank, " Rank", Default, Default, Default) ;
end ;

Custom Programming Services provided by:

                 Samuel K. Tennis
                    PO Box 1984
         Ft. Walton Beach, Florida  32549
          voice :  1(850) 243-5105
          cell  :  1(850) 582-7342
          fax   :  1(510) 743-8274

          AIM   : SKTennis
          GMail : SKTennis
          Yahoo : SamuelKTennis

Note, debugging comments have been removed from this article for space reasons.

This indicator calls 2 other functions which I have included
Index = MAR_Find_Index (myArray, my_Index) ;
Rank = MAR_Find_Rank (myArray, my_Rank) ;

MAR_Find_Index gives us a unique identifier to put in column 2. MAR_Find_Rank returns the sorted value. The My_Rank variable returns the offset in the array. In our case we used 1 which returns the highest value and 2 returns the second highest.

Check out this unique and awesome free TradeStation code by filling out the form on the right!

Leave a Reply

Your email address will not be published. Required fields are marked *