Like with any API for algo trading, there is a rate limit. If you exceed the rate limit in a certain period of time, you will be denied. Try to design your strategy with care in order to stay within the limit. Think of requests as currency. You have a budget to work with.
What is a rate limit?
Rate limiting will restrict how many requests you can make in a specific timeframe. This could be, for example, a number of requests per second, minute or day. Rate limits are imposed in order to fairly manage a service across users. The rate limit will vary depending on the broker and endpoint. Check the official documentation, and do not assume.
What is throughput?
The amount of work done by your system in a specific time period. It could be orders per second or price updates per second. The amount of throughput you can have cannot be greater than the rate limit. Therefore, take the smaller value between them.
Check your account first
Ensure that your open demat account, trading account and API are enabled. Depending on your account, you may be able to do different things.
Polling or streaming?
Polling means repeatedly asking, “Has the price changed?” This will cost requests. Alternatively, you can use streaming, where updates will be sent to you automatically through an open connection.
|
Polling |
Streaming |
|
|---|---|---|
|
Cost |
One request per check |
One connection |
|
Delay |
Depends on your interval |
Usually lower |
|
Limit risk |
High |
Low |
Streaming may or may not be possible. If it is not, you should use polling.
Spend your request budget well
-
Save data you already have, instead of asking again.
-
Poll slow data, such as holdings, less often than prices.
-
Remove duplicate calls.
-
Never check an order’s status in a tight loop.
-
Batch requests only where the platform allows it.
Do the maths before you code
If you are polling for 100 stocks, and polling once per second, you will make 100 requests per second. If you only have a limit of 10 requests per second, you will quickly exceed the limit.
However, if you poll once every 10 seconds, you have 10 requests per second. To be safe, poll once every 15 seconds. This would give around 7 requests per second.
What to do when you hit the limit
If you exceed the rate limit, the server will likely return an error. However, do not immediately retry the request. Wait, and try again. Increase the wait time on subsequent attempts. This is known as back-off. Make sure to log all events so you can identify any patterns.
Prioritise exit and cancellation orders in the queue. Stop-loss orders should never be delayed behind regular data requests.
Shape the strategy to fit
If the limit is tight, slow the strategy down:
-
Use longer candles, such as five minutes instead of one.
-
Watch fewer instruments.
-
Trade less often, with clearer signals.
-
Stream the few prices that matter most.
Often, a less intensive strategy will perform better by reducing noise.
Test safely
Start with very small volumes. Try to count how many requests you are making per minute. Include logic within your code to ensure that you are making fewer than approximately 80% of the limit so that you have a buffer during busy times.
Final Words
Overall, rate limiting should be considered in the design. Know what values you have, stream where possible, make requests on important data, and prioritise exiting. Start with small volumes to test that the strategy is working.