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:

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:

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!

Free TradeStation Code

Get free, simplified versions of the the tools that the TradeStation experts
use in their daily research and system building. These tools help you learn
EasyLanguage as they are entirely open source and let you build complex systems
without needing to know how to code.

All you need to provide is a name and e-mail address. No credit card or address
required!

Featured Product

Build adaptive indicators in your TradeStation strategies. The adaptive indicator library automatically tunes its indicators to half of the current dominant cycle based on use of the Hilbert transform...
Learn More

About Murray Ruggiero Jr.

Murray Ruggiero is the chief systems designer, and market analyst at TTM. He is one of the world's foremost experts on the use of inter-market and trend analysis in locating and confirming developing price moves in the markets. Murray is often referred to in the industry as the Einstein of Wall Street.
Read more...