API Security: Protecting Your Trading Credentials
5 mins read

API Security: Protecting Your Trading Credentials

Once an order is sent, you should be able to answer the following questions: what is in my portfolio, what is outstanding and what has changed? Let’s consider each in turn.

What do I hold right now?

Using the positions endpoint, you can see how much of each product you have, your average price, product types, and realised/unrealised P&L. Start by querying this on initialisation, and after disconnections. Never rely on remembering what you have in your portfolio, always refer back to the broker.

Before trading anything with algorithmic trading API, make sure that you go through the fundamental points of your account. You must have a trading account, you must have login and API enabled. Many newbies don’t remember the finances side as well. Orders must have sufficient cash and/or margin behind them or they will be outright rejected. Verify these points before, avoid getting your code in trouble as a result of such a problem.

What orders are still waiting?

See what orders you have made today. Often you can filter by order ID or tag. Orders can be in various states: pending, partially filled, filled, rejected, cancelled. The trade book will show you what fills you have had. Even though these are separate endpoints, it is worth checking both. In future and options, where market prices may be changing rapidly, you may receive fills at a bad time if the order is out-of-date. To avoid this, frequently check the order book.

How do I change an order?

Change price, trigger or quantity of a pending order. If partially filled, only the unfilled amount can be changed. After each change, recheck the order book to ensure the change was successful.

How do I cancel one?

Cancel individual or bulk orders. Cancel before placing a new order, to ensure that you do not accidentally have multiple live orders. Recheck the order book to ensure the cancellation was successful. Cancellations may be unsuccessful if the order was just filled.

How do I protect a position?

Stop loss and targets (where available) can be added to your position. These can be changed or removed at a later date. Products can also be converted (i.e. Intraday to Delivery), or you can square-off. Before increasing anything, use a margin calculator to check how much cash you will need.

How do I avoid double orders?

Connection issues may occur. If a timeout occurs, you may be unsure whether your order was successful. Where possible, give your order a unique client order ID/tag so that if you repeat the request you will not make a duplicate. Be careful not to exceed rate limits. For frequent updates, use websockets/webhooks for status updates rather than frequent polling.

If you don’t know it all, start with the account itself. The first step is open demat account setup as your shares are deposited in your demat account once the trade is settled. Once you’ve done that, connect it to your trading account and complete any broker verification you are required to do. Only then, you can create API credentials. This approach makes your initial tests easy, and enables you to identify problems with the accounts as early as possible.

How do I stay in sync with the app?

Fills and position changes may not be in sync. Therefore, on initialisation and after disconnection, ensure that your records are correct. Check that your records match those in the order book and positions endpoint. Occasionally open the broker application to double-check that everything matches. If there is a discrepancy, trust the broker’s data. Do not make any new orders until you know why there is a discrepancy.

Where do I look?

Question Where to look
What do I hold? Positions
What is pending? Order book
What was filled? Trade book
What changed? Status updates

How do I test this safely?

Sandbox (if available). Or try very small amounts. Try orders being rejected, partially filled, cancelled and disconnected. Ensure you log requests and responses. Ensure size and margin rules are correctly applied in error scenarios.

Conclusion

Once an order is placed, it is important to know if it has been filled, partially filled, rejected, or is still outstanding. If your bot cannot answer these questions, it is essentially blind.

After every action, check the order book and positions. Do not assume changes are successful. Give orders unique IDs so that duplicates can never be made. On initialisation and after disconnections, ensure that your data matches that of the broker. If there is a discrepancy, trust the broker’s data and do not make any new orders until you know why.

Start small. Try very small amounts and try error scenarios such as rejections, partial fills and disconnections. Occasionally check the application for a second opinion. It is far better to run a slow, stable system than a fast, fragile system. Once your bot can handle bad days well, you can confidently increase the scale.