Seeking Help with Instrument Keys for Specific Options in Upstox API

Hello everyone,

I’m currently working with the Upstox API and I’ve hit a bit of a roadblock. I’ve been trying to understand the logic behind the creation of instrument keys, specifically for options trading. I’ve gone through the documentation available at Upstox API - Instruments, but I’m still unclear about a few things.

My main issue is that I can’t seem to find the instrument key for a specific option, namely the “20000PE Nifty 30 Dec”. I’ve looked through the available resources but haven’t had any luck.

Does anyone know if there’s a way to programmatically retrieve or construct the instrument key for such specific options? Any guidance, tips, or code snippets would be greatly appreciated. I’m sure others might also benefit from this information.

Thanks in advance for your help!

Best regards,
Charly

Let’s consider you know the date, strike price, and whether it’s a put or call option for the contract NIFTY23DEC23000PE. To programmatically find the instrument_key (such as NSE_FO|37721) for a given trading symbol like NIFTY23DEC23000PE, when you have the date, strike price, and whether it’s a put or call option, you can follow these steps:

  1. Standardize the Date Format: Ensure the date is in a standard format that matches the format used in your dataset. For example, if your date is 23-Dec-2023, you may need to convert it to a format like 23DEC.
  2. Concatenate Strings for Trading Symbol: Construct the trading symbol by concatenating the index name, standardized expiry date, strike price, and option type (PE for Put, CE for Call).
  3. Filter the Dataset: Use the constructed trading symbol to filter your dataset and extract the instrument_key.

Here’s a Python function that demonstrates this:

python

import pandas as pd

def find_instrument_key(df, index, date, strike_price, option_type):
    """
    Finds the instrument key in the dataframe for a given option.

    :param df: DataFrame containing the data.
    :param index: The index, e.g., 'NIFTY'.
    :param date: Expiry date in the format 'DD-MMM-YYYY' (e.g., '23-Dec-2023').
    :param strike_price: Strike price as an integer or float.
    :param option_type: 'Put' or 'Call'.
    :return: The instrument key or None if not found.
    """
    # Convert date to the required format (e.g., '23DEC')
    date_str = pd.to_datetime(date).strftime('%d%b').upper()

    # Define option type abbreviation
    option_abbr = 'PE' if option_type.lower() == 'put' else 'CE'

    # Construct the trading symbol
    trading_symbol = f"{index}{date_str}{strike_price}{option_abbr}"

    # Filter the dataframe for the trading symbol
    filtered_df = df[df['tradingsymbol'] == trading_symbol]

    # Return the instrument key
    if not filtered_df.empty:
        return filtered_df.iloc[0]['instrument_key']
    else:
        return None

# Example usage
data = pd.read_csv('/path/to/NSE.csv',  low_memory=False)  # Load the dataset
instrument_key = find_instrument_key(data, 'NIFTY', '23-Dec-2023', 23000, 'Put')
instrument_key

This function will find the instrument_key for the trading symbol constructed from the given date, strike price, and option type. If the trading symbol does not exist in the dataset, it will return None. Make sure to adjust the date format conversion (strftime('%d%b')) to match the format used in your dataset.

Tested it randomly for 3-4 entries and worked for me. You can give a shot and let me know.

Hi Pradeep_Jaiswar,

Thank you for sharing the Python function to find the instrument_key for options contracts. I appreciate your effort in crafting this solution. However, I’ve encountered some issues when applying it to certain use cases.

Specifically, the function doesn’t seem to work for options with expiry dates within this week or for those expiring next year. For instance, when I tried to find the instrument_key for NIFTY30NOV23000PE, the function did not return the expected results. Similarly, I faced an issue with an option expiring in March next year.

Thanks,
Charly

I tried something like this

import pandas as pd
from datetime import datetime, timedelta

def get_thursday_date():
    today = datetime.today()
    days_until_thursday = (3 - today.weekday() + 7) % 7
    thurdays_date = today + timedelta(days=days_until_thursday)
    format_date = thurdays_date.strftime('%y%m%d')
    if format_date[2] == '0':
        format_date = format_date[0:2]+format_date[3:]
    return format_date

def find_instrument_key(df, index, date, strike_price, option_type):
    """
    Finds the instrument key in the dataframe for a given option.

    :param df: DataFrame containing the data.
    :param index: The index, e.g., 'NIFTY'.
    :param date: Expiry date in the format 'DD-MMM-YYYY' (e.g., '23-Dec-2023').
    :param strike_price: Strike price as an integer or float.
    :param option_type: 'Put' or 'Call'.
    :return: The instrument key or None if not found.
    """
    # Convert date to the required format (e.g., '23DEC')
    date_str = pd.to_datetime(date).strftime('%y%b').upper()
    date_str_1 = get_thursday_date()

    # Define option type abbreviation
    option_abbr = 'PE' if option_type.lower() == 'put' else 'CE'

    # Construct the trading symbol
    trading_symbol = f"{index}{date_str}{strike_price}{option_abbr}"
    trading_symbol_1 = f"{index}{date_str_1}{strike_price}{option_abbr}"
    #print(trading_symbol,trading_symbol_1)
    # Filter the dataframe for the trading symbol
    filtered_df = df[df['tradingsymbol'] == trading_symbol]
    filtered_df_1 = df[df['tradingsymbol'] == trading_symbol_1]

    # Return the instrument key
    if not filtered_df.empty:
        return filtered_df.iloc[0]['instrument_key']
    elif not filtered_df_1.empty:
        return filtered_df_1.iloc[0]['instrument_key']
    else:
        return None

# Example usage
# get_thursday_date()
data = pd.read_csv('\NSE.csv',  low_memory=False)  # Load the dataset
instrument_key = find_instrument_key(data, 'NIFTY', '29-Feb-2024', 21500, 'CALL')
print(instrument_key)

How to i get the prevours 6 months instrument key

@Dev_Jalla The Upstox API does not offer access to the keys for historical instruments. However, you can find keys for all currently active instruments in the BOD (Beginning of Day) file, available at Instruments | Upstox Developer API.

Could you share more details about why you need the instrument keys from the past six months?

I also need to get instruments keys for past periods for back testing strategy. At least for past one month.