sortData (var* Data, int Length)

Sorts the Data array in ascending (Length > 0) or descending (Length < 0) order.

sortIdx (var* Data, int Length): int*

Does not affect the Data array, but returns a sorted int array of the indices from the smallest to the greatest Data value.

sortRank (var* Ranks, var* Data, int Length): int*

Stores the rank of any Data element in 0..1 range in the Ranks array, from 0 for the smallest to 1 for the greeatest element. If the elements are OHLC prices, candle patterns can be encoded this way for machine learning algorithms.

findIdx (var* Data, int Length, var Value): int

Returns the index of the highest Data element below or equal to Value, or -1 when no such element was found. 


Data var / double array or series to be sorted
Rank var / double array to receive the Data ranks.
Length Length of the Data array; negative for sorting in descending order.
Value Value to be found in the Data array.,


Temporary pointer to an int array containing the numbers of the Data values after sorting (sortIdx). The array will be overwriten by the next sortIdx call.



// Spearman trend indicator
var Spearman(var* Data,int Period)
Period = clamp(Period,2,g->nLookBack);
int* Idx = sortIdx(Data,Period);
var sum = 0; int i,n;
for(i=0,n=Period-1; i<Period; i++,n--)
sum += (n-Idx[i])*(n-Idx[i]);
return 1. - 6.*sum/(Period*(Period*Period-1.));

See also:

randomize, frechet, series, dataSort

► latest version online