Skip to content

Commit dc430f6

Browse files
committed
Merge branch 'master' of github.com:sammchardy/python-binance into verbose
2 parents 05285dc + a76774b commit dc430f6

10 files changed

Lines changed: 836 additions & 29 deletions

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ pass `testnet=True` when creating the client.
166166
# fetch weekly klines since it listed
167167
klines = client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
168168
169+
# create conditional order using the dedicated method
170+
algo_order = client.futures_create_algo_order(symbol="LTCUSDT", side="BUY", type="STOP_MARKET", quantity=0.1, triggerPrice = 120)
171+
172+
# create conditional order using the create_order method (will redirect to the algoOrder as well)
173+
order2 = await client.futures_create_order(symbol="LTCUSDT", side="BUY", type="STOP_MARKET", quantity=0.1, triggerPrice = 120)
174+
175+
# cancel algo/conditional order
176+
cancel2 = await client.futures_cancel_algo_order(orderId=order2["orderId"], symbol="LTCUSDT")
177+
178+
# fetch open algo/conditional orders
179+
open_orders = await client.futures_get_open_algo_orders(symbol="LTCUSDT")
180+
169181
# create order through websockets
170182
order_ws = client.ws_create_order( symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1)
171183
@@ -254,6 +266,7 @@ for more information.
254266
# fetch weekly klines since it listed
255267
klines = await client.get_historical_klines("NEOBTC", Client.KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
256268
269+
257270
# create order through websockets
258271
order_ws = await client.ws_create_order( symbol="LTCUSDT", side="BUY", type="MARKET", quantity=0.1)
259272

binance/async_client.py

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,9 +1906,32 @@ async def futures_loan_interest_history(self, **params):
19061906
)
19071907

19081908
async def futures_create_order(self, **params):
1909-
if "newClientOrderId" not in params:
1910-
params["newClientOrderId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
1911-
return await self._request_futures_api("post", "order", True, data=params)
1909+
# Check if this is a conditional order type that needs to use algo endpoint
1910+
order_type = params.get("type", "").upper()
1911+
conditional_types = [
1912+
"STOP",
1913+
"STOP_MARKET",
1914+
"TAKE_PROFIT",
1915+
"TAKE_PROFIT_MARKET",
1916+
"TRAILING_STOP_MARKET",
1917+
]
1918+
1919+
if order_type in conditional_types:
1920+
# Route to algo order endpoint
1921+
if "clientAlgoId" not in params:
1922+
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
1923+
# Remove newClientOrderId if it was added by default
1924+
params.pop("newClientOrderId", None)
1925+
params["algoType"] = "CONDITIONAL"
1926+
# Convert stopPrice to triggerPrice for algo orders (camelCase per API docs)
1927+
if "stopPrice" in params and "triggerPrice" not in params:
1928+
params["triggerPrice"] = params.pop("stopPrice")
1929+
return await self._request_futures_api("post", "algoOrder", True, data=params)
1930+
else:
1931+
# Use regular order endpoint
1932+
if "newClientOrderId" not in params:
1933+
params["newClientOrderId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
1934+
return await self._request_futures_api("post", "order", True, data=params)
19121935

19131936
async def futures_limit_order(self, **params):
19141937
"""Send in a new futures limit order.
@@ -1989,6 +2012,8 @@ async def futures_modify_order(self, **params):
19892012
"""
19902013
return await self._request_futures_api("put", "order", True, data=params)
19912014

2015+
futures_modify_order.__doc__ = Client.futures_modify_order.__doc__
2016+
19922017
async def futures_create_test_order(self, **params):
19932018
return await self._request_futures_api("post", "order/test", True, data=params)
19942019

@@ -2005,21 +2030,66 @@ async def futures_place_batch_order(self, **params):
20052030
)
20062031

20072032
async def futures_get_order(self, **params):
2008-
return await self._request_futures_api("get", "order", True, data=params)
2033+
# Check if this is a request for a conditional/algo order
2034+
is_conditional = params.pop("conditional", False)
2035+
# Also check if algoId or clientAlgoId is provided
2036+
if "algoId" in params or "clientAlgoId" in params:
2037+
is_conditional = True
2038+
2039+
if is_conditional:
2040+
return await self._request_futures_api("get", "algoOrder", True, data=params)
2041+
else:
2042+
return await self._request_futures_api("get", "order", True, data=params)
2043+
2044+
futures_get_order.__doc__ = Client.futures_get_order.__doc__
20092045

20102046
async def futures_get_open_orders(self, **params):
2011-
return await self._request_futures_api("get", "openOrders", True, data=params)
2047+
is_conditional = params.pop("conditional", False)
2048+
2049+
if is_conditional:
2050+
return await self._request_futures_api("get", "openAlgoOrders", True, data=params)
2051+
else:
2052+
return await self._request_futures_api("get", "openOrders", True, data=params)
2053+
2054+
futures_get_open_orders.__doc__ = Client.futures_get_open_orders.__doc__
20122055

20132056
async def futures_get_all_orders(self, **params):
2014-
return await self._request_futures_api("get", "allOrders", True, data=params)
2057+
is_conditional = params.pop("conditional", False)
2058+
2059+
if is_conditional:
2060+
return await self._request_futures_api("get", "allAlgoOrders", True, data=params)
2061+
else:
2062+
return await self._request_futures_api("get", "allOrders", True, data=params)
2063+
2064+
futures_get_all_orders.__doc__ = Client.futures_get_all_orders.__doc__
20152065

20162066
async def futures_cancel_order(self, **params):
2017-
return await self._request_futures_api("delete", "order", True, data=params)
2067+
# Check if this is a request for a conditional/algo order
2068+
is_conditional = params.pop("conditional", False)
2069+
# Also check if algoId or clientAlgoId is provided
2070+
if "algoId" in params or "clientAlgoId" in params:
2071+
is_conditional = True
2072+
2073+
if is_conditional:
2074+
return await self._request_futures_api("delete", "algoOrder", True, data=params)
2075+
else:
2076+
return await self._request_futures_api("delete", "order", True, data=params)
2077+
2078+
futures_cancel_order.__doc__ = Client.futures_cancel_order.__doc__
20182079

20192080
async def futures_cancel_all_open_orders(self, **params):
2020-
return await self._request_futures_api(
2021-
"delete", "allOpenOrders", True, data=params
2022-
)
2081+
is_conditional = params.pop("conditional", False)
2082+
2083+
if is_conditional:
2084+
return await self._request_futures_api(
2085+
"delete", "algoOpenOrders", True, data=params
2086+
)
2087+
else:
2088+
return await self._request_futures_api(
2089+
"delete", "allOpenOrders", True, data=params
2090+
)
2091+
2092+
futures_cancel_all_open_orders.__doc__ = Client.futures_cancel_all_open_orders.__doc__
20232093

20242094
async def futures_cancel_orders(self, **params):
20252095
if params.get("orderidlist"):
@@ -2039,6 +2109,44 @@ async def futures_countdown_cancel_all(self, **params):
20392109
"post", "countdownCancelAll", True, data=params
20402110
)
20412111

