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:
Strategy compiling............ ok
............................
Test: Strategy 2004..2023
Error 041: Too many matrices!
Error 111: Crash in run: GetSmallerValue() at bar 3