Risk Controls for API Exchange: Limits, Kill Switches and Sanity Checks
Infinite orders. Massive unauthorized exposure. In the event that your algo goes into an infinite loop sending buy orders, and you have exceeded rate limits on your broker, you will get locked out of your account. The exchange will not protect you from erroneous code. Only once you have executed orders will the broker prevent you from making further orders. You must implement safeguards.
As a risk manager, in designing an API for Algo Trading, you must implement the following protections in your code.
The Sanity Checks: Stale Data and Fat Fingers
Do not execute orders on stale data. If there is a delay in your WebSocket and your data is older than 2 seconds, do not execute an order. This will definitely lead to losses.
If the same order (same symbol, side, size, price) is sent within 500ms, then the second order should be prevented on your local system. This will prevent accidental loss of large amounts of money.
If a limit order has a price that differs from the last traded price by more than 5%, do not execute the order. This will prevent simple mistakes in decimal points from causing trades to be executed outside of your desired range.
The Speed Governor: Rate Limiting
Exchanges have strict regulations on the number of orders you can make. You are limited to 10 orders per second per account. If you exceed this, your API may be limited, or even suspended. Do not assume that you will be protected from exceeding these limits by your broker.
Implement a token bucket or sliding window queue to prevent you from sending more than 8/9 orders per second. This will give you a buffer so that you will never accidentally exceed the limit. Be cautious of data limits. Ensure that you are using WebSockets to get live price data, rather than using REST endpoints. Additionally, make sure that you are only making 3 concurrent connections.
The Capital Guard: Pre-Trade Margin Validation
Ensure you have enough funds to complete the order. It is dangerous to guess how much money you have available in your account, especially if you have multiple strategies.
Within your application, you should call a margin calculator endpoint. Send in the order details, and it will return the margin required. If this margin is higher than free cash in your account, then the bot will not perform the trade. This simple check will prevent a chain of rejections and penalty charges. To verify lot sizes and tick sizes, synchronize daily with the security master file provided by the broker. Do not hardcode this.
The Ultimate Fail Safe: Kill Switches
Things will go wrong. You need a switch to turn it all off.
True fail-safe. All open orders should be cancelled, and positions should be flat. Implement triggers within your code. If the maximum drawdown in your portfolio has been reached, set the flag TRADING_ENABLED to false globally, and exit all positions.
If you receive 3 rejections from the API in a row (either due to margin issues or incorrect order details), put the strategy on pause. This will prevent erroneous logic from causing a flood of requests to the server.
Create a simple command-line command, or button in a private dashboard. When pressed, this will cause all threads to stop and all open positions to be exited.


