# Help in creating a matrix with one row per ticker

I suspect I’m asking a simple question, but would appreciate some assistance. I’m trying to create a matrix with rows equal to the number of tickers in a watchlist, retaining a selectedvalue of an indicator in the matrix.
I can create a matrix for a single ticker with barcount number of rows or columns with code similar to “code 1”.
Also, I can create a matrix with a watchlist count of rows with “code 2”, but am unable to accumulate the desired value in the matrix.

Any help would be greatly appreciated.

``````//Code 1
function F1Value(level)
{
X1=MA(C,10);
return x1;
}
function F2Value(level)
{
X2=MA(C,50);
return x2;
}

function getvalues( level )
{

F1num=F1value(C);
F2num=F2value(C);

mat = Matrix(BarCount, 2);
mat = MxSetBlock(mat, 0, BarCount-1, 0,0, F1num);
mat = MxSetBlock(mat, 0, BarCount-1, 1,1, F2num);
return mat;
}

mat = getvalues( C );

MxToString(Mat);

``````

//code 2
wlnumber = GetOption( "FilterIncludeWatchlist" ); // choose WL in Analysis window
symlist = CategoryGetSymbols( categoryWatchlist, wlnumber );
SYMCOUNT=0;

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
num = StrCount( symlist, "," ) + 1 ;
if ( Status( "stocknum" ) == 0 )
{
StaticVarRemove("*");
}

StaticVarSet("symcount", num);

}

//**************************************************************************************

function F1Value(level)
{
X1=MA(C,10);
return x1;
}
function F2Value(level)
{
X2=MA(C,50);
return x2;
}

//**************************************************************************************

//Create matrix

SYMCOUNT=StaticVarGet("SYMCOUNT",num);
MXCOORD=Matrix(SYMCOUNT,4,Null);

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign(sym);
F1num=F1value(C);
F2num=F2value(C);

``````MxSetBlock(MXCOORD, 0, 0, 1,1, f1num);
MxSetBlock(MXCOORD, 0, 0, 2,2, f2num);
MxSetBlock(MXCOORD, 0, 0, 3,3, i);

RestorePriceArrays();

StaticVarSetText("name " + NumToStr(i,1.0),sym);

``````

}

MxToString( MXCOORD);
Filter=1;

``````type or paste code here
``````

Sorry - I didnt format the second code correctly

``````//code 2
wlnumber = GetOption( "FilterIncludeWatchlist" ); // choose WL in Analysis window
symlist = CategoryGetSymbols( categoryWatchlist, wlnumber );
SYMCOUNT=0;

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
num = StrCount( symlist, "," ) + 1 ;
if ( Status( "stocknum" ) == 0 )
{
StaticVarRemove("*");
}

StaticVarSet("symcount", num);

}

//**************************************************************************************

function F1Value(level)
{
X1=MA(C,10);
return x1;
}
function F2Value(level)
{
X2=MA(C,50);
return x2;
}

//**************************************************************************************

//Create matrix

SYMCOUNT=StaticVarGet("SYMCOUNT",num);
MXCOORD=Matrix(SYMCOUNT,4,Null);  // Rows = number of symbols, col= 3

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign(sym);
F1num=F1value(C);
F2num=F2value(C);

MxSetBlock(MXCOORD, 0, 0, 1,1, f1num);
MxSetBlock(MXCOORD, 0, 0, 2,2, f2num);
MxSetBlock(MXCOORD, 0, 0, 3,3, i);

RestorePriceArrays();

StaticVarSetText("name " + NumToStr(i,1.0),sym);

}

MxToString( MXCOORD);
Filter=1;

``````

You do not need first loop as it makes no sense.
And since you store three different things per symbol your rownum needs to be 3*Symcount (so indexing in MxSetBlock needs to take care of that too).
And since you intend to store arrays you need column size being BarCount.
Also you need to assign MxSetBlock to variable of initial matrix.

``````/// original code at
/// fix by fxshrat@gmail.com
//code 2
wlnumber = GetOption( "FilterIncludeWatchlist" ); // choose WL in Analysis window
symlist = CategoryGetSymbols( categoryWatchlist, wlnumber );

//**************************************************************************************
function F1Value(level)
{
X1=MA(C,10);
return x1;
}
function F2Value(level)
{
X2=MA(C,50);
return x2;
}
//**************************************************************************************

//Create matrix
SYMCOUNT = StrCount( symlist, "," ) + 1;
MXCOORD = Matrix(3*SYMCOUNT, Barcount, Null);

rownum = MxGetSize(MXCOORD, 0);
colnum = MxGetSize(MXCOORD, 1);

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign(sym);
F1num=F1value(C);
F2num=F2value(C);

MXCOORD = MxSetBlock(MXCOORD, 3*i, 3*i, 0, colnum-1, f1num);
MXCOORD = MxSetBlock(MXCOORD, 3*i+1, 3*i+1, 0, colnum-1, f2num);
MXCOORD = MxSetBlock(MXCOORD, 3*i+2, 3*i+2, 0, colnum-1, i);
RestorePriceArrays();

StaticVarSetText("name " + NumToStr(i,1.0),sym);

}

MxToString( MXCOORD);
Filter=1;
``````

Sample matrix output via explorer (explorer code not included in upper code, it is just to show that it works):

4 Likes

Thanks!. I appreciate the help.