Python API SDK for Brokerage Client
Overview
The BrokerageClient
Python SDK provides an interface to interact with the brokerage API, enabling users to manage orders, trades, and positions efficiently. It supports authentication, order creation, modification, cancellation, and data retrieval.
Table of Contents
- Installation
- Initialization
- Authentication
- Order Management
- Data Retrieval
- Logging
- Error Handling
- Support
Installation
Ensure you have the required dependencies installed:
pip install requests asyncio nats-py
Initialization
First, initialize the client:
from brokerage_client import BrokerageClient
client = BrokerageClient()
Authentication
Set Token
The user need to have a valid authentication token which can be manually set using the following function:
client.setToken("your_auth_token")
Order Management
Order parameters
These parameters are common across different order varieties.
Parameter | Type | Description |
---|---|---|
ui_id | string | Unique order id given by client |
symbol_id | string | Unique symbol ID for the underlying asset |
exchange | enum string | Name of the exchange NSE BSE MCX |
segment | enum string | Segment FO CM |
price | float32 | Price at which order is placed |
quantity | int | Number of units in multiples of lot_size |
side | enum string | Side BUY SELL |
order_type | enum string | Order type LIMIT MARKET |
validity | enum string | Validity of order DAY IOC |
disclosed_quantity | int | Number of shares visible (Keep more than 30% of quantity) |
user_tag | enum string | An optional tag to apply to an order to identify it (alphanumeric, max 20 chars) |
Create Order
Creates a new order with the specified parameters.
Request Format:
client.create_order(
UI_id=54,
Price=100,
Quantity=10,
symbol_id=5678,
Side=1,
Exchange="NSE",
Segment="FO",
update="NEW_ORDER",
order_type="LIMIT"
)
API Request Body:
{
"ui_id": 54,
"price": 100,
"quantity":10,
"symbol_id":5678,
"side": 1,
"exchange": "NSE",
"segment": "FO",
"update_type": "NEW_ORDER",
"order_type": "LIMIT"
}
Response:
- Success Response:
- Code : 201
- Response:
{
"success": true,
"message": "Order created ...",
"data": 54,
"timestamp": {
"start_time": "2025-03-24T11:46:36.303115137+05:30",
"end_time": "2025-03-24T11:46:36.303249484+05:30"
}
}
Note - "data" represents the ui_id here
- Error Response:
- Code:
400 Bad Request
- Response:
{error :No Order Created!.}
Modify Order
Modifies an existing order.
Request:
client.modify_order(
UI_id=54,
Price=101,
Quantity=11,
SymbolID=5678,
Side=1,
Exchange="NSE",
Segment="FO",
update="MODIFY_ORDER",
order_type="LIMIT"
)
API Request:
{
"ui_id": 54,
"price": 101,
"quantity":11,
"symbol_id":5678,
"side": 1,
"exchange": "NSE",
"segment": "FO",
"update_type": "MODIFY_ORDER",
"order_type": "LIMIT"`
}
Response:
- Success Response:
- Code : 201
- Response:
{
"success": true,
"message": "Order modified ...",
"data": 54,
"timestamp": {
"start_time": "2025-03-24T11:54:59.364828106+05:30",
"end_time": "2025-03-24T11:54:59.365077399+05:30"
}
}
Note - "data" represents the ui_id here
- Error Response:
- Code: 401 Unauthorized
- Response:
{
"error": "Invalid credentials",
"status": "failure"
}
Delete Order
Cancels an existing order.
Request:
client.delete_order(
UI_id=54,
Price=100,
Quantity=10,
SymbolID=5678,
Side=1,
Exchange="NSE",
Segment="FO",
update="CANCEL_ORDER",
order_type="LIMIT"
)
API Request:
{
"ui_id": 54,
"price": 100,
"quantity":10,
"symbol_id":5678,
"side": 1,
"exchange": "NSE",
"segment": "FO",
"update_type": "CANCEL_ORDER",
"order_type": "LIMIT",
}
Response:
- Success Response:
- Code : 201
- Response
{
"success": true,
"message": "Order created ...",
"data": 40098,
"timestamp": {
"start_time": "2025-03-24T11:46:36.303115137+05:30",
"end_time": "2025-03-24T11:46:36.303249484+05:30"
}
}
Note - "data" represents the ui_id here
- Error Response:
- Code: 400 Bad Request
- Response: {error :No Order Created!.}
Data Retrieval
Get Orders
Retrieves a list of all orders for the client.
Regular order parameters
These parameters are common across different order varieties.
Parameter | Type | Description |
---|---|---|
cl_ordID | string | Unique client order ID |
exchg_ordID | string | The user/partner generated ID for tracking back |
order_status | enum string | Last updated status of the order PENDING REJECTED CANCELLED TRADED EXPIRED |
exchange | enum string | Name of the exchange NSE BSE MCX |
segment | enum string | Segment FO CM |
side | enum string | Side BUY SELL |
validity | enum string | Validity of order DAY IOC |
order_type | enum string | Order type LIMIT MARKET |
symbol | string | Symbol names where you can trade both regular shares and F&O, such as ACC HDFC BANK BANKNIFTY24O1651900CE |
symbol_id | string | Unique symbol ID |
quantity | int | Number of units in multiples of lot_size |
disclosed_quantity | int | Number of shares visible (Keep more than 30% of quantity) |
price | float32 | Price at which order is placed |
exchange_time | string | Exchange time |
created_time | string | Created time refers to the time when the order is placed |
updated_time | string | Updated time refers to the time when the order is updated |
avg_trade_price | float32 | Average trade price is the accumulated average price when multiple shares are traded at different prices |
user_tag | enum string | An optional tag to apply to an order to identify it (alphanumeric, max 20 chars) |
filled_qty | int | Filled quantity refers to the number of shares that have been traded |
oms_error_code | string | OMS Error code in case the order is REJECTED or FAILED |
oms_error_description | string | Description of error in case the order is REJECTED or FAILED |
Request:
orders = client.get_orders()
Response:
- Success Response:
- Code : 200
- Content:
{
"success": true,
"message": "Orders retrieved ...",
"data": [
{
"ui_id": 2013,
"user_order_id": 1000006,
"exchg_order_id": "101000009",
"order_status": "STANDING",
"exchange": "NSE",
"segment": "FO",
"side": 1,
"validity": "",
"order_type": "LIMIT",
"symbol": "MIDCPNIFTY25MAR8675CE",
"symbol_id": 35068,
"quantity": 6,
"disclosed_quantity": 0,
"price": 207845,
"avg_trade_price": 0,
"user_tag": "my_user_tag",
"remaining_quantity": 6,
"filled_qty": 0,
"oms_error_code": 0,
"oms_error_description": "",
"exchange_time": "",
"created_time": "2025-03-24 11:41:02.197656879 +0530 IST",
"updated_time": ""
}
],
"timestamp": {
"start_time": "2025-03-24T11:44:32.754177584+05:30",
"end_time": "2025-03-24T11:44:32.754408119+05:30"
}
}
- Error Response:
- Code:
404 NOT FOUND
- Content:
{error :Order not found!.}
Get Trades
Fetches trade details for executed orders.
Regular trade parameters
These parameters are common across different trades.
Parameter | Type | Description |
---|---|---|
symbol_id | string | Unique symbol ID |
exchange | enum string | Name of the exchange NSE BSE MCX |
segment | enum string | Segment FO CM |
side | enum string | Side BUY SELL |
symbol | string | Symbol names where you can trade both regular shares and F&O, such as ACC.NSE.EQ HDFCBANK.NSE.EQ BANKNIFTY24O1651900CE |
price | float32 | Price at which order is placed |
quantity | int | Number of units in multiples of lot_size |
fill_id | string | Trade specific identification generated by exchange |
order_id | string | Order specific identification generated by iRage |
fill_timestamp | string | Timestamp at which the trade was filled at the exchange |
created_time | string | Created time refers to the time when the order is placed |
exchange_timestamp | string | Timestamp at which the order was registered by the exchange |
Request:
trades = client.get_trades()
Response:
- Success Response:
- Code : 201
- Content:
{
"success": true,
"message": "Order created ...",
"data": 40098,
"timestamp": {
"start_time": "2025-03-24T11:46:36.303115137+05:30",
"end_time": "2025-03-24T11:46:36.303249484+05:30"
}
}
- Error Response:
- Code:
400 Bad Request
- Content:
{error :No Order Created!.}
Get Positions
Retrieves the current positions of the user.
Regular positions parameters
These parameters are common across different positions.
Parameter | Type | Description |
---|---|---|
security_id | string | Unique security ID |
symbol | string | Symbol names where you can trade both regular shares and F&O, such as ACC HDFC BANK BANKNIFTY24O1651900CE |
exchange | enum string | Name of the exchange NSE BSE MCX |
segment | enum string | Segment FO CM |
buy_avg | float32 | Buy Avg |
buy_qty | int | Buy quantity |
sell_avg | float32 | Average sell price of the share |
net_qty | int | Net quantity |
realized_profit | float32 | Realized Profit |
unrealized_profit | float32 | Unrealized Profit |
multiplier | int | multiplier |
day_buy_quantity | int | In day buying quantity |
day_buy_price | float32 | In day buying price |
day_sell_price | float32 | In day selling price |
day_sell_quantity | int | In day selling quantity |
overnight_quantity | int | In overnight quantity |
Request:
positions = client.get_positions()
Response:
- Success Response:
- Code : 200
- Content:
{
"success": true,
"message": "Positions retrieved ...",
"data": [
{
"ucc": "KESHAV",
"exchange_destination": "",
"symbol_id": 35068,
"symbol_name": "",
"expiry": "",
"strike": 0,
"instrument_type": "",
"underlying": "",
"buy_quantity": 2280,
"buy_value": 473949240,
"buy_avg_price": 207872,
"sell_quantity": 0,
"sell_value": 0,
"sell_avg_price": 0,
"net_quantity": 2280,
"realized_pnl": 473949240,
"order_type": 0,
"user_tag": "my_user_tag"
}
],
"timestamp": {
"start_time": "2025-03-24T11:41:05.313009683+05:30",
"end_time": "2025-03-24T11:41:05.313535334+05:30"
}
}
- Error Response:
- Code:
404 NOT FOUND
- Content:
{error :No Positions Found!.}
Logging
The SDK logs API interactions to api-client.log
. This helps in debugging and tracking API requests and responses.
Error Handling
The SDK includes error handling mechanisms to manage HTTP errors, SSL verification issues, and other exceptions.
Support
For any issues, contact IRAGE BROKING SERVICES LLP