How to auto pick weekly options based on strike price

Hello,

I am new to options trading on indexes. Need some help with code references
I want to get the BANKNIFTY option pair for CE & PE based on premium price i specify.

Say if set my premium price target as 200, i want to get BANKNIFY-I option chain pair CE/PE that are closest to 200 price mark.

Also how do i code this to backtest ? I want to backtest 6 months data that trades on weekly options.

Please help, thanks in advance

-Pradeep

1 Like

Strike price or premium price?

premium price

in stockmock and algo test this feature is available. i implemented similar feature in my own algo strategy as well. it’s simple get prices x strikes ce and pe above and below atm. do sum of there combinations and check if they are within say 90%-110% of req premium and ratio of ce/pe should also be in similar range, if yes then thats the required strike

thanks Dinesh for reply, can you please share the code snippet of this that you got working?

Tried it with While loop, but count get it working, sure it might not be right way

	offset = 0 ;
	while( offset < 4 ) {
		optionCEtype = WriteIf(offsetCE == 0, "ATM CE", WriteIf(offsetCE<0,"ITM"+abs(offsetCE)+" CE","OTM"+abs(offsetCE)+" CE"));
		strikeCE = strike + (offsetCE * iInterval);
		symCE    = spot+"_"+strikeCE+"CE-"+expiry;

		SetForeign(SymbolCE);
			CEclose = Close;
		RestorePriceArrays();
		printf("\nTrading CE Symbol :"+symCE+" : "+CEclose);

		for( bar = 0; bar < BarCount; bar++ ) {
			if (CEclose[bar] > 190 AND CEClose[bar] < 210) { break; } 
		}
		offset = offset + 1;
	}

ltpCEl = self.getLtp(ceInstrumentToken,[self.EXCHANGE_NFO]*len(ceInstrumentToken))
ltpPEl = self.getLtp(peInstrumentToken,[self.EXCHANGE_NFO]*len(peInstrumentToken))

    print(ltpCEl)
    print(ltpPEl)

    PEIndex = 0
    CEIndex = 0

    for ltpPE in ltpPEl:

        if ltpPE < premiumCombined:
            for ltpCE in ltpCEl:

                if (ltpCE + ltpPE) / premiumCombined >= 0.85 and (ltpCE + ltpPE) / premiumCombined <= 1.25:

                    if (ltpCE / ltpPE) <= 1.1 and (ltpCE / ltpPE) >= 0.975:
                        isFound = True
                        CEIndex = ltpCEl.index(ltpCE)
                        PEIndex = ltpPEl.index(ltpPE)
                        break

Thanks Dinesh for sharing code snipped, will use this as reference for my AFL.

Was able to get the data with below code, only issue i am seeing is when there is missing data from my data feed, its returning {EMPTY} in that case (referenced code from marketcalls.in and modified as per my need with your inputs

	strike   = IIf(spotC % iInterval > iInterval/2, 		// Calculate reminder value
					spotC - (spotC%iInterval) + iInterval, 	// Reminder greater than 50
						spotC - (spotC%iInterval));			// Reminder lesser  than 50

	offset_lower_limit  = -10;
	offset_higher_limit =  10;
	found_ce_within_limit = False;
	found_pe_within_limit = False;
	
	for ( offset=offset_lower_limit; offset < offset_higher_limit; offset++ ) {
		optionCEtype = WriteIf(offset == 0, "ATM CE", WriteIf(offset<0,"ITM"+abs(offset)+" CE","OTM"+abs(offset)+" CE"));
		strikeCE = strike + (offset * iInterval);
		
		symCE    = spot+"_"+strikeCE+"CE-"+expiry;//Derives the Symbol Format from the Strike Price - BEST-RT format
		SymbolCE = symCE;
		if (SymbolCE != "") {
			i = SelectedValue( BarIndex() );
			CEclose = Foreign(SymbolCE, "Close");
			if (CEclose[i] > 180 AND CEclose[i] < 220) {
				found_ce_within_limit = True;
				CEclose[i] = CEclose[i]; break;
			}
		}
	}
	for ( offset=offset_lower_limit; offset < offset_higher_limit; offset++ ) {
		optionPEtype = WriteIf(offset == 0, "ATM PE", WriteIf(offset<0,"ITM"+abs(offset)+" PE","OTM"+abs(offset)+" PE"));
		strikePE = strike - (offset * iInterval);
		symPE    = spot+"_"+strikePE+"PE-"+expiry;//Derives the Symbol Format from the Strike Price - BEST-RT format
		SymbolPE = symPE;

		i = SelectedValue( BarIndex() );
		PEclose = Foreign(SymbolPE, "Close");
		if (PEclose[i] > 180 AND PEclose[i] < 220) {
			found_pe_within_limit = True;
			PEclose[i] = PEclose[i]; break;
		}
	}