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
Screenshot (115)

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.

my mobile number is 9938412300
i need the atm strikes and their ltp for all the fno stocks in one go.
can anyone hep me with that
plz call me
i am ready to pay

1 Like

“i need the atm strikes and their ltp for all the fno stocks”

Transfer 1K to 9658986525 via Paytm, and I’ll promptly provide the code in this very space.

Note: I’m resolute about not making a call, even with an offer of 100,000 rupees. Additionally, my stipulation is that I’ll disclose the code publicly, right here. :innocent:

There is another quick way @Adithya_K

def atm_strike(n,m):
    a = (n // m) * m
    b = a + m
    return int(b if n - a > b - n else a)

atm_strike = atm_strike(bnltp,100)

Now You need to have this differentiator 100 , which is easy to get. It reduces algo time hugely.