Skip to content
Merged
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ pass `testnet=True` when creating the client.
# fetch weekly klines since it listed
klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")

# create conditional order using the dedicated method
algo_order = client.futures_create_algo_order(symbol="LTCUSDT", side="BUY", type="STOP_MARKET", quantity=0.1, triggerPrice = 120)

# create conditional order using the create_order method (will redirect to the algoOrder as well)
order2 = await client.futures_create_order(symbol="LTCUSDT", side="BUY", type="STOP_MARKET", quantity=0.1, triggerPrice = 120)

# cancel algo/conditional order
cancel2 = await client.futures_cancel_algo_order(orderId=order2["orderId"], symbol="LTCUSDT")

# fetch open algo/conditional orders
open_orders = await client.futures_get_open_algo_orders(symbol="LTCUSDT")

# create order through websockets
order_ws = client.ws_create_order( symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1)

Expand Down Expand Up @@ -254,6 +266,7 @@ for more information.
# fetch weekly klines since it listed
klines = await client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")


# create order through websockets
order_ws = await client.ws_create_order( symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1)

Expand Down
139 changes: 129 additions & 10 deletions binance/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,9 +1888,32 @@ async def futures_loan_interest_history(self, **params):
)

async def futures_create_order(self, **params):
if "newClientOrderId" not in params:
params["newClientOrderId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
return await self._request_futures_api("post", "order", True, data=params)
# Check if this is a conditional order type that needs to use algo endpoint
order_type = params.get("type", "").upper()
conditional_types = [
"STOP",
"STOP_MARKET",
"TAKE_PROFIT",
"TAKE_PROFIT_MARKET",
"TRAILING_STOP_MARKET",
]

if order_type in conditional_types:
# Route to algo order endpoint
if "clientAlgoId" not in params:
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
# Remove newClientOrderId if it was added by default
params.pop("newClientOrderId", None)
params["algoType"] = "CONDITIONAL"
# Convert stopPrice to triggerPrice for algo orders (camelCase per API docs)
if "stopPrice" in params and "triggerPrice" not in params:
params["triggerPrice"] = params.pop("stopPrice")
return await self._request_futures_api("post", "algoOrder", True, data=params)
else:
# Use regular order endpoint
if "newClientOrderId" not in params:
params["newClientOrderId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
return await self._request_futures_api("post", "order", True, data=params)

async def futures_limit_order(self, **params):
"""Send in a new futures limit order.
Expand Down Expand Up @@ -1971,6 +1994,8 @@ async def futures_modify_order(self, **params):
"""
return await self._request_futures_api("put", "order", True, data=params)

futures_modify_order.__doc__ = Client.futures_modify_order.__doc__

async def futures_create_test_order(self, **params):
return await self._request_futures_api("post", "order/test", True, data=params)

Expand All @@ -1987,21 +2012,66 @@ async def futures_place_batch_order(self, **params):
)

async def futures_get_order(self, **params):
return await self._request_futures_api("get", "order", True, data=params)
# Check if this is a request for a conditional/algo order
is_conditional = params.pop("conditional", False)
# Also check if algoId or clientAlgoId is provided
if "algoId" in params or "clientAlgoId" in params:
is_conditional = True

if is_conditional:
return await self._request_futures_api("get", "algoOrder", True, data=params)
else:
return await self._request_futures_api("get", "order", True, data=params)

futures_get_order.__doc__ = Client.futures_get_order.__doc__

async def futures_get_open_orders(self, **params):
return await self._request_futures_api("get", "openOrders", True, data=params)
is_conditional = params.pop("conditional", False)

if is_conditional:
return await self._request_futures_api("get", "openAlgoOrders", True, data=params)
else:
return await self._request_futures_api("get", "openOrders", True, data=params)

futures_get_open_orders.__doc__ = Client.futures_get_open_orders.__doc__

async def futures_get_all_orders(self, **params):
return await self._request_futures_api("get", "allOrders", True, data=params)
is_conditional = params.pop("conditional", False)

if is_conditional:
return await self._request_futures_api("get", "allAlgoOrders", True, data=params)
else:
return await self._request_futures_api("get", "allOrders", True, data=params)

futures_get_all_orders.__doc__ = Client.futures_get_all_orders.__doc__

async def futures_cancel_order(self, **params):
return await self._request_futures_api("delete", "order", True, data=params)
# Check if this is a request for a conditional/algo order
is_conditional = params.pop("conditional", False)
# Also check if algoId or clientAlgoId is provided
if "algoId" in params or "clientAlgoId" in params:
is_conditional = True

if is_conditional:
return await self._request_futures_api("delete", "algoOrder", True, data=params)
else:
return await self._request_futures_api("delete", "order", True, data=params)

futures_cancel_order.__doc__ = Client.futures_cancel_order.__doc__

async def futures_cancel_all_open_orders(self, **params):
return await self._request_futures_api(
"delete", "allOpenOrders", True, data=params
)
is_conditional = params.pop("conditional", False)

if is_conditional:
return await self._request_futures_api(
"delete", "algoOpenOrders", True, data=params
)
else:
return await self._request_futures_api(
"delete", "allOpenOrders", True, data=params
)

futures_cancel_all_open_orders.__doc__ = Client.futures_cancel_all_open_orders.__doc__

async def futures_cancel_orders(self, **params):
if params.get("orderidlist"):
Expand All @@ -2021,6 +2091,44 @@ async def futures_countdown_cancel_all(self, **params):
"post", "countdownCancelAll", True, data=params
)

