Skip to content

Commit a5617be

Browse files
authored
feat: add support for rpi orders (#1644)
1 parent f82d5fb commit a5617be

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

binance/async_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,11 @@ async def futures_exchange_info(self):
17161716
async def futures_order_book(self, **params):
17171717
return await self._request_futures_api("get", "depth", data=params)
17181718

1719+
async def futures_rpi_depth(self, **params):
1720+
return await self._request_futures_api("get", "rpiDepth", data=params)
1721+
1722+
futures_rpi_depth.__doc__ = Client.futures_rpi_depth.__doc__
1723+
17191724
async def futures_recent_trades(self, **params):
17201725
return await self._request_futures_api("get", "trades", data=params)
17211726

@@ -2139,6 +2144,11 @@ async def futures_account(self, **params):
21392144
"get", "account", True, version=2, data=params
21402145
)
21412146

2147+
async def futures_symbol_adl_risk(self, **params):
2148+
return await self._request_futures_api("get", "symbolAdlRisk", True, data=params)
2149+
2150+
futures_symbol_adl_risk.__doc__ = Client.futures_symbol_adl_risk.__doc__
2151+
21422152
async def futures_change_leverage(self, **params):
21432153
return await self._request_futures_api("post", "leverage", True, data=params)
21442154

binance/client.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7309,6 +7309,43 @@ def futures_order_book(self, **params):
73097309
"""
73107310
return self._request_futures_api("get", "depth", data=params)
73117311

7312+
def futures_rpi_depth(self, **params):
7313+
"""Get RPI Order Book with Retail Price Improvement orders
7314+
7315+
https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/RPI-Order-Book
7316+
7317+
:param symbol: required
7318+
:type symbol: str
7319+
:param limit: Default 1000; Valid limits:[1000]
7320+
:type limit: int
7321+
7322+
:returns: API response
7323+
7324+
.. code-block:: python
7325+
7326+
{
7327+
"lastUpdateId": 1027024,
7328+
"E": 1589436922972, // Message output time
7329+
"T": 1589436922959, // Transaction time
7330+
"bids": [
7331+
[
7332+
"4.00000000", // PRICE
7333+
"431.00000000" // QTY
7334+
]
7335+
],
7336+
"asks": [
7337+
[
7338+
"4.00000200",
7339+
"12.00000000"
7340+
]
7341+
]
7342+
}
7343+
7344+
:raises: BinanceRequestException, BinanceAPIException
7345+
7346+
"""
7347+
return self._request_futures_api("get", "rpiDepth", data=params)
7348+
73127349
def futures_recent_trades(self, **params):
73137350
"""Get recent trades (up to last 500).
73147351

@@ -8195,6 +8232,47 @@ def futures_account(self, **params):
81958232
"""
81968233
return self._request_futures_api("get", "account", True, 2, data=params)
81978234

8235+
def futures_symbol_adl_risk(self, **params):
8236+
"""Query the symbol-level ADL (Auto-Deleveraging) risk rating
8237+
8238+
The ADL risk rating measures the likelihood of ADL during liquidation.
8239+
Rating can be: high, medium, low. Updated every 30 minutes.
8240+
8241+
https://developers.binance.com/docs/derivatives/usds-margined-futures/account/rest-api/Query-ADL-Risk-Rating
8242+
8243+
:param symbol: optional - if not provided, returns ADL risk for all symbols
8244+
:type symbol: str
8245+
8246+
:returns: API response
8247+
8248+
.. code-block:: python
8249+
8250+
# Single symbol
8251+
{
8252+
"symbol": "BTCUSDT",
8253+
"adlRisk": "low",
8254+
"updateTime": 1597370495002
8255+
}
8256+
8257+
# All symbols (when symbol not provided)
8258+
[
8259+
{
8260+
"symbol": "BTCUSDT",
8261+
"adlRisk": "low",
8262+
"updateTime": 1597370495002
8263+
},
8264+
{
8265+
"symbol": "ETHUSDT",
8266+
"adlRisk": "high",
8267+
"updateTime": 1597370495004
8268+
}
8269+
]
8270+
8271+
:raises: BinanceRequestException, BinanceAPIException
8272+
8273+
"""
8274+
return self._request_futures_api("get", "symbolAdlRisk", True, data=params)
8275+
81988276
def futures_change_leverage(self, **params):
81998277
"""Change user's initial leverage of specific symbol market
82008278

binance/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
TIME_IN_FORCE_FOK = "FOK" # Fill or kill
5757
TIME_IN_FORCE_GTX = "GTX" # Post only order
5858
TIME_IN_FORCE_GTD = "GTD" # Good till date
59+
TIME_IN_FORCE_RPI = "RPI" # Retail Price Improvement
5960

