Hi,

I trade using a lot of legs and wanted to calculate the margin required upfront. So I made a tool using selenium and if anyone wants to use the same, please utilize the python code I've posted here. 

Things to note: 

  1. Edit the "Insert the loaction of your chrome driver" as per the location of the chrome driver on your computer. The chrome driver can be downloaded from https://chromedriver.chromium.org/downloads
  2. Make an excel sheet with the name "margin_calc_FnO_Fyers.xlsx"
Sample of how the excel sheet should look
Sample of how the excel sheet should look
import pandas as pd
from selenium import webdriver
import time


def margin_calc():
    driver = webdriver.Chrome("Insert the loaction of your chrome driver")
    driver.get("https://fyers.in/margin-calculator/span-calculator/")
    time.sleep(5)

    # select exchange option of NFO
    exchange = driver.find_element_by_id('exchange')
    exchange.send_keys("NFO")
    time.sleep(1)

    df = pd.read_excel("margin_calc_FnO_Fyers.xlsx")

    for i in range(len(df)):

        if df['FO'][i] == "OPT":
            # select product option of option
            driver.find_elements_by_css_selector("input[name='product'][value='Options']")[0].click()
            time.sleep(1)

            # select the expiry of option
            expiry = driver.find_element_by_id('expiry')
            expiry.send_keys(df['Expiry'][i])

            # select the symbol
            symbol = driver.find_element_by_id('symbol')
            symbol.send_keys(df['Symbol'][i])
            time.sleep(1)

            # select type of option
            if df['OPT_type'][i] == "CE":
                a = "input[name='optionType'][value='Call']"
            else:
                a = "input[name='optionType'][value='Put']"
            driver.find_elements_by_css_selector(a)[0].click()
            time.sleep(1)

            # select strike of option
            strike = driver.find_element_by_id('strikePrice')
            strike.send_keys(int(df['Strike'][i]))
            time.sleep(1)

        else:
            # select product option of option
            driver.find_elements_by_css_selector("input[name='product'][value='Futures']")[0].click()
            time.sleep(1)

            # select the symbol
            symbol = driver.find_element_by_id('symbol')
            symbol.send_keys(df['Symbol'][i])

            # select the expiry
            expiry = driver.find_element_by_id('expiry')
            expiry.send_keys(df['Expiry'][i])

        # select buy or sell
        # it is decided by the qty in our excel file
        if df['Qty'][i] > 0:
            a = "input[name='side'][value='Buy']"
        else:
            a = "input[name='side'][value='Sell']"
        driver.find_elements_by_css_selector(a)[0].click()

        # enter the net qty
        netqty = driver.find_element_by_id('net_quantity')
        netqty.clear()
        netqty.send_keys(abs(int(df['Qty'][i])))

        driver.find_element_by_xpath(
            "/html/body/div[1]/div/div[4]/div[1]/div[3]/div/div/div[1]/div[2]/div[3]/div[3]/a[1]/span").click()
        time.sleep(2)

    margin = driver.find_element_by_id("totalAmtRequired")
    print("Total margin required is:".format(margin.text))
    driver.quit()


if __name__ == '__main__':
    margin_calc()