BinanceFutures.RateLimiter (Binance Futures API v0.1.0) View Source

Rate Limiter handles Binance Futures API limits.

More info could be found here: https://binance-docs.github.io/apidocs/futures/en/#limits

Binance API has two type of limits.

  • weight limit - you have N available weight by IP address per time frame
  • orders limit - you have N available calls to ORDERS API's per API Key, per time frame.

Time frames for now are:

  • 1M - 1 Minute. Applicable for weight and orders limits.
  • 10S - 10 Seconds. Applicable for orders limits only.

By default it will only collect already used rates from API requests. And wouldn't be able to provide any remaining rate limits information.

Already used rate limits

To get already used limits for your IP/API Key you could use:

Remaining limits

If you need to keep track of remaining rate limits, you have to call BinanceFutures.RateLimiter.fetch_limits/0 function.

Example

iex(1)> BinanceFutures.RateLimiter.fetch_limits
:ok

It will spend some on your weight limit by calling BinanceFutures.USDM.MarketData.exchange_info/0 function. But also will grab remainig rate limits for your account.

After this call you will be able to keep track on remaining limits by using:

Link to this section Summary

Types

Limit type. Contain time frame as key, example: 1M, 10S And actual limit as value: 1, 2400

Limits for weight and orders types that fetched from BinanceFutures.USDM.MarketData.exchange_info/0

Functions

Returns a specification to start this module under a supervisor.

Fetches Binance Futures API limits. Uses BinanceFutures.USDM.MarketData.exchange_info/0 function for pulling information.

Get all available limits information.

Gets already used weight limit.

Gets remaining limits information. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining/0

Gets remaining orders limits. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining_orders/0

Gets remaining weight limits. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining_weight/0

Parses used limits from given headers.

Link to this section Types

Specs

limit() :: %{optional(binary()) => non_neg_integer()}

Limit type. Contain time frame as key, example: 1M, 10S And actual limit as value: 1, 2400

Example

%{"1M" => 2399}

%{"10S" => 300, "1M" => 1200}

Specs

limits() :: %{weight: limit(), orders: limit()}

Limits for weight and orders types that fetched from BinanceFutures.USDM.MarketData.exchange_info/0

Example

%{orders: %{"10S" => 300, "1M" => 1200}, weight: %{"1M" => 2400}}

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

fetch_limits() :: :ok | {:error, term()}

Fetches Binance Futures API limits. Uses BinanceFutures.USDM.MarketData.exchange_info/0 function for pulling information.

Returns {:error, term} in case of some issues with API call.

Example

iex(1)> BinanceFutures.RateLimiter.fetch_limits()
:ok

Specs

Get all available limits information.

Note that if you didn't call BinanceFutures.RateLimiter.fetch_limits/0 limits field will be empty.

Example

iex(1)> BinanceFutures.RateLimiter.get()
%{limits: %{orders: %{}, weight: %{}}, orders: %{}, weight: %{"1M" => 1}}
iex(2)> BinanceFutures.RateLimiter.fetch_limits()
:ok
iex(3)> BinanceFutures.RateLimiter.get()
%{
  limits: %{orders: %{"10S" => 300, "1M" => 1200}, weight: %{"1M" => 2400}},
  orders: %{},
  weight: %{"1M" => 2}
}

Specs

get_orders() :: limit()

Specs

get_weight() :: limit()

Gets already used weight limit.

Example

iex(1)> BinanceFutures.RateLimiter.get_weight()
%{}
iex(2)> BinanceFutures.USDM.MarketData.server_time()
{:ok, 1616347174621}
iex(3)> BinanceFutures.RateLimiter.get_weight()
%{"1M" => 2}

Specs

remaining() :: %{orders: limit(), weight: limit()}

Gets remaining limits information. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining/0

Example

iex(1)> BinanceFutures.RateLimiter.remaining()
%{orders: %{}, weight: %{}}
iex(2)> BinanceFutures.USDM.MarketData.server_time()
{:ok, 1616347615118}
iex(3)> BinanceFutures.RateLimiter.remaining()
%{orders: %{}, weight: %{}}
iex(4)> BinanceFutures.RateLimiter.fetch_limits()
:ok
iex(5)> BinanceFutures.RateLimiter.remaining()
%{orders: %{"10S" => 300, "1M" => 1200}, weight: %{"1M" => 2399}}

Specs

remaining_orders() :: limit()

Gets remaining orders limits. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining_orders/0

Specs

remaining_weight() :: limit()

Gets remaining weight limits. By default it does not calculate any remaining limits. To make it happen you have to call BinanceFutures.RateLimiter.fetch_limits/0 before calling BinanceFutures.RateLimiter.remaining_weight/0

Example

iex(1)> BinanceFutures.RateLimiter.remaining_weight()
%{}
iex(2)> BinanceFutures.USDM.MarketData.server_time()
{:ok, 1616347833596}
iex(3)> BinanceFutures.RateLimiter.remaining_weight()
%{}
iex(4)> BinanceFutures.RateLimiter.fetch_limits()
:ok
iex(5)> BinanceFutures.RateLimiter.remaining_weight()
%{"1M" => 2398}

Specs

set(HTTPoison.Base.headers()) :: :ok

Parses used limits from given headers.

Don't use this function if you don't know what are you doing ! This function is used in all REST API calls.