nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
1.95k stars 445 forks source link

API integration rate limits #547

Closed happysammy closed 1 year ago

happysammy commented 2 years ago

Exchanges usually impose what is called a rate limit. Exchanges will remember and track your user credentials and your IP address and will not allow you to query the API too frequently. So I was wondering how you process ratelimits for these Exchanges(such as Binance or Ftx)? As far as i know, token bucket algorithm often be used to solve the rate limit problem.

cjdsellers commented 2 years ago

Hi @happysammy apologies for the delayed response!

The short answer is no, the current adapters don't do a good job of handling any rate limits.

However, there is some indirect rate limiting available via the RiskEngines order throttler, which is configurable (100/s by default). See the RiskEngineConfig docs:

class RiskEngineConfig(pydantic.BaseModel):
    """
    Configuration for ``RiskEngine`` instances.

    Parameters
    ----------
    bypass : bool
        If True then all risk checks are bypassed (will still check for duplicate IDs).
    max_order_rate : str, default=100/00:00:01
        The maximum order rate per timedelta.
    max_notional_per_order : Dict[str, str]
        The maximum notional value of an order per instrument ID.
        The value should be a valid decimal format.
    """

    bypass: bool = False
    max_order_rate: ConstrainedStr = ConstrainedStr("100/00:00:01")
    max_notional_per_order: Dict[str, str] = {}

If rate limiting is extremely important, you could probably adapt the generic Throttler into any adapter you may be writing.

Going forward my ideas on how to handle this are:

Do you have any thoughts or ideas?

FGU1 commented 2 years ago

For what it's worth, IB API allow automatic pacing. From their release notes :

API messages sent at a higher rate than 50/second can now be paced by TWS at the 50/second rate instead of potentially causing a disconnection. This is now done automatically by the RTD Server API and can be done with other API technologies by invoking SetConnectOptions("+PACEAPI") prior to eConnect.

cjdsellers commented 1 year ago

Closing this issue for now, as we finally have basic rate limiting available for the base HttpClient, with quotas currently being added for Binance.

Discussions and developments can be tracked here.