2. API Reference
2.1. OpenBroker client
The main client and entry point to AlgoTest APIs.
It provides authentication and session management for the APIs and exposes the following interfaces: - Brokers API
BrokersClientfor broker information lookup - Instruments APIInstrumentsClientfor instrument information lookup - Orders APIOrdersClientfor order management
- class openbroker.OpenBroker(phone_number: str, password: str, orders_group_tag: str = '')
OpenBroker client. The main entry point to AlgoTest APIs. Initialize the OpenBroker client with the user credentials.
- Parameters:
phone_number – AlgoTest account phone number
password – AlgoTest account password
orders_group_tag – A common tag that a user wants to assign to all the orders placed by this client. It’s commonly used to identify a session or a strategy, by marking all the orders with the same identifier. The identifier can be kept same across multiple sessions to track and restore the orders state.
Attributes
- brokers
The
BrokersClientinstance for broker information lookup
- instruments
The
InstrumentsClientinstance for instrument information lookup
- orders
The
OrdersClientinstance for order management
Methods
- connect(instrument_filepath: str | None = None, order_update_callback: Callable | None = None) None
Connect to AlgoTest account and initialize internal components to start using the APIs. It initializes the OrdersClient, establishing a websocket connection to receive order updates. And if a group tag was supplied during initialization, fetches state of all previous orders belonging to that tag.
It also initializes the BrokersClient, fetching the list of brokers connected by the user. It also initializes the InstrumentsClient, fetching or restoring the list of instruments available for trading. It is recommended to provide a static instrument_filepath to avoid fetching instruments every time.
- Parameters:
instrument_filepath – [Optional] path to the file where instruments are stored. If it is None then the instruments are not initialized. If the file exists, it will be loaded. If the file does not exist, instruments will be fetched from the server and saved to the file.
order_update_callback – [Optional] A callback function that will be called whenever an order update is received on websocket.
- Returns:
None
- close()
Close the OpenBroker API connection
2.2. Brokers interface
- class openbroker.BrokersClient(user_session: UserSession)
- property brokers
Dictionary representing broker connections.
- get_broker(broker_type: Broker | None = None, broker_id: str | None = None, api_key: str | None = None) BrokerConnection | None
Get a broker configured in the user’s account. Use any of the kwargs to select the broker. Only one of the kwargs may be provided at a time.
- Parameters:
broker_type – Broker enum
broker_id – broker id
api_key – broker api key
- Returns:
BrokerConnection if requested broker is found, None otherwise
- login_broker(broker_type: Broker, **kwargs)
Login to the broker - Experimental feature currently implemented for a few brokers (Zerodha and XTS brokers)
- update_brokers()
Fetch the list of brokers connected by the user and update the internal state
- class openbroker.entity.BrokerConnection
Represents a broker account registered on AlgoTest.in :param id: :param broker_type: :param api_key: :param logged_until:
- id: str
- api_key: str
- property logged_in: bool
Boolean flag exposing the broker status. True if the broker is logged in for the trading day.
2.3. Instruments interface
- class openbroker.InstrumentsClient
- dump(file: str | TextIO)
Dump the contract map to a file for later use :param file: a path or a file object
- find_instrument(underlying: str, segment: Segment, expiry: str | None = None, strike: float | None = None, option_type: str | OptionType | None = None) Instrument | None
Find an instrument matching the kwargs options from all available instruments.
- Parameters:
underlying – The underlying symbol (i.e. ‘NIFTY’)
segment – The segment of the instrument (i.e. Segment.Future)
expiry – Expiry of the instrument, if applicable, for futures and options (i.e. ‘2023-12-28’)
strike – Strike price of the option, if applicable (i.e. 15000)
option_type – Option type, if applicable (i.e. OptionType.Call)
- Returns:
Instrument object if found, None otherwise
- load(file: str | TextIO)
Load the contract map from a file :param file: a path or a file object
- update()
Fetch the updated contract map from AlgoTest API :return:
- class openbroker.datatype.Instrument
Instrument dataclass representing an instrument and its properties.
- token: str
- symbol: str
- underlying: str
- segment: Segment
- expiry: str | None
- strike: float | None
- option_type: OptionType | None
- lot_size: int
- tick_size: float
- max_qty_in_order: int
- exchange: str
- is_tradable: bool
2.4. Orders interface
- class openbroker.OrdersClient(user_session: UserSession, orders_group_tag: str = '', order_update_callback: Callable | None = None)
- property all_orders
Dictionary representing all orders.
- cancel_orders(order_list: List[UUID]) Dict[UUID, CancelResponse]
Cancel orders in a single API call.
- Parameters:
order_list – list of order ids to be cancelled
- Returns:
dictionary of order_id: CancelResponse pairs for each order
- connect(ws_update=True, fetch_prev_orders=True)
Establish a connection to the Orders API Update Websocket, and fetches previous orders, if required.
- Parameters:
ws_update – If websocket should be enabled for orders update
fetch_prev_orders – If previous orders placed with the same orders_group_tag are to be fetched
- Returns:
None
- get_order(order_id: UUID, force_fetch=False) Order
Fetch the order object associated with the order_id. If the order is not available and updated in memory, it is fetched via API request. Specifying force_fetch=True will always fetch the order via API request.
- Parameters:
order_id – UUID of the order
force_fetch – always fetch the order via API request
- Returns:
The requested order, if found
- modify_orders(order_list: List[ModifyOrderRequestParams]) Dict[UUID, ModifyResponse]
Modify orders in a single API call.
The API supports modifying up to 10 orders in a single call.
- Parameters:
order_list – list of modify requests
- Returns:
dictionary of order_id: ModifyResponse pairs for each order
- place_orders(broker_connection: BrokerConnection, order_list: List[PlaceOrderRequestParams]) List[PlaceResponse]
Place orders in a single API call.
The API supports placing up to 10 orders in a single call.
- Parameters:
broker_connection – the broker the orders are to be placed with
order_list – list of place orders requests
- Returns:
list of PlaceResponse objects for each order placed (sorting is maintained)
2.4.1. Order objects
Order is a dataclass representing an existing order in the system.
- class openbroker.entity.Order
Class representing all the properties of and order that has been placed.
- instrument_id
The instrument id of the order.
- Type:
str
- symbol
The symbol of the order.
- Type:
str
- side
The side, buy or sell, of the order.
- Type:
Side
- product_type
The product type of the order (MIS or NRML).
- Type:
- order_params
The type and parameters of the order (Market, Limit, …).
- Type:
- quantity
The quantity of the order.
- Type:
int
- order_id
The internal order id of the order.
- Type:
int
- broker_order_id
The broker order id of the order.
- Type:
str
- status
The status of the order.
- Type:
2.4.1.1. Order parameters
Order parameters are specific to the order type and are used to create or modify an order.
- class openbroker.datatype.order.OrderParams
OrderParams()
- class openbroker.datatype.MarketOrderParams(reference_price: float = 0.0)
Bases:
OrderParamsDataclass representing the parameters of a market order
- class openbroker.datatype.LimitOrderParams(limit_price: float)
Bases:
OrderParamsDataclass representing the parameters of a limit order
- class openbroker.datatype.StopLossOrderParams(limit_price: float, trigger_price: float)
Bases:
OrderParamsDataclass representing the parameters of a stop loss order
2.4.1.2. Enums and constants
- class openbroker.constant.Broker
Enum representing the brokers integrated on AlgoTest.
- AcAgrawal = 'Broker.AcAgrawal'
- AliceBlue = 'Broker.AliceBlue'
- AngelOne = 'Broker.AngelOne'
- BigulXTS = 'Broker.BigulXTS'
- Choice = 'Broker.Choice'
- Dhan = 'Broker.Dhan'
- Edelweiss = 'Broker.Edelweiss'
- Firstock = 'Broker.Firstock'
- Finvasia = 'Broker.Finvasia'
- FivePaisa = 'Broker.FivePaisa'
- FlatTrade = 'Broker.FlatTrade'
- Fyers = 'Broker.Fyers'
- IIFL = 'Broker.IIFL'
- ICICI = 'Broker.ICICI'
- Jainam = 'Broker.Jainam'
- JainamPro = 'Broker.JainamPro'
- JainamXTS = 'Broker.JainamXTS'
- JMFL = 'Broker.JMFL'
- Kotak = 'Broker.Kotak'
- MasterTrust = 'Broker.MasterTrust'
- Motilal = 'Broker.Motilal'
- Paytm = 'Broker.Paytm'
- Upstox = 'Broker.Upstox'
- WisdomCapital = 'Broker.WisdomCapital'
- Zebu = 'Broker.Zebu'
- Zerodha = 'Broker.Zerodha'
- class openbroker.constant.ProductType
Enum representing the product type of an order: MIS or NRML.
- MIS = 'ProductType.MIS'
- NRML = 'ProductType.NRML'
- class openbroker.constant.PositionType
Enum representing the side of an order: Buy or Sell.
- Buy = 'PositionType.Buy'
- Sell = 'PositionType.Sell'
- class openbroker.constant.OrderStatus
Enum representing the status of an order
- Open = 'Open'
- TriggerPending = 'Trigger Pending'
- PlacePending = 'Place Pending'
- ModifyPending = 'Modify Pending'
- CancelPending = 'Cancel Pending'
- Canceled = 'Canceled'
- Rejected = 'Rejected'
- Completed = 'Completed'
2.4.2. Place/Modify/Cancel orders interfaces
2.4.2.1. Place order
- class openbroker.datatype.PlaceOrderRequestParams
Dataclass representing the place request parameters of a single order
- instrument_id: str
- symbol: str
- product_type: ProductType
- side: PositionType
- quantity: int
- order_info: OrderParams
- tags: Dict[str, str]
- user_tag: str = ''
2.4.2.2. Modify order
- class openbroker.datatype.ModifyOrderRequestParams
Dataclass representing the modify request parameters of a single order
- order_id: UUID
- order_info: OrderParams