处理加密交易所 API 速率限制,保持冷静

发布: (2026年3月17日 GMT+8 14:25)
2 分钟阅读
原文: Dev.to

Source: Dev.to

交易所的速率限制规则

  • Binance – 基于 IP,1200 权重/分钟
  • OKX – 按端点,60 请求/2 秒
  • Bybit – 分层限制,120 请求/分钟
  • Kraken – 随时间衰减的调用计数器

自适应速率限制器实现

import time

class AdaptiveRateLimiter:
    def __init__(self, base_delay=0.5):
        self.delay = base_delay
        self.consecutive_429s = 0

    def wait(self):
        time.sleep(self.delay)

    def on_success(self):
        self.consecutive_429s = 0
        self.delay = max(self.delay * 0.9, 0.1)

    def on_rate_limit(self):
        self.consecutive_429s += 1
        self.delay = min(self.delay * 2, 60)

最佳实践

  • 每个交易所的队列 – 不要在不同交易所之间共享速率限制器。
  • 对 HTTP 429 响应使用 指数退避,上限设为 60 秒。
  • 积极缓存 – 例如费用数据并不会每分钟都变化。
  • 在 API 支持的情况下 批量请求(例如 Binance 的批量端点)。

欲了解更多技术细节,请访问 kkinvesting.io

0 浏览
Back to Blog

相关文章

阅读更多 »