This script interacts with the National Stock Exchange of India (NSE) API to retrieve options chain data for specific instruments, filter data based on parameters like call/put options, and calculate margin requirements and premium earnings based on strike prices. The script includes two primary functions: one for fetching and processing options data and another for calculating financial metrics related to options trading.
Ensure that the required libraries are installed:
pip install requests pandasrequests- For handling HTTP requests to the NSE API.pandas- For structuring and analyzing the retrieved datatime- For managing delay to avoid being blocked by the NSE server.
Function: Retrieves options chain data for a specified instrument (e.g., "NIFTY") and option side (either "CE" for call or "PE" for put options) from the NSE API, processes it, and returns a DataFrame containing options information.
-
Assumptions:
- The NSE API is accessible, and appropriate headers are set to avoid being blocked.
- The instrument name is a valid NSE symbol, and the side parameter is either "CE" or "PE".
-
How It Works:
- The function starts a
requestssession with headers to simulate a legitimate browser request. - A preliminary GET request to
https://www.nseindia.comestablishes session cookies. - The function then queries the NSE options chain API for the specified instrument name.
- After receiving the JSON response, it iterates over records in
data['records']['data'], filtering based on the provided option side (CEorPE). - Extracts fields like
strike_priceand the corresponding bid or ask price (based onside). - Finally, the function returns a
pandasDataFrame, filtered to include only the highest bid/ask price for the specified side.
- The function starts a
Example API Response:
{
"records": {
"data": [
{
"CE": {
"strikePrice": 17000,
"bidPrice": 182.35,
"askPrice": 183.5,
"lastPrice": 182.75
},
"PE": {
"strikePrice": 17000,
"bidPrice": 192.0,
"askPrice": 193.0,
"lastPrice": 191.5
}
},
{
"CE": {
"strikePrice": 17500,
"bidPrice": 150.1,
"askPrice": 150.75,
"lastPrice": 150.25
},
"PE": {
"strikePrice": 17500,
"bidPrice": 195.5,
"askPrice": 196.0,
"lastPrice": 195.25
}
}
]
}
}Data Processing Example:
For side="CE", the function filters out CE (Call Options) records, extracts the strike_price and askPrice, and identifies the highest askPrice across the DataFrame.
Output DataFrame:
| instrument_name | strike_price | side | bid/ask |
|---|---|---|---|
| NIFTY | 17000 | CE | 183.5 |
Function: Calculates margin requirements and premium earnings for each option based on the retrieved data.
-
Assumptions:
- The data parameter is a valid
DataFramecontainingstrike_priceandbid/askcolumns. - The
lot_sizerepresents the number of contracts per lot (default is 1).
- The data parameter is a valid
-
How It Works:
- For each option record, the function calculates a
margin_requiredas 20% of the strike price (simulated margin rate). - Calculates
premium_earnedby multiplying the bid/ask price by thelot_size. - Appends these calculations to the
DataFrameand returns it.
- For each option record, the function calculates a
Example Calculation:
Given a strike_price of 17000, bid/ask of 183.5, and lot_size of 75:
margin_required= 17000 * 0.20 = 3400premium_earned= 183.5 * 75 = 13762.5
Output DataFrame:
| instrument_name | strike_price | side | bid/ask | margin_required | premium_earned |
|---|---|---|---|---|---|
| NIFTY | 17000 | CE | 183.5 | 3400 | 13762.5 |
# Fetch the option chain data for NIFTY call options (CE)
df = get_option_chain_data_nse_json("NIFTY", "CE")
# Calculate margin and premium for the options data with a specified lot size
df_with_calculations = calculate_margin_and_premium(df, lot_size=75)
print(df_with_calculations)- API Rate Limiting: NSE may block requests if rate limits are exceeded. Implementing delays and avoiding frequent requests is recommended.
- Data Validity: Ensure the retrieved data structure matches expectations; NSE API may change structure over time.
- Credentials: NSE's API does not require credentials but may limit or block access for high-volume requests.
- Used ChatGPT for refining the code and detecting errors
- Used ChatGPT for learning about the working of NSE api
- Used ChatGPT for refining my readme document