Base package
Submodules
Base.CustomRequest module
- class Base.CustomRequest.CustomSession(headers: dict | None = None)[source]
Bases:
objectA custom class for creating a session object with retries, timeouts, and headers.
- Attributes:
session : session object for making HTTP requests headers: headers required for getting data from a website via API
- Methods:
- __init__(self, headers: dict = None) -> None:
Initializes the CustomSession object with the given headers.
- get_session(self) -> Session:
Returns the session object.
- hit_and_get_data(self, url: str, params: dict = None) -> dict:
Hits the API and gets the data based on the endpoint and parameters passed.
- Args:
headers : (optional) headers required for getting data from a website via API
- Returns:
dict : JSON parsed result of the output response data from the API
- Raises:
JSONDecodeError : If there is an error in decoding the JSON response Exception: If there is an error in connecting to the URL
- get_session() Session[source]
This functions returns the session object which is built when a class object being constructed.
- Parameters:
self – Represent the instance of the class
- Returns:
Session object of the class
- hit_and_get_data(url: str, params: dict | None = None) dict[source]
Hitting the api and gets the data based on the endpoint passed as well as the url params / params for the get type of requests, all api used in this library are of get type so its supports only GET type requests
- Parameters:
self – Represent the instance of the class.
url – Endpoint of the api; aka link of the api
params – (optional) parameters which is required to get exact data from the api aka url params
- Returns:
Dict object which is json parsed result of the output response data got from hitting above request
Base.NSEBase module
- class Base.NSEBase.NSEBase[source]
Bases:
CustomSessionA class to interact with the NSE (National Stock Exchange) API.
- Attributes:
_base_url: base URL for the NSE API
- Methods:
__init__(): Initializes the class and sets up the session and headers for all subsequent requests. get_market_status_and_current_val(index: str = ‘NIFTY 50’) -> tuple: Returns the market status and current value of a given index. get_last_traded_date() -> datetime.date: Returns the last traded date of NIFTY 50 index. get_second_wise_data(ticker_or_index: str = “NIFTY 50”, is_index: bool = True, underlying_symbol: str = None) -> pd.DataFrame: Returns a dataframe with second wise data for a given index or stock. get_ohlc_data(ticker_or_idx: str = “NIFTY 50”, timeframe: str = ‘5Min’, is_index: bool = True, underlying_symbol: str = None) -> pd.DataFrame: Returns the OHLC data for a given ticker or index. search(search_text: str) -> dict: Searches for data related to an equity, derivative, or any type of asset traded on NSE. get_nse_turnover() -> pd.DataFrame: Provides the entire turnover happened in NSE exchange for the day or previous trading session as DataFrame. get_nse_equity_meta_info(ticker: str) -> dict: Returns the equity meta information for a given ticker. get_ohlc_from_charting(ticker: str, timeframe: str, start_date: datetime, end_date: datetime) -> pd.DataFrame: Returns a DataFrame containing the OHLC data for a given ticker and timeframe from new Charting Website of NSE (https://charting.nseindia.com). get_charting_mappings() -> pd.DataFrame: Returns a DataFrame containing the mappings for charting for all Equity and F&O instruments from the new Charting Website of NSE (https://charting.nseindia.com).
- get_charting_mappings() DataFrame[source]
The get_charting_mappings function returns a dictionary containing the mappings for charting.
- Parameters:
self – Represent the instance of the class
- Returns:
A DataFrame containing the mappings for charting for all Equity and F&O instruments
- get_last_traded_date()[source]
The get_last_traded_date function returns the last traded date of NIFTY 50 index.
- Parameters:
self – Represent the instance of the class
- Returns:
The date of the last traded day
- get_market_status_and_current_val(index: str = 'NIFTY 50') tuple[source]
The get_market_status_and_current_val function returns the market status and current value of a given index.
- Parameters:
self – Represent the instance of the class
index – Get the market status and last price of a particular index
- Returns:
A tuple of the market status and the current value
- get_nse_turnover() DataFrame[source]
The get_nse_turnover provides the entire turnover happened in NSE exchange for the day / previous trading session as DataFrame.
- Parameters:
self – Represent the instance of the class
- Returns:
The exchange turnover data in the DataFrame format
- get_ohlc_data(ticker_or_idx: str = 'NIFTY 50', timeframe: str = '5Min', is_index: bool = True, underlying_symbol: str | None = None) DataFrame[source]
The get_ohlc_data function takes in a ticker or index name, and returns the OHLC data for that ticker/index. The function also takes in a timeframe parameter which can be used to specify the time interval of each candle. By default, it is set to 5 minutes.
- Parameters:
underlying_symbol – Index Symbol for options strikes
self – Represents the instance of the class
ticker_or_idx – Specify the ticker or index for which we want to get data
timeframe – (optional) Define the time interval for which we want to get the data
is_index – (optional) Determine whether the ticker is an index or a stock
- Returns:
The ohlc data for a given ticker or index
- get_ohlc_from_charting(ticker: str, timeframe: str, start_date: datetime, end_date: datetime) DataFrame[source]
The get_ohlc_from_charting function returns a DataFrame containing the OHLC data for a given ticker and timeframe.
- Parameters:
self – Represent the instance of the class
ticker – Specify the ticker for which we want to get the data (!! Its not same as NSE website ticker, Get the mapping from get_charting_mappings() function !!)
timeframe – Specify the time interval for which we want to get the data
start_date – Specify the start date of the data
end_date – Specify the end date of the data
- Returns:
A DataFrame containing OHLC data for a given ticker and timeframe
- get_second_wise_data(ticker_or_index: str = 'NIFTY 50', is_index: bool = True, underlying_symbol: str | None = None) DataFrame[source]
- The get_second_wise_data function returns a dataframe with the following columns:
timestamp - The time at which the price was recorded. price - The value of the index/stock at that particular time.
- Parameters:
self – Bind the method to a class
ticker_or_index – Specify the index for which we want to get data
is_index – (optional) Determine whether the index is an index or not
underlying_symbol – (optional) This is required for fetching derivatives OHLC data where underlying
assets ticker
- Returns:
A dataframe with second wise data
- search(search_text: str) dict[source]
The search function can be used to take out data related to an Equity/ Derivatives or any type of asset traded on NSE, this is required to take out symbol/ticker ids respective to that asset
- Parameters:
self – Represent the instance of the class
search_text – Specify the ticker or index for which we want to get data
- Returns:
The ohlc data for a given ticker or index