Gamestudio Links
Zorro Links
Newest Posts
Zorro Trader GPT
by TipmyPip. 02/19/25 03:24
New Zorro version 2.64
by jcl. 02/17/25 15:32
Smaller Windows version
by Nicole. 02/08/25 14:51
Command Help in notepad++
by pr0logic. 02/07/25 18:19
How to export list of current open trades?
by vicknick. 02/07/25 17:22
Initial RithmicZorroPlugin Release.
by kzhao. 02/05/25 03:30
AUM Magazine
Latest Screens
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Zeal-X2
Who's Online Now
3 registered members (VoroneTZ, AndrewAMD, TipmyPip), 457 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
crazyhedgehog, Nicole, Columboss, quantenesis, YanniD
19109 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Too Many Matrix Error when Looping Multiple Times #488527
01/07/25 12:56
01/07/25 12:56
Joined: Apr 2023
Posts: 48
V
vicknick Offline OP
Newbie
vicknick  Offline OP
Newbie
V

Joined: Apr 2023
Posts: 48
I try to loop through multiple times to get the smallest tracking value of a portfolio.

Say I have 5 assets = [2.1, 2.3, 5.4, 0.2, 0.9] in a matrix format. I have a starting matrix [0.0, 0.0, 0.0, 0.0, 0.0]. The goal is to minimize the tracking value of the portfolio. Basically, I want to reduce the difference between "starting matrix" and the original matrix.

In our example, say "starting matrix" is [0.0, 0.0, 0.0, 0.0, 0.0], and "starting tracking value" is 5.3%.

From the starting matrix, I then add 0.1 to the each of the matrix. So I have 5 matrix [0.1, 0.0, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0, 0.0], ..., [0.0, 0.0, 0.0, 0.0, 0.1]. From there, I calculate the tracking value of these 5 matrix with the staring matrix. I finally select the one with the smallest value as our "starting matrix", and the tracking value as our "starting tracking value".

Let's say [0.0, 0.0, 0.0, 0.1, 0.0] gives the smallest value with tracking value 4.6%, I take this as my starting matrix and starting tracking value.

And then process repeats again, until I can no longer get a smaller value. Say in loop 7th our previous value is 1.3%, if our next loop gives value of 1.3% or greater, then the process completes.

However, Zorro seems to give the too many matrix error during the process. I have 30 assets in my script. Not sure if Zorro can't handle repeating process.

Error:
Code
Strategy compiling............ ok
............................
Test: Strategy 2004..2023
Error 041: Too many matrices!
Error 111: Crash in run: GetSmallerValue() at bar 3


Last edited by vicknick; 01/07/25 12:57.
Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488528
01/07/25 14:33
01/07/25 14:33
Joined: Feb 2017
Posts: 1,767
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,767
Chicago
There's most likely a bug in your code:
Quote
Error 041: series size / number / usage
Something was wrong with a series, loop, or matrix call in your script. They are special functions that cannot be arbitrarily called. Series might have been called in wrong order or with different size, or loop might have been called outside the run function. Check all such calls and all indicators or functions that are described to create series, such as Volatility, ATR, LowPass, etc. Any run must have the same series with the same size in the same order. Don't skip series calls with if statements, don't create them in event-triggered functions such as tmf, tick, or tock, and don't chnage the size of a series. The error message tells you at which bar the problem occured first. If you need an extremely large number of series, increase TradesPerBar until the error message disappears.
https://zorro-project.com/manual/en/errors.htm

Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488530
01/08/25 12:43
01/08/25 12:43
Joined: Apr 2023
Posts: 48
V
vicknick Offline OP
Newbie
vicknick  Offline OP
Newbie
V

Joined: Apr 2023
Posts: 48
The code is as such:
Code
int Round = 1;	
var CurrentSD = 100;
var ProposedSD = 100;