2112+
# Algo Orders (Conditional Orders)
2113+
2114+
async def futures_create_algo_order(self, **params):
2115+
if "clientAlgoId" not in params:
2116+
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
2117+
if "algoType" not in params:
2118+
params["algoType"] = "CONDITIONAL"
2119+
return await self._request_futures_api("post", "algoOrder", True, data=params)
2120+
2121+
futures_create_algo_order.__doc__ = Client.futures_create_algo_order.__doc__
2122+
2123+
async def futures_cancel_algo_order(self, **params):
2124+
return await self._request_futures_api("delete", "algoOrder", True, data=params)
2125+
2126+
futures_cancel_algo_order.__doc__ = Client.futures_cancel_algo_order.__doc__
2127+
2128+
async def futures_cancel_all_algo_open_orders(self, **params):
2129+
return await self._request_futures_api(
2130+
"delete", "algoOpenOrders", True, data=params
2131+
)
2132+
2133+
futures_cancel_all_algo_open_orders.__doc__ = Client.futures_cancel_all_algo_open_orders.__doc__
2134+
2135+
async def futures_get_algo_order(self, **params):
2136+
return await self._request_futures_api("get", "algoOrder", True, data=params)
2137+
2138+
futures_get_algo_order.__doc__ = Client.futures_get_algo_order.__doc__
2139+
2140+
async def futures_get_open_algo_orders(self, **params):
2141+
return await self._request_futures_api("get", "openAlgoOrders", True, data=params)
2142+
2143+
futures_get_open_algo_orders.__doc__ = Client.futures_get_open_algo_orders.__doc__
2144+
2145+
async def futures_get_all_algo_orders(self, **params):
2146+
return await self._request_futures_api("get", "allAlgoOrders", True, data=params)
2147+
2148+
futures_get_all_algo_orders.__doc__ = Client.futures_get_all_algo_orders.__doc__
2149+
20422150
async def futures_account_balance(self, **params):
20432151
return await self._request_futures_api(
20442152
"get", "balance", True, version=3, data=params
@@ -3991,6 +4099,17 @@ async def ws_futures_account_status(self, **params):
39914099
"""
39924100
return await self._ws_futures_api_request("account.status", True, params)
39934101

4102+
async def ws_futures_create_algo_order(self, **params):
4103+
if "clientAlgoId" not in params:
4104+
params["clientAlgoId"] = self.CONTRACT_ORDER_PREFIX + self.uuid22()
4105+
return await self._ws_futures_api_request("algoOrder.place", True, params)
4106+
ws_futures_create_algo_order.__doc__ = Client.ws_futures_create_algo_order.__doc__
4107+
4108+
async def ws_futures_cancel_algo_order(self, **params):
4109+
return await self._ws_futures_api_request("algoOrder.cancel", True, params)
4110+
4111+
ws_futures_cancel_algo_order.__doc__ = Client.ws_futures_cancel_algo_order.__doc__
4112+
39944113
####################################################
39954114
# Gift Card API Endpoints
39964115
####################################################

0 commit comments

Comments
 (0)