To work with instrument data, you need to download the daily instrument JSON file from the following link:
Upstox Instruments JSON.
For searching a specific stock, you must know the segment
, instrument_type
, and a value for trading_symbol
. Below are two examples of code that can help you filter and search the instrument data:
Exact Trading Symbol Search
The code below filters instruments based on exact matches for segment
, instrument_type
, and trading_symbol
:
import json
import pandas as pd
def filter_instruments_with_pandas(file_path, segment, instrument_type, trading_symbol):
"""
Filters instruments from a JSON file using Pandas and stores the output in a variable.
Parameters:
- file_path (str): Path to the JSON file.
- segment (str): The segment to filter by (e.g., "NSE_EQ", "NSE_FO").
- instrument_type (str): The instrument type to filter by (e.g., ""EQ", "CE", "PE").
- trading_symbol (str): The trading symbol to filter by (e.g., "RELIANCE" , "TCS 3550 CE 26 DEC 24").
Returns:
- list: Filtered JSON data as a list of dictionaries.
"""
try:
# Load the JSON data from the file
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Convert the JSON data to a Pandas DataFrame
df = pd.DataFrame(data)
# Filter the DataFrame
filtered_df = df[
(df['segment'] == segment) &
(df['instrument_type'] == instrument_type) &
(df['trading_symbol'] == trading_symbol)
]
# Convert the filtered DataFrame back to a JSON-compatible list of dictionaries
filtered_json = filtered_df.to_dict(orient='records')
return filtered_json
except Exception as e:
print(f"Error: {e}")
return []
# Example usage
file_path = "/path/to/your/file/NSE.json" # Replace with your actual file path
segment = "NSE_EQ" # Example segment
instrument_type = "EQ" # Example instrument type
trading_symbol = "RELIANCE" # Example trading symbol
# Store filtered data in a variable
filtered_data = filter_instruments_with_pandas(file_path, segment, instrument_type, trading_symbol)
# Print the filtered data
print(json.dumps(filtered_data, indent=4))
SQL-Like āTrading Symbolā Search
If you want to search for a trading_symbol
using a partial match (like a SQL LIKE
query), you can use the following code:
import json
import pandas as pd
def filter_instruments_with_search(file_path, segment, instrument_type, trading_symbol_like):
"""
Filters instruments from a JSON file using Pandas with SQL-like "LIKE" functionality.
Parameters:
- file_path (str): Path to the JSON file.
- segment (str): The segment to filter by (e.g., "NSE_EQ", "NSE_FO").
- instrument_type (str): The instrument type to filter by (e.g., "EQ", "CE", "PE", ).
- trading_symbol_like (str): A pattern to search for in trading symbols (e.g., "REL", "TATA").
Returns:
- list: Filtered JSON data as a list of dictionaries.
"""
try:
# Load the JSON data from the file
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Convert the JSON data to a Pandas DataFrame
df = pd.DataFrame(data)
# Filter the DataFrame with SQL-like conditions
filtered_df = df[
(df['segment'] == segment) &
(df['instrument_type'] == instrument_type) &
(df['trading_symbol'].str.contains(trading_symbol_like, case=False, na=False))
]
# Convert the filtered DataFrame back to a JSON-compatible list of dictionaries
filtered_json = filtered_df.to_dict(orient='records')
return filtered_json
except Exception as e:
print(f"Error: {e}")
return []
# Example usage
file_path = "/path/to/your/file/NSE.json" # Replace with your actual file path
segment = "NSE_EQ" # Example segment
instrument_type = "EQ" # Example instrument type
trading_symbol_like = "REL" # Example trading symbol
# Store filtered data in a variable
filtered_data = filter_instruments_with_search(file_path, segment, instrument_type, trading_symbol_like)
# Print the filtered data
print(json.dumps(filtered_data, indent=4))
Browser Display Consideration
If you plan to display the results in a browser, creating a search interface that dynamically filters data from the instrument file is best. This way, you can handle input dynamically and display results efficiently.