Skip to content

API Reference ‐ Utils

voyz edited this page May 3, 2024 · 3 revisions

Table of Contents

StockQuery

A class to encapsulate query parameters for filtering stock data.

This class is used to define a set of criteria for filtering stocks, which includes the stock symbol, name matching pattern, and conditions for instruments and contracts.

Attributes:

  • symbol str - The stock symbol to query.
  • name_match Optional[str], optional - A string pattern to match against stock names. Optional.
  • instrument_conditions Optional[dict], optional - Key-value pairs representing conditions to apply to stock instruments. Each condition is matched exactly against the instrument's attributes.
  • contract_conditions Optional[dict], optional - Key-value pairs representing conditions to apply to stock contracts. Each condition is matched exactly against the contract's attributes.

make_order_request

def make_order_request(conid: str,
                       side: str,
                       quantity: float,
                       order_type: str,
                       price: float,
                       coid: str,
                       acct_id: str,
                       conidex: str = None,
                       sec_type: str = None,
                       parent_id: str = None,
                       listing_exchange: str = None,
                       is_single_group: bool = None,
                       outside_rth: bool = None,
                       aux_price: float = None,
                       ticker: str = None,
                       tif: str = 'GTC',
                       trailing_amt: float = None,
                       trailing_type: str = None,
                       referrer: str = None,
                       cash_qty: float = None,
                       fx_qty: float = None,
                       use_adaptive: bool = None,
                       is_ccy_conv: bool = None,
                       allocation_method: str = None,
                       strategy: str = None,
                       strategy_parameters=None)

Create an order request object. Arguments set as None will not be included.

Arguments:

  • conid str - Identifier of the security to trade.

  • side str - Order side, either 'SELL' or 'BUY'.

  • quantity int - Order quantity in number of shares.

  • order_type str - Type of the order (e.g., LMT, MKT, STP).

  • price float - Order limit price, depends on order type.

  • coid str - Customer Order ID, unique for a 24h span.

  • acctId str, optional - Account ID, defaults to the first account if not provided.

  • conidex str, Optional - Concatenated value of contract identifier and exchange.

  • sec_type str, Optional - Concatenated value of contract-identifier and security type.

  • parent_id str, Optional - Used for child orders in bracket orders, must match the parent's cOID.

  • listing_exchange str, Optional, optional - Exchange for order routing, default is "SMART".

  • is_single_group bool, Optional - Set to True for placing single group orders (OCA).

  • outside_rth bool, Optional - Set to True if the order can be executed outside regular trading hours.

  • aux_price float, Optional - Auxiliary price parameter.

  • ticker str, Optional - Underlying symbol for the contract.

  • tif str, Optional - Time-In-Force for the order (e.g., GTC, OPG, DAY, IOC). Default: "GTC".

  • trailing_amt float, Optional - Trailing amount for TRAIL or TRAILLMT orders.

  • trailing_type str, Optional - Trailing type ('amt' or '%') for TRAIL or TRAILLMT orders.

  • referrer str, Optional - Custom order reference.

  • cash_qty float, Optional - Cash Quantity for the order.

  • fx_qty float, Optional - Cash quantity for Currency Conversion Orders.

  • use_adaptive bool, Optional - Set to True to use the Price Management Algo.

  • is_ccy_conv bool, Optional - Set to True for FX conversion orders.

  • allocation_method str, Optional - Allocation method for FA account orders.

  • strategy str, Optional - IB Algo algorithm to use for the order.

  • strategy_parameters dict, Optional - Parameters for the specified IB Algo algorithm.

ibind_logs_initialize

def ibind_logs_initialize(log_to_console: bool = var.LOG_TO_CONSOLE,
                          log_to_file: bool = var.LOG_TO_FILE,
                          log_level: str = var.LOG_LEVEL,
                          log_format: str = var.LOG_FORMAT)

Initialises the logging system.

Arguments:

  • log_to_console bool - Whether the logs should be output to the current console, True by default
  • log_to_file bool - Whether the logs should be written to a daily log file, True by default.
  • log_level str - What is the minimum log level of ibind logs, INFO by default.
  • log_format str - What is the log format to be used, '%(asctime)s|%(levelname)-.1s| %(message)s' by default.

Notes:

  • All of these parameters are read from the environment variables by default.
  • The daily file logs are saved in the directory specified by the IBIND_LOGS_DIR environment variable, the system temp directory by default.
  • To get more verbose logs, set either the log_level parameter or the IBIND_LOG_LEVEL environment variable to 'DEBUG'

execute_in_parallel

def execute_in_parallel(func: callable,
                        requests: Union[List[dict], Dict[str, dict]],
                        max_workers: int = None,
                        max_per_second: int = 20) -> Union[dict, list]

Executes a function in parallel using multiple sets of arguments with rate limiting.

This function utilises a thread pool to execute the given 'func' concurrently across different sets of arguments specified in 'requests'. The 'requests' can be either a list or a dictionary.

Arguments:

  • func callable - The function to be executed in parallel.
  • requests dict[str, dict] or list - A dictionary where keys are unique identifiers and values are dictionaries with 'args' and 'kwargs' for the 'func', or a list of such dictionaries.
  • max_workers int, optional - The maximum number of threads to use.
  • max_per_second int, optional - The maximum number of function executions per second. Defaults to 20.

Returns:

Union[dict, list]: A collection of results from the function executions, keyed by the same keys as 'requests' if it is a dictionary, or a list in the same order as the 'requests' list. The function returns results in a dictionary if 'requests' was a dictionary, and a list if 'requests' was a list.