To get the at the money (ATM) strikes of a stock we will use the NSEpython module in python.

Please make sure that you have the nsepython module installed, else:

Type this in the terminal:

  • $pip install nsepython

We will take Reliance as an example in this tutorial

Using oi_chain_builder function we can get the option chain, ltp for Reliance as:

symbol = "RELIANCE"
oi_mode = "compact"
expiry = "latest"
#ltp = (nse_eq(symbol)['priceInfo']['lastPrice'])
oi_data, ltp, crontime = oi_chain_builder(symbol,expiry,oi_mode)

we will get the following data frame as oi_data:

Then in order to get the atm strike we will perform some operations on this dataframe:

oi_data['diff'] = oi_data['Strike Price'].apply(lambda x: abs(x - ltp))
    oi_data = oi_data.sort_values(by='diff', ascending=True)
    oi_data = oi_data.reset_index(drop=True)
    atm_strike = oi_data['Strike Price'][0]
    print("Atm strike for %s is: %s " %(symbol, atm_strike))

Finally, putting it all together in a function:

from nsepython import *

def get_atmstrike(symbol):
    oi_mode = "compact"
    expiry = "latest"
    #ltp = (nse_eq(symbol)['priceInfo']['lastPrice'])
    oi_data, ltp, crontime = oi_chain_builder(symbol,expiry,oi_mode)
    oi_data['diff'] = oi_data['Strike Price'].apply(lambda x: abs(x - ltp))
    oi_data = oi_data.sort_values(by='diff', ascending=True)
    oi_data = oi_data.reset_index(drop=True)
    atm_strike = oi_data['Strike Price'][0]
    print("Atm strike for %s is: %s " %(symbol, atm_strike))
    return atm_strike

v1 = get_atmstrike("RELIANCE")

we get the output as:

If you liked what i post here regularly then kindly subscribe and visit me at https://twitter.com/rahul_12gh

https://medium.com/@mysteryta47

2 Comments

  1. I asked for it and you gave this tutorial. Thank you so much.

    1. rajista's avatar rajista says:

      you’re welcome 🙂

Leave a Reply