SparseCompress
- compress sparse array

Miscellaneous functions
(AmiBroker 60)


SYNTAX SparseCompress( query_points, data )
RETURNS ARRAY
FUNCTION The function gets values from 'data' array at points defined by non-zero values of 'query_points' array and compresses them so they are squeezed at the end of resulting array
EXAMPLE only_when = ( Month() % 2 ) == 0; // even months only

x = SparseCompress( only_when, Close ); // compact sparse data

y = MA( x, 10 ); // regular calculation

y = SparseExpand( only_when, y ); // expand sparse data

Plot( C, "Price", colorDefault, styleBar );
Plot( y, "Sparse MA from even months", colorRed );


function SparseCompressEquiv( sparse_array, data_array )
{
     result = Null;
    
     j = BarCount - 1;
     for( i = BarCount - 1; i >= 0; i-- )
     {
       if( sparse_array[ i ] ) result[ j-- ] = data_array[ i ];
     }
    
     return result;
}

function SparseExpandEquiv( sparse_array, data_array )
{
     result = Null;
    
     j = BarCount - 1;
     for( i = BarCount - 1; i >= 0; i-- )
     {
       if( sparse_array[ i ] ) result[ i ] = data_array[ j-- ];
     }
    
     return result;
}
SEE ALSO SparseExpand() function

References:

The SparseCompress function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.