Losing a matrix value after sort

image

After adding values to a matrix, sorting them (only 1 in test results but the same happens with more) the value seems to changed (highlighted in yellow below). I am surprised the first _trace puts out the value as I thought I had to use a "numtostr". Is that part of my problem?

The staticvars are loaded by

accumResult = floor( ( accumResult / accumCnt ) );
StaticVarSet("result_" + Name(), LastValue( accumResult))

Hope it's not too trivial to be wasting your time.

Cheers,
Gary.

        scoreList = StaticVarInfo( "result_*", "list" );
        
        scoreNames = "";
        scoreMatrix = Matrix( 2000, 2, 0 );

        scoreCount = 0;
        for( i = 0; ( sym = StrExtract( scoreList, i ) ) != ""; i++ )
        {
            score = StaticVarGet( sym );
            SymName = StrMid( sym, 7 );
            scoreNames = scoreNames + symName + ",";

            if( score > 0 )
            {
                _TRACE( "score " +  symName  + " " + score );
                scoreMatrix[scoreCount][0] = score;
                scoreMatrix[scoreCount][1] = scoreCount;
                scoreCount++;
            }
        }

        scoreMatrix = mxSortRows( scoreMatrix, True, 0 );

        for( row = 0; row < scoreCount; row++ )
        {
            score = scoreMatrix[row][0];
            id = scoreMatrix[row][1];
            symName =  StrExtract( scoreNames, id );
            
            _TRACE( "add " +  symName  + " " + NumToStr( score, 4.0 ) );
            CategoryAddSymbol( symName, categoryWatchlist, 1 );
        }

image

You should display ENTIRE matrix instead of one element to better understand what is happening in your code. Use MxToString http://www.amibroker.com/guide/afl/mxtostring.html

_TRACE( MxToString( scoreMatrix ) );

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

1 Like

@garydown, try setting the matrix dimension to the list length:

scoreMatrix = Matrix( StrCount(scorelist, ",") + 1, 2, 0 );

Otherwise, you are iterating over a number of 0 ids.

As suggested by @Tomasz extended _TRACE usage helps a lot to debug formulas.

Re your doubt about _TRACE: as you can see in the documentation examples, AFL automatically concatenates string and numbers if you start the debug message with a string, also with an empty one like _TRACE("" + Close[0]);

1 Like

Thanks guys,
that has been a great help. So much to learn, but getting there.

Both tips helped me . The tip from beppe is great, I have always used "numtostr" regardless of a leading string. A pain at best.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.