Skip to content

Commit

Permalink
feat(client): update oco-endpoint (#1476)
Browse files Browse the repository at this point in the history
* feat(client): update oco-endpoint

* add tests
  • Loading branch information
carlosmiei authored Nov 19, 2024
1 parent 6866ca5 commit 23e17d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,7 @@ def order_market_sell(self, **params):
def create_oco_order(self, **params):
"""Send in a new OCO order
https://binance-docs.github.io/apidocs/spot/en/#new-oco-trade
https://binance-docs.github.io/apidocs/spot/en/#new-order-list-oco-trade
:param symbol: required
:type symbol: str
Expand Down Expand Up @@ -1924,7 +1924,9 @@ def create_oco_order(self, **params):
:raises: BinanceRequestException, BinanceAPIException, BinanceOrderException, BinanceOrderMinAmountException, BinanceOrderMinPriceException, BinanceOrderMinTotalException, BinanceOrderUnknownSymbolException, BinanceOrderInactiveSymbolException
"""
return self._post("order/oco", True, data=params)
if "listClientOrderId" not in params:
params["listClientOrderId"] = self.SPOT_ORDER_PREFIX + self.uuid22()
return self._post("orderList/oco", True, data=params)

def order_oco_buy(self, **params):
"""Send in a new OCO buy order
Expand Down Expand Up @@ -11143,7 +11145,9 @@ async def order_market_sell(self, **params):
order_market_sell.__doc__ = Client.order_market_sell.__doc__

async def create_oco_order(self, **params):
return await self._post("order/oco", True, data=params)
if "listClientOrderId" not in params:
params["listClientOrderId"] = self.SPOT_ORDER_PREFIX + self.uuid22()
return await self._post("orderList/oco", True, data=params)

create_oco_order.__doc__ = Client.create_oco_order.__doc__

Expand Down
29 changes: 29 additions & 0 deletions tests/test_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def test_spot_cancel_replace_id():
url_dict = dict(pair.split("=") for pair in m.last_request.text.split("&"))
assert url_dict["newClientOrderId"].startswith("x-HNA2TXFJ")

def test_spot_oco_order_id():
with requests_mock.mock() as m:
m.post("https://api.binance.com/api/v3/orderList/oco", json={}, status_code=200)
client.create_oco_order(
symbol="LTCUSDT", side="BUY", aboveType="MARKET", quantity=0.1
)
url_dict = dict(pair.split("=") for pair in m.last_request.text.split("&"))
assert url_dict["listClientOrderId"].startswith("x-HNA2TXFJ")

def test_swap_id():
with requests_mock.mock() as m:
m.post("https://fapi.binance.com/fapi/v1/order", json={}, status_code=200)
Expand Down Expand Up @@ -262,6 +271,26 @@ def handler(url, **kwargs):
await clientAsync.close_connection()


@pytest.mark.asyncio()
async def test_spot_oco_id():
clientAsync = AsyncClient(api_key="api_key", api_secret="api_secret")
with aioresponses() as m:

def handler(url, **kwargs):
client_order_id = kwargs["data"][0][1]
assert client_order_id.startswith("x-HNA2TXFJ")

m.post(
"https://api.binance.com/api/v3/orderList/oco",
payload={"id": 1},
status=200,
callback=handler,
)
await clientAsync.create_oco_order(
symbol="BTCUSDT", side="BUY", type="MARKET", quantity=0.1
)
await clientAsync.close_connection()

@pytest.mark.asyncio()
async def test_swap_batch_id_async():
with aioresponses() as m:
Expand Down

0 comments on commit 23e17d6

Please sign in to comment.