Lots of loops for no reason, was: Getting Next Expiry

I want to access options data with the most recent expiry. To access options data i would need strike,expiry date, option type.

Getting current next expiry is where the trouble is.
It is pretty simple to display this data in interpretation window, i am doing it like this

bnfOptions = "05/01/2023,12/01/2023,19/01/2023,25/01/2023";
tosec = 60*60*24;


//curr week
countstr = StrCount( bnfOptions, ",") +1;
printf("countstr %g \n",countstr);
nextexp = "";
for(i=0; i<countstr; i++){
	a = DateTimeDiff( _DT(Date()), _DT(StrExtract( bnfOptions, i )) ) > 0;
	b = DateTimeDiff( _DT(StrExtract( bnfOptions, i+1 )), _DT(Date()) ) > 0;
	
	if(a == 1 AND b == 1){
		nextexp = (StrExtract( bnfOptions, i+1 ) );
		break;
	}
}
daysToNextExpiry = floor(DateTimeDiff(_DT(nextexp), _DT(Date()))/tosec);
printf("nextexp %s \n",nextexp);

To access this data in backtest as well, i created above into an array like this

bnfOptions = "05/01/2023,12/01/2023,19/01/2023,25/01/2023";
tosec = 60*60*24;

//curr week
countstr = StrCount( bnfOptions, ",") +1;
printf("countstr %g \n",countstr);
nextexp = "";
for(j=1;j<BarCount-1;j++){
	for(i=0; i<countstr; i++){
		a[j] = DateTimeDiff( _DT(Date()), _DT(StrExtract( bnfOptions, i )) ) > 0;
		b[j] = DateTimeDiff( _DT(StrExtract( bnfOptions, i+1 )), _DT(Date()) ) > 0;
		
		if(a[j] == 1 AND b[j] == 1){
			nextexp = (StrExtract( bnfOptions, i+1 ) );
			break;
		}
	}
}
daysToNextExpiry = floor(DateTimeDiff(_DT(nextexp), _DT(Date()))/tosec);

expDate = StrLeft(nextexp, 2);
expMonth = StrMid(nextexp, 3, 2);
expyear = Strright(nextexp, 2);
expMonthStr = "";
myexp = 0;

printf("datetime() %g \n", (DateNum()));

for(i=2;i<=BarCount-1;i++){
	myexp[i] = StrToNum(expDate+expMonth+expyear);
}
printf("myexp %g \n", myexp);

It is exhaustive to process, taking more than 20 mins to run.
Please guide me. Is the process right?
Or is there a better way?

Your code has lots of inefficient coding.
First thing to do is to move LOOP INVARIANT code outside of the loop.
For example you are calling date() many times inside the loop even if it doesn't depend on loop. Don't do that.

Second thing is do you really need to execute your outer loop every bar?
It is pointless. Options expiry dates don't change on every minute. Yet you keep calculating them like crazy every bar.

Third thing is that after doing all those loops you are DISCARDING results ( you are NOT using a[] and b[] arrays at all after the loop). So you are doing lots of calculations for no reason as you don't use those results.
What your code is doing is actually performing lots of calculations but at the end of the day using only LAST one.

You also don't describe GOAL, as explained here: How to ask a good question

1 Like

I couldn't find a way to do it. Getting current expiry while trading an index for backtesting (in interpretation window all is okay but in backtest I am getting last date from the list).

I worked it around using options, Backtesting on atm and gettinf expiry date from options.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.