6061
ORDER_RESP_TYPE_ACK = "ACK"
6162
ORDER_RESP_TYPE_RESULT = "RESULT"

binance/ws/streams.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,22 @@ def futures_depth_socket(self, symbol: str, depth: str = "10", futures_type=Futu
11281128
symbol.lower() + "@depth" + str(depth), futures_type=futures_type
11291129
)
11301130

1131+
def futures_rpi_depth_socket(self, symbol: str, futures_type=FuturesType.USD_M):
1132+
"""Subscribe to a futures RPI (Retail Price Improvement) depth data stream
1133+
1134+
RPI orders are included and aggregated in the stream. Crossed price levels are hidden.
1135+
Updates every 500ms.
1136+
1137+
https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/websocket-market-streams/RPI-Order-Book
1138+
1139+
:param symbol: required
1140+
:type symbol: str
1141+
:param futures_type: use USD-M or COIN-M futures default USD-M
1142+
"""
1143+
return self._get_futures_socket(
1144+
symbol.lower() + "@rpiDepth@500ms", futures_type=futures_type
1145+
)
1146+
11311147
def options_new_symbol_socket(self):
11321148
"""Subscribe to a new symbol listing information stream.
11331149

tests/test_async_client_futures.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async def test_futures_order_book(futuresClientAsync):
1818
order_book = await futuresClientAsync.futures_order_book(symbol="BTCUSDT")
1919
assert_ob(order_book)
2020

21+
async def test_futures_rpi_depth(futuresClientAsync):
22+
rpi_depth = await futuresClientAsync.futures_rpi_depth(symbol="BTCUSDT")
23+
assert_ob(rpi_depth)
24+
2125
async def test_futures_recent_trades(futuresClientAsync):
2226
await futuresClientAsync.futures_recent_trades(symbol="BTCUSDT")
2327

@@ -251,6 +255,21 @@ async def test_futures_account_balance(futuresClientAsync):
251255
async def test_futures_account(futuresClientAsync):
252256
await futuresClientAsync.futures_account()
253257

258+
async def test_futures_symbol_adl_risk(futuresClientAsync):
259+
# Test without symbol (get all)
260+
adl_risks = await futuresClientAsync.futures_symbol_adl_risk()
261+
assert isinstance(adl_risks, list)
262+
263+
# Test with specific symbol (if any symbols available)
264+
if len(adl_risks) > 0:
265+
test_symbol = adl_risks[0]["symbol"]
266+
adl_risk = await futuresClientAsync.futures_symbol_adl_risk(symbol=test_symbol)
267+
assert isinstance(adl_risk, dict)
268+
assert "symbol" in adl_risk
269+
assert "adlRisk" in adl_risk
270+
assert adl_risk["adlRisk"] in ["low", "medium", "high"]
271+
assert adl_risk["symbol"] == test_symbol
272+
254273
async def test_futures_change_leverage(futuresClientAsync):
255274
await futuresClientAsync.futures_change_leverage(symbol="LTCUSDT", leverage=10)
256275

tests/test_client_futures.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def test_futures_order_book(futuresClient):
2424
assert_ob(order_book)
2525

2626

27+
def test_futures_rpi_depth(futuresClient):
28+
rpi_depth = futuresClient.futures_rpi_depth(symbol="BTCUSDT")
29+
assert_ob(rpi_depth)
30+
31+
2732
def test_futures_recent_trades(futuresClient):
2833
futuresClient.futures_recent_trades(symbol="BTCUSDT")
2934

@@ -300,6 +305,22 @@ def test_futures_account(futuresClient):
300305
futuresClient.futures_account()
301306

302307

308+
def test_futures_symbol_adl_risk(futuresClient):
309+
# Test without symbol (get all)
310+
adl_risks = futuresClient.futures_symbol_adl_risk()
311+
assert isinstance(adl_risks, list)
312+
313+
# Test with specific symbol (if any symbols available)
314+
if len(adl_risks) > 0:
315+
test_symbol = adl_risks[0]["symbol"]
316+
adl_risk = futuresClient.futures_symbol_adl_risk(symbol=test_symbol)
317+
assert isinstance(adl_risk, dict)
318+
assert "symbol" in adl_risk
319+
assert "adlRisk" in adl_risk
320+
assert adl_risk["adlRisk"] in ["low", "medium", "high"]
321+
assert adl_risk["symbol"] == test_symbol
322+
323+
303324
def test_futures_change_leverage(futuresClient):
304325
futuresClient.futures_change_leverage(symbol="LTCUSDT", leverage=10)
305326

0 commit comments

Comments
 (0)