Derivatives package
Submodules
Derivatives.NSE module
- class Derivatives.NSE.NSE[source]
Bases:
NSEBaseA class to interact with NSE (National Stock Exchange) API.
- Attributes:
valid_pcr_fields : list of valid fields for put-call ratio calculation
- Methods:
__init__ : Initialize the NSE class get_option_chain : Get the option chain for a given ticker get_raw_option_chain : Get the raw option chain data for a given ticker get_options_expiry : Get the next expiry date for a given ticker get_all_derivatives_enabled_stocks : Get the list of equities available for derivatives trading get_equity_future_trade_info : Get the trade information of active future contracts for a given ticker get_equity_options_trade_info : Get the trade information of equity options for a given ticker _mapped_index_ticker_for_futures : Get the mapped index ticker for index futures get_index_futures_data : Get the data for index futures of a given index or ticker get_currency_futures : Get the data for currency futures get_commodity_futures : Get the data for commodity futures get_pcr : Get the put-call ratio for a given ticker and expiry date
- get_all_derivatives_enabled_stocks() list[source]
The get_all_derivatives_enabled_stocks provides the list of Equities available for derivative trading
- Parameters:
self – Represent the instance of the class
- Returns:
List of all Equities tickers / symbols for which derivative trading is allowed
- get_commodity_futures() DataFrame[source]
Fetches commodity futures data.
- Parameters:
self – Represent the instance of the class
- Returns:
Pd.DataFrame: DataFrame containing the currency futures data
- get_currency_futures() DataFrame[source]
Fetches currency futures data.
- Parameters:
self – Represent the instance of the class
- Returns:
DataFrame containing the currency futures data
- get_equity_future_trade_info(ticker: str) DataFrame[source]
The get_equity_future_trade_info provides all active future contracts trade information including its price details
- Parameters:
self – Represent the instance of the class
ticker – Specify the ticker / symbol for which we want to get the expiry date
- Returns:
A DataFrame of trade info data of Equity Future contracts
- get_equity_options_trade_info(ticker: str) DataFrame[source]
Gets equity options trade information for a given ticker.
- Parameters:
ticker – Ticker symbol of the equity options trade.
- Returns:
DataFrame containing the trade information.
- get_index_futures_data(index_or_ticker: str) DataFrame[source]
Fetches index futures data.
- Parameters:
self – Represent the instance of the class
index_or_ticker – Name or ticker symbol of the index.
- Returns:
DataFrame containing the FUTURES data
- get_option_chain(ticker: str, expiry: datetime, is_index: bool = True) DataFrame[source]
The get_option_chain function takes a ticker as input and returns the option chain for that ticker. The function uses the try_n_times_get_response function to get a response from NSE’s API, which is then converted into a DataFrame using pd.json_normalize.
- Parameters:
self – Represent the instance of the class
ticker – Specify the stock ticker for which we want to get the option chain its also called symbol in
NSE :param is_index: (optional) Boolean value Specifies the given ticker is an index or not :param expiry: (optional) It takes the expiry date in the datetime format of the options contracts, default is very next expiry day
- Returns:
A dataframe with option chain
- get_options_expiry(ticker: str, is_index: bool = False) datetime[source]
The get_expiry function takes in a ticker and returns the next expiry date for that ticker. The function uses the NSE API to get all expiry dates for a given ticker, sorts them in ascending order, and then returns the nth element of this sorted list.
- Parameters:
self – Represent the instance of the class
ticker – Specify the ticker / symbol for which we want to get the expiry date
is_index – Boolean value Specifies the given ticker is an index or not
- Returns:
The very next expiry date
- get_pcr(ticker: str, is_index: bool = True, on_field: str = 'OI', expiry: datetime | None = None) float[source]
Calculate the put-call ratio (PCR) for a given ticker.
- Parameters:
self – Represent the instance of the class
ticker – The ticker symbol.
is_index – Boolean value Specifies the given ticker is an index or not
expiry – The expiry date of the option contract. Defaults to None.
on_field – The field to calculate PCR on. Volume or oi (open-interest) Default to ‘OI’.
- Returns:
The calculated PCR value
- get_raw_option_chain(ticker: str, expiry: datetime, is_index: bool = True) dict[source]
The get_option_chain function takes a ticker as input and returns the option chain for that ticker. The function uses the try_n_times_get_response function to get a response from NSE’s API, which is then converted into a DataFrame using pd.json_normalize.
- Parameters:
is_index – Boolean value Specifies the given ticker is an index or not
self – Represent the instance of the class
ticker – Specify the stock ticker for which we want to get the option chain
- Returns:
A dataframe with option chain data
Derivatives.Sensibull module
- class Derivatives.Sensibull.Sensibull[source]
Bases:
CustomSessionA class to interact with Sensibull API.
- Attributes:
headers : dictionary containing the headers for the API requests
- Methods:
__init__ : Initializes the Sensibull class instance _get_n_strikes_from_atm : Returns a list of strikes that are evenly spaced around the atm_strike by the strike gap search_token : Returns the token of a given symbol get_token_details : Returns the details of a given token get_options_data_with_greeks : Returns a dataframe with options data and greeks
- get_options_data_with_greeks(ticker_data: dict, num_look_ups_from_atm: int, expiry_date: datetime) tuple[source]
- The get_options_data_with_greeks function takes in the following parameters:
ticker_data - a dictionary containing the instrument token and trading symbol of an underlying stock. num_look_ups_from_atm - number of strikes to look up from at-the-money strike price. expiry date - expiry date for which options data is required.
- Parameters:
self – Bind the method to an object
ticker_data – Get the instrument_token of the underlying
num_look_ups_from_atm – Get the number of strikes from atm strike
expiry_date – Gets the data for that particular expiry date
- Returns:
A dataframe with the following columns:
- get_token_details(token: int) dict[source]
The get_token_details function takes in a token and returns the details of that token. The function first makes a GET request to the URL https://oxide.sensibull.com/v2/compute/cache/underlying_instruments, which returns an array of dictionaries containing information about all tokens on Sensibull’s platform.
- Parameters:
self – Bind the method to an object
token – Get the details of a particular token
- Returns:
The details of the token
- search_token(symbol: str) dict[source]
- The search_token function takes in a symbol and returns the token of that symbol.
It does this by making a GET request to the Sensibull API, which returns an array of dictionaries. The function then searches through each dictionary for one with ‘tradingsymbol’ as key and ‘symbol’ as value, returning its corresponding token.
- Parameters:
self – Represent the instance of the class
symbol – Search for the underlying instrument in the response
- Returns:
The token of the symbol entered