Handling API Rate Limits in Crypto Trading Bots
Nothing breaks a live crypto trading bot faster than a rate limit ban. Exchanges enforce strict limits on API calls to prevent abuse and ensure fair access. Violate them and you'll get 429 errors at the worst possible moment—during high volatility when you most need to execute.
How Rate Limits Work
Most exchanges use a token bucket or sliding window system. You have a budget of N requests per time window (e.g. 1200 weight per minute on Binance). Different endpoints cost different weights—order placement costs more than ticker queries. On delta exchange api trading, the limit is per IP and per API key.
Building a Rate Limiter
import time\nfrom collections import deque\nclass RateLimiter:\n def __init__(self, max_calls, period):\n self.calls = deque()\n self.max_calls = max_calls\n self.period = period\n def wait(self):\n now = time.time()\n while len(self.calls) >= self.max_calls:\n if now - self.calls[0] > self.period:\n self.calls.popleft()\n else:\n time.sleep(0.01)\n self.calls.append(time.time())Exponential Backoff
When you do hit a 429, back off exponentially: wait 1s, then 2s, then 4s. Never hammer the API in a tight retry loop or you will trigger an IP ban in your automated crypto trading system.