You have a script that is created to purchase a Nifty option. You hit run. It is immediately rejected by the broker’s server. Why? It is not like pressing a green button on the screen when placing an order. This is a formatting test: it is strict. If you are slightly off, the exchange won’t hesitate to get rid of your order.
In trading, the use of an API in trading involves the creation of a structured JSON payload. It’s read by the system, evaluated against a dozen compliance rules and passed to the exchange or an error is thrown. A missing comma and/or the wrong product type, and your trade will be dead in the water.
The Four Order Types You Can Send
The majority of broker APIs offer the following basic order types for derivatives. You must have knowledge of the exact nature of each of them.
The Limit order is very simple. You indicate what you will pay actually. It’s only available at or below that price.
The Market order is one that is executed immediately at the best possible price. But many brokers will automatically turn the Market order into a Limit order with Market Price Protection. This ensures that you don’t end up with a bad deal in an uncommon option.
There are two prices that are needed for the Stop Loss order: the trigger price and the limit price. If the trigger is activated, a limit order will be executed.
The Stop Loss (SL) Market order takes over from your Stop Loss (SL) order and places a market order when your trigger price is touched. However, note that this is a tricky one. This order type is often limited or even prohibited for stock and index options, because of volatility rules on the exchange. If you send it, it will bounce.
The Parameters That Will Reject Your Payload
If you’ve chosen the right order type, the parameters have to be perfect. The system will not accept any mistakes.
The Exchange and Segment need to be specified. Trading derivatives on NSE should be done using NFO, BSE: BFO and commodities: MCX.
The largest & most dangerous trap is the Trading Symbol. It can’t be guessed in advance or dynamically built. You have to check with the broker’s daily Instrument Master file for the precise token. Just a simple typo will result in immediate rejection.
Product Type matters. Use NRML to carry overnight, or MIS for intraday. Derivatives cannot be settlement options. Options that cannot be settled are invalid for derivatives (such as CNC).
Must be a whole number of contract lots. An order of 100 will not be accepted for a lot size of 75.
Price Rules are enforceable. Stop Loss buy orders will require the limit price to be equal to or higher than the trigger price. If you are placing a sell order for Stop Loss, this sell limit should be less than or equal to the trigger price. The tick size also needs to be taken into consideration; typically, it will be 0.05.
You would need to open demat account with a facility for algorithmic trading before you write any code. API order routing isn’t available for all standard accounts. Requires correct permissions and an Algo ID tag (under regulations, this is a requirement).
Validation Errors to Expect
You will get some failures in your script. Create error handlers for some of the most common problems. Invalid symbols, lot sizes or prices outside the daily circuit limit will be rejected. Occasionally, the exchange refuses the order due to an error in the ID number of the Algo.
The Bottom Line
The power of automating trades is great. However, shoddy coding doesn’t get approved by the marketplace. Think of the JSON payload as a contract. Check that all parameters are correct prior to the “send” button being pressed.
