# Matrix related problem

Hello, Can anybody help me with the following problem, I got stuck for several hours
Let me explain the problem first----
I am trying to get the Put/Call ratio of option chain of "NIFTY" (Index traded in India), but in a very different manner suppose the closing price of NIFTY in 5min TF at the end of 5 min is 14030, then I will look for 03 strike prices i.e 14000,14050,13950 of CE and PE each and find the P/E ratio by the individual sum of OI of each strike prices of CE and PE respectively and then divide and similarly at the end of every 5 minutes.
I have written the code but it is showing the error "subscript out of range, you attempted to access non-existing %d-th element of an array"

``````SetBarsRequired(sbrAll);
symbol = "NIFTY";
strikeinterval = 50;
expiry = "10MAR22";
exchange = ".NFO";
strikeCE=Matrix(BarCount,3,0);
strikePE=Matrix(BarCount,3,0);
CEopenInt=Matrix(BarCount,3,0);
PEopenInt=Matrix(BarCount,3,0);
count=0;
for(i=0;i<BarCount-1;i++)
{
ltp = C[i];
iPE = ltp - (ltp%strikeinterval);   //nearest strike price
iCE = ltp - (ltp%strikeinterval);

for(j=0;j<=2;j++)
{
strikeCE[i][j] = iCE[i] + strikeinterval * (j-1);
strikePE[i][j] = iPE[i] + strikeinterval * (j-1);
//option symbol formation CE & PE
VarSetText("CE"+count,symbol+expiry+strikeCE[i][j]+"CE"+exchange);
VarSetText("PE"+count,symbol+expiry+strikePE[i][j]+"PE"+exchange);
CEopenInt[i][j] = Foreign(VarGetText("CE"+count),"I");
PEopenInt[i][j]=Foreign(VarGetText("PE"+count),"I");
count=count+1;
}
CEopenInt=MxGetBlock(CEopenInt,i,i,0,2,False);z1=MxSum(CEopenInt);
PEopenInt=MxGetBlock(PEopenInt,i,i,0,2,False);z2=MxSum(PEopenInt);
pcr[i] = SafeDivide(z2,z1);
}
Plot(pcr,"pcr",colorRed,styleHistogram);

``````

@prabhas

I think something is wrong here: you are assigning the values of a matrix row to the same entire matrix (essentially changing the original matrixes dimensions).

The whole loop code is not proper and incorrect AFL programming.
You should delete entire loop and describe goal.

This is simply incorrect coding.
Putting entire array inside loop and assigning to array element.

This is overkill too (besides of changing original matrix size).

@prabhas, there are plenty of solutions on Option OI given by @fxshrat and other seniors in this forum if you search carefully, however, I thought to revert since your requirement doesn't require any matrix or VarSetText/VargetText.

Here is the code for your requirement you can simply define the logic as per your data vendor requirement.

``````NStrikes    = Param( "Number of Strikes +/- ATM", 2, 1, 30, 1 );
StrikeInterval = 50;

ATMStrike = Round( Close / StrikeInterval ) * StrikeInterval;
Symbol = StrReplace( Name(), "-FUT", "WK" ); //Check your database vendor requirement for symbol.

for( i = ( NStrikes * -1 ) ; i <= NStrikes; i++ )
{
OptionStrike = NumToStr( ATMStrike + ( i * StrikeInterval ), 1.0, False );
CallStrike = Symbol + OptionStrike + "CE"; // Check your database vendor requirement for symbol.
PutStrike = Symbol + OptionStrike + "PE"; // Check your database vendor requirement for symbol.
ArrayRow = i + NStrikes;

if( SetForeign( CallStrike ) )
{
CallOI[ArrayRow] = LastValue( OI );
RestorePriceArrays();
}

if( SetForeign( PutStrike ) )
{
PutOI[ArrayRow] = LastValue( OI );
RestorePriceArrays();
}
}

PCR = SafeDivide( Cum( PutOI ), Cum( CallOI ) );

//_TRACE( "PutOI  = " + Cum( PutOI ) );
//_TRACE( "CallOI  = " + Cum( CallOI ) );
//_TRACE( "PCR = " + PCR );
``````
1 Like

Thanks for providing the code, it solves my problem, it broadens my mind in different ways of thinking, very helpful This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.