# Algo Orders (Conditional Orders)

async def futures_create_algo_order(self, **params):
if "clientAlgoId" not in params:
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
if "algoType" not in params:
params["algoType"] = "CONDITIONAL"
return await self._request_futures_api("post", "algoOrder", True, data=params)

futures_create_algo_order.__doc__ = Client.futures_create_algo_order.__doc__

async def futures_cancel_algo_order(self, **params):
return await self._request_futures_api("delete", "algoOrder", True, data=params)

futures_cancel_algo_order.__doc__ = Client.futures_cancel_algo_order.__doc__

async def futures_cancel_all_algo_open_orders(self, **params):
return await self._request_futures_api(
"delete", "algoOpenOrders", True, data=params
)

futures_cancel_all_algo_open_orders.__doc__ = Client.futures_cancel_all_algo_open_orders.__doc__

async def futures_get_algo_order(self, **params):
return await self._request_futures_api("get", "algoOrder", True, data=params)

futures_get_algo_order.__doc__ = Client.futures_get_algo_order.__doc__

async def futures_get_open_algo_orders(self, **params):
return await self._request_futures_api("get", "openAlgoOrders", True, data=params)

futures_get_open_algo_orders.__doc__ = Client.futures_get_open_algo_orders.__doc__

async def futures_get_all_algo_orders(self, **params):
return await self._request_futures_api("get", "allAlgoOrders", True, data=params)

futures_get_all_algo_orders.__doc__ = Client.futures_get_all_algo_orders.__doc__

async def futures_account_balance(self, **params):
return await self._request_futures_api(
"get", "balance", True, version=3, data=params
Expand Down Expand Up @@ -3973,6 +4081,17 @@ async def ws_futures_account_status(self, **params):
"""
return await self._ws_futures_api_request("account.status", True, params)

async def ws_futures_create_algo_order(self, **params):
if "clientAlgoId" not in params:
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
return await self._ws_futures_api_request("algoOrder.place", True, params)
ws_futures_create_algo_order.__doc__ = Client.ws_futures_create_algo_order.__doc__

async def ws_futures_cancel_algo_order(self, **params):
return await self._ws_futures_api_request("algoOrder.cancel", True, params)

ws_futures_cancel_algo_order.__doc__ = Client.ws_futures_cancel_algo_order.__doc__

####################################################
# Gift Card API Endpoints
####################################################
Expand Down
Loading
Loading