Getting the ATM strikes for all the FNO stocks

Hey, I am trying to get the ATM strikes for all the FNO stocks, I’m able to fetch the option chain data using nsepytjon, but some stocks don’t have equidistant strikes.
How should I proceed further?
Thanks

this should get you the ATM strike for any FNO scrip

import requests
symbol = input('Enter Symbol: ').upper()
option_chain_json = requests.get(f'https://www.nseindia.com/api/quote-derivative?symbol={symbol}',
                        headers={'User-Agent': 'Mozilla/5.0'}).json()
ltp = option_chain_json['underlyingValue']
atm_strike = sorted([[round(abs(ltp-i),2),i] for i in option_chain_json['strikePrices']])[0][1]
print(atm_strike)
1 Like

The code is giving some error, regarding particular scripts,

symbol = “M&MFIN”
def get_atm_strike(symbol):
symbol = symbol.upper()
option_chain_json = requests.get(f’https://www.nseindia.com/api/quote-derivative?symbol={symbol}’,
headers={‘User-Agent’: ‘Mozilla/5.0’}).json()
ltp = option_chain_json[‘underlyingValue’]
atm_strike = sorted([[round(abs(ltp-i), 2), i]
for i in option_chain_json[‘strikePrices’]])[0][1]
return atm_strike

the json dict isn’t returning the desired value:

json_dict:
{‘underlyingValue’: ‘-’, ‘vfq’: ‘-’, ‘fut_timestamp’: ‘-’, ‘opt_timestamp’: ‘-’, ‘stocks’: [], ‘strikePrices’: [], ‘expiryDates’: []}

error:
IndexError Traceback (most recent call last)
in
----> 1 atm = get_atm_strike(symbol)

in get_atm_strike(symbol)
7 ltp = option_chain_json[‘underlyingValue’]
8 atm_strike = sorted([[round(abs(ltp-i), 2), i]
----> 9 for i in option_chain_json[‘strikePrices’]])[0][1]
10 return atm_strike

IndexError: list index out of range

I fixed it by fetching data using nsepython, and using the same algo as this.
will post the working function

1 Like

from nsepython import *
def get_atm_strike(symbol):
symbol = symbol.upper()
option_chain_json = nse_optionchain_scrapper(symbol)
data = option_chain_json[‘filtered’][‘data’]
ltp = data[0][‘PE’][‘underlyingValue’]
strike_price_list = [x[‘strikePrice’] for x in data]
atm_strike = sorted([[round(abs(ltp-i),2),i] for i in strike_price_list])[0][1]
return atm_strike

def get_pe_ce_price(symbol,atm_strike):
symbol = symbol.upper()
option_chain = nse_optionchain_scrapper(symbol)
for dictt in option_chain[‘filtered’][‘data’]:
if dictt[‘strikePrice’] ==atm_strike:
pe_price = dictt[‘PE’][‘askPrice’]
ce_price = dictt[‘CE’][‘askPrice’]
return pe_price,ce_price

1 Like

looks good, you can also you “%26” instead of “&” to get required strikes

1 Like

nse_optionchain_scrapper has already have that condition inbuilt.

max([x for x in strikes_list if x < ltp])
This method works for all stocks. Just supply list of all strike prices of that stock(strikes_list) and latest traded price(ltp)

1 Like
def get_atm_strike(symbol): #Thanks to Ronit_Hain and Ankit Jain
    payload = nse_optionchain_scrapper(symbol.upper())
    ltp = float(payload['records']['underlyingValue'])
    strike_price_list = [x['strikePrice'] for x in payload['records']['data']]
    atm_strike = sorted([[round(abs(ltp-i),2),i] for i in strike_price_list])[0][1]
    return atm_strike

Added this.