Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API integration rate limits #547

Closed
happysammy opened this issue Jan 10, 2022 · 3 comments
Closed

API integration rate limits #547

happysammy opened this issue Jan 10, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@happysammy
Copy link

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
Copy link
Member

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:

  • Leverage a library such a ratelimit and decorate the HTTP client methods accordingly.
  • Write some efficient Cython for a token bucket algo, and utilize this into the HttpClient base class for flexible/configurable applications of rate limiting for certain endpoint paths.
  • Add functionality into adapter HTTP clients for monitoring rate limit status.

Do you have any thoughts or ideas?

@FGU1
Copy link

FGU1 commented Jan 20, 2022

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 cjdsellers self-assigned this Jan 26, 2022
@cjdsellers cjdsellers added enhancement New feature or request question Further information is requested labels Jan 26, 2022
@cjdsellers cjdsellers changed the title rateLimits API integration rate limits Apr 16, 2022
@cjdsellers
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
Development

No branches or pull requests

4 participants