Hi,
I've the problem with implementation Min Variance procedure to Explore or Back Testing. In my function which calculate weigts in portfolio are used matrixes. The matrix covariance (and other) in AFL don't allowe to use array (historical datas), so I have to use function SelectedValue. As a result of this, in Explore or Back Testing mode, only last calculations filled all cells. Any idea, how to solve it ?
symlist = CategoryGetSymbols( categoryMarket, 0 );
function minvar (list, len_cov)
{
matrix_size_r = 1 + StrCount( list, "," );
matrix_1n = Matrix(matrix_size_r,1,0);
matrix_cov = MxIdentity(matrix_size_r);
for( i = 0; ( name1 = StrExtract( list, i ) ) != ""; i++ )
{
c1 = Foreign(name1, "c");
matrix_1n[i][0] = 1;
sdv1 = StDev(ln(c1/Ref(c1,-1)), len_cov);
for( j = 0; ( name2 = StrExtract(list, j ) ) != ""; j++ )
{
c2 = Foreign(name2, "c");
corr = Correlation(ln(c1/Ref(c1,-1)), ln(c2/Ref(c2,-1)), len_cov);
sdv2 = StDev(ln(c2/Ref(c2,-1)), len_cov);
cov = corr * sdv1 * sdv2;
matrix_cov[i][j] = SelectedValue(cov);
}}
A_suma = MxTranspose(matrix_1n)@(MxInverse(matrix_cov) @ matrix_1n);
risk_minvar = sqrt(1 / A_suma[0][0]) * sqrt(365)*100;
weights_minvar = (MxInverse(matrix_cov) @ matrix_1n) / A_suma[0][0];
list_weights = StrReplace(MxToString(weights_minvar),"{ ","");
list_weights = StrReplace(lista_wag," }","");
list_weights = StrReplace(lista_wag,"}","");
list_weights = StrReplace(lista_wag,"{","");
return list_weights;
}
weights = minvar(symlist, 126);
Filter = 1;
AddtextColumn(weights,"weights");