Python code to read data (scrapping strike price & expiry date) from NSE option chain

import requests
from bs4 import BeautifulSoup

# Get all get possible expiry date details for the given script
def get_expiry_from_option_chain (symbol):

    # Base url page for the symbole with default expiry date
    Base_url = "" + symbol + "&date=-"

    # Load the page and sent to HTML parse
    page = requests.get(Base_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    # Locate where expiry date details are available
    locate_expiry_point = soup.find(id="date")
    # Convert as rows based on tag option
    expiry_rows = locate_expiry_point.find_all('option')

    index = 0
    expiry_list = []
    for each_row in expiry_rows:
        # skip first row as it does not have value
        if index <= 0:
            index = index + 1
        index = index + 1
        # Remove HTML tag and save to list
        expiry_list.append(BeautifulSoup(str(each_row), 'html.parser').get_text())

    # print(expiry_list)
    return expiry_list # return list

def get_strike_price_from_option_chain(symbol, expdate):

    Base_url = "" + symbol + "&date=" + expdate

    page = requests.get(Base_url)
    soup = BeautifulSoup(page.content, 'html.parser')

    table_cls_2 = soup.find(id="octable")
    req_row = table_cls_2.find_all('tr')

    strike_price_list = []

    for row_number, tr_nos in enumerate(req_row):

        # This ensures that we use only the rows with values
        if row_number <= 1 or row_number == len(req_row) - 1:

        td_columns = tr_nos.find_all('td')
        strike_price = int(float(BeautifulSoup(str(td_columns[11]), 'html.parser').get_text()))

    # print (strike_price_list)
    return strike_price_list

Jegathesan Durairaj, a mathematician by qualification, an Ex-software professional and now a full time Risk Defined Option Writer. With his Open Book Strategies, he is the most known identity in twitter as itjegan, with huge followers. He had won Zerodha 60 Day Challenge – 10 Times round-the-clock. He is one of the prime Option Writing Trainer and tutored close to 1000 members till now.

He is the Mastermind of CapitalZone РActive Option Trading where Traders can avail Trading Chart with customised indicators, FNO tools and Payoff analysis. His keynote is to provide whole FNO deets to be handy for Traders in a sole place.

Now he is taking more heed on hedging portfolio in Option Writing where we might subject to more market risk. He is streamlining his strategies often and improving perpetually. At this time unfolding ways to measure good-quality Trend Following System.

Conducting workshop in all Metro Cities (Delhi, Mumbai, Bangalore, Chennai and Coimbatore) often based on queries from Traders.



6 comments found

Comments are closed.