Trade API, which is also known as trade APIs or brokerage APIs, are the programming interfaces that brokers make available so that outside programs can talk to their trading systems. In algorithmic trading, these APIs are the main way that plans are put into action automatically. They are in charge of four different types of work:
- Get market information like real-time prices, depth of book, last traded price, volume, and open interest.
- Order handling means placing, changing, or canceling orders.
- Account and position searches, such as P&L, available margin, present positions, and holdings.
- Event streaming lets you see changes in the state of live orders, trade confirmations, and margin alerts.
Most Indian brokers make these functions available through RESTful HTTP endpoints for synchronous operations and WebSocket channels for updates that happen in real time and with low delay. Once a strategy is set up, the API is the only way to run it.
Authentication and the Lifecycle of a Session
Trade APIs need to be authenticated before they can be used. Most common flows:
- First login: POST /login with user ID, password (or TOTP), and API key/secret; session token is returned.
- To use a token, put it in the Authorization header or as a query argument for all calls after this one.
- Token refresh: Call the reset endpoint or log in again before the token expires, which is usually 24 hours.
- Logout means ending the session explicitly (optional but suggested).
Session control has to be done automatically in algo trading:
- Find replies with the status “401 Unauthorized” and force re-authentication.
- Tokens should only be kept in memory, never in logs or on files.
- Use heartbeat checks to find sessions that aren’t being used anymore.
- During execution, session expiration can lead to missed order changes or stopped position monitoring, which are both very bad things that can happen in real trading.
Find forced square-offs or margin calls
- When a disconnect happens, the auto-reconnect code needs to have exponential backoff and state recovery (re-sync positions/orders).
- Manage your position, margin, and risk through an API
Endpoints for positions (GET /positions) return:
- Sign, amount, average price, and realized/unrealized P&L
- Breakdown by product (CNC, MIS, and NRML)
The ends of the margins show:
- Available space
- Margin used (open orders plus options)
- Value of collateral
In algorithm dealing, you have to keep an eye on things all the time:
- You can poll every 5-15 seconds or sign up for information on the margins (if supported).
- Set strict limits on things like position size, exposure, and daily loss.
- On margin breach, drawdown cutoff, or end of day, auto-square-off.
Reconciliation and auditing after trade
- Internal history vs. trades recorded by brokers
- Price of fill compared to amount wanted (slippage)
Use of margin vs. expected
Every 5 to 15 minutes, reconciliation checks for errors and raises a red flag. Full audit trails record:
