Convert a matrix to an array with blanks inside the matrix causing error 5

I have a matrix that I set using the following statement:

ProfitTradeItems = Matrix ( Max (1, AllTradeCount ), 1, 0 );	

I then filled this Matrix inside the “for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())” loop to get all the profit trades and losing trades inside this matrix.

I then tried to convert his matrix to an array so I can use the AFL StDev ( ) call to calculate the profit variance / deviation, beginning with:

MxGetBlock ( ProfitTradeItems, 0, Max( 1, AllTradeCount ) - 1, 0, 0, asArray = True);

My problem is when the matrix block contains a blank cell in it (i.e., when a symbol has no trade in that symbol, the bo.GetFirstTrade() loop would insert a “0” (zero) into that matrix cell, causing Amibroker to complain with an error: Error 5, '1st' argument of the 'MxGetBlock()' call has incorrect type. The function expected 'a MATRIX' here, but found 'a NUMBER' . Now if I hit the close button in the error dialog box popup, the error would disappear, and the program continues to convert other elements of the matrix into an array, until another zero value is encountered, then I get the error pop up once again, and when I hit the close button, the process would continue and so forth.

Is there a way to fix this issue? I tried using the Nz () function, but that didn't help. Thank you kindly for any suggestions.

A couple of things. First, you don't have to mess around with matrices and arrays to calculate standard deviation. There's a nice example of calculating standard deviation yourself here: Trying to use stdev in a custom metric - I get no output from stdev but others work, compliments of @fxshrat. Among other things, this avoids the problem that would occur if you had more trades than bars in your backtest, i.e. the array wouldn't be large enough to hold all your data.

I don't completely understand your description of the problem, but it sounds like maybe you're trying to move forward with the matrix to array copy when you don't really have any data to process, or you have a single point. Of course you can't calculate standard deviation on one point of data anyway, so maybe you should be detecting this condition and skipping over the calculation of StDev regardless of how you do it.


Thank you, here is the print out of the result in the log window, as we can see here, clearly, item 5 is a number when the variable is in fact a matrix. Yes, I realize I can sum the total and then use the regular standard deviation formula to calculate the value. But I love Amibroker, best trading app out there, so I want to try this matrix / array method. With the statement: _trace(typeof(ProfitTradeItems));


I finally got the StDev() to work in the second pass. It was my fault, I had a coding error: I put the declaration statement:

ProfitTradeItems = Matrix ( Max (1, AllTradeCount ), 1, 0 );

inside the "for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())" loop, when I moved the declaration statement outside the for loop, I don't get the number item to show up. Thank you. btw, you can indeed do everything in Amibroker, great program, I am very pleased.

That's why experienced forum members are always asking people to post their code instead of just describing it. No one else can see the mistakes that you don't share, and therefore it's harder, if not impossible, to offer good solutions.


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