do
{
   CurrentSD = ProposedSD;
   printf("#\n///// Round: %i //////", Round);
   ProposedSD = GetNewestSD(CurrentSD);
   Round++;
} while (CurrentSD > ProposedSD);

var GetNewestSD(var currentSD)
{
   var NewSD = currentSD;
   int i;
   for(i = 0; i < N; i++)
   {	
      mat TrackingErrorMatrix = matrix(N,1);
      mat TransposeTrackingErrorMatrix = matTrans(matrix(TrackingErrorMatrix->cols,TrackingErrorMatrix->rows), TrackingErrorMatrix); 
   }

   if (Round == 20) return NewSD;
   else return NewSD/2;
}


Basically, the script will loop through the function 20 times before it stops. However, every time at loop 18th, the same error will occur:

Code
//////// Round: 1 //////////

//////// Round: 2 //////////

...


//////// Round: 17 //////////

//////// Round: 18 //////////
Error 041: Too many matrices!
Error 111: Crash in run: GetNewestSD()


Last edited by vicknick; 01/08/25 12:44.
Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488532
01/09/25 01:26
01/09/25 01:26
Joined: Feb 2017
Posts: 1,767
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,767
Chicago
Your code does not show what is calling these functions. This is critical. run() in fact must be calling matrix() the same exact number of times every time and we have zero information about that. Reread the error code description I supplied for clues. Or post the full code.

Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488533
01/09/25 03:49
01/09/25 03:49
Joined: Apr 2023
Posts: 48
V
vicknick Offline OP
Newbie
vicknick  Offline OP
Newbie
V

Joined: Apr 2023
Posts: 48
The code is similar as such:
Code
var GetNewestSD(var currentSD)
{
   var NewSD = currentSD;
   int i;
   for(i = 0; i < N; i++)
   {	
      mat TrackingErrorMatrix = matrix(N,1);
      mat TransposeTrackingErrorMatrix = matTrans(matrix(TrackingErrorMatrix->cols,TrackingErrorMatrix->rows), TrackingErrorMatrix); 
   }

   if (Round == 20) return NewSD;
   else return NewSD/2;
}

function run()
{
 int Round = 1;	
 var CurrentSD = 100;
 var ProposedSD = 100;

 do
 {
    CurrentSD = ProposedSD;
    printf("#\n///// Round: %i //////", Round);
    ProposedSD = GetNewestSD(CurrentSD);
    Round++;
 } while (CurrentSD > ProposedSD);
}

Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488534
01/09/25 06:01
01/09/25 06:01
Joined: Feb 2017
Posts: 1,767
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,767
Chicago
Well, there’s your problem. You have a different number of matrix() calls per run() call when you must guarantee the same number of matrix() calls every time.

Restructure it so that you guarantee a fixed number of matrix() calls for every run() call.

Re: Too Many Matrix Error when Looping Multiple Times [Re: AndrewAMD] #488535
01/09/25 12:57
01/09/25 12:57
Joined: Apr 2023
Posts: 48
V
vicknick Offline OP
Newbie
vicknick  Offline OP
Newbie
V

Joined: Apr 2023
Posts: 48
I see. But the run() function will call the matrix exactly 20 times though, so there is same number of matrix() call for each run().

Maybe I overlooked something? Or matrix cannot be called in "while" loop?

Re: Too Many Matrix Error when Looping Multiple Times [Re: vicknick] #488536
01/09/25 15:05
01/09/25 15:05
Joined: Feb 2017
Posts: 1,767
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,767
Chicago
How would I know that? You don't show the definition of N, and the number of GetNewestSD() calls depends on the outcome of the do-while loop condition. Plus int Round is local to run() but somehow you're using it in the GetNewestSD() code? Does this even compile?

Consider also what the manual says:
Quote
Matrices are created in the INITRUN and released after the EXITRUN. Just like series, matrix() creation must happen in the same order at any bar, preferably at the begin of the run function.
This probably means the values are retained in memory between run calls. Also note the advice it gives about the location of the matrix() calls.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1