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?