Skip to content

Commit 5539e0a

Browse files
authored
Merge pull request #518 from yzh-pelle/algo-orders-update
feat: support algo trades
2 parents 47753d1 + 96b7c51 commit 5539e0a

File tree

2 files changed

+362
-14
lines changed

2 files changed

+362
-14
lines changed

php-binance-api.php

Lines changed: 204 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public function cancel(string $symbol, ?string $orderid = null, $params = [])
516516
];
517517
if (!is_null($orderid)) {
518518
$request["orderId"] = $orderid;
519-
} else if (!is_set($params['origClientOrderId'])) {
519+
} else if (!isset($params['origClientOrderId'])) {
520520
throw new Exception("Either orderId or origClientOrderId must be provided");
521521
}
522522
return $this->apiRequest("v3/order", "DELETE", array_merge($request, $params), true);
@@ -545,7 +545,7 @@ public function orderStatus(string $symbol, ?string $orderid = null, array $para
545545
];
546546
if (!is_null($orderid)) {
547547
$request["orderId"] = $orderid;
548-
} else if (!is_set($params['origClientOrderId'])) {
548+
} else if (!isset($params['origClientOrderId'])) {
549549
throw new Exception("Either orderId or origClientOrderId must be provided");
550550
}
551551
return $this->apiRequest("v3/order", "GET", array_merge($request, $params), true);
@@ -1795,7 +1795,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param
17951795
$this->setXMbxUsedWeight1m($header['x-mbx-used-weight-1m']);
17961796
}
17971797
if (isset($json['msg']) && !empty($json['msg'])) {
1798-
if ($json['msg'] !== 'success' && $url != 'v1/system/status' && $url != 'v3/systemStatus.html' && $url != 'v3/accountStatus.html' && $url != 'v1/allOpenOrders') {
1798+
if ($json['msg'] !== 'success' && $url != 'v1/system/status' && $url != 'v3/systemStatus.html' && $url != 'v3/accountStatus.html' && $url != 'v1/allOpenOrders' && $url != 'v1/algoOpenOrders') {
17991799
// should always output error, not only on httpdebug
18001800
// not outputing errors, hides it from users and ends up with tickets on github
18011801
throw new \Exception('signedRequest error: '.print_r($output, true));
@@ -4721,7 +4721,7 @@ protected function createFuturesOrderRequest(string $side, string $symbol, $quan
47214721
echo "warning: price expected string got " . gettype($price) . PHP_EOL;
47224722
}
47234723

4724-
if ($type === "LIMIT" || $type === "STOP_LOSS_LIMIT" || $type === "TAKE_PROFIT_LIMIT") {
4724+
if ($type === "LIMIT" || $type === "STOP_LOSS_LIMIT" || $type === "TAKE_PROFIT_LIMIT" || $type === "STOP" || $type === "TAKE_PROFIT") {
47254725
$request["price"] = $price;
47264726
if (!isset($params['timeInForce'])) {
47274727
$request['timeInForce'] = 'GTC';
@@ -4804,8 +4804,56 @@ protected function createFuturesOrderRequest(string $side, string $symbol, $quan
48044804
public function futuresOrder(string $side, string $symbol, $quantity = null, $price = null, string $type = 'LIMIT', array $params = [], $test = false)
48054805
{
48064806
$request = $this->createFuturesOrderRequest($side, $symbol, $quantity, $price, $type, $params);
4807-
$qstring = ($test === false) ? 'v1/order' : 'v1/order/test';
4808-
return $this->fapiRequest($qstring, 'POST', $request, true);
4807+
$isAlgoOrder = $this->isAlgoFuturesOrderType($request['type']);
4808+
if ($isAlgoOrder) {
4809+
$algoRequest = $this->createAlgoFuturesOrderRequest($request);
4810+
return $this->fapiRequest('v1/algoOrder', 'POST', $algoRequest, true);
4811+
} else {
4812+
// regular order
4813+
$qstring = ($test === false) ? 'v1/order' : 'v1/order/test';
4814+
return $this->fapiRequest($qstring, 'POST', $request, true);
4815+
}
4816+
}
4817+
4818+
/**
4819+
* isAlgoFuturesOrderType helper to determine if an order type is an algo order
4820+
* @param string $type order type
4821+
* @return bool true if it is an algo order type
4822+
*/
4823+
protected function isAlgoFuturesOrderType(string $type): bool
4824+
{
4825+
$algoOrderTypes = [
4826+
'STOP',
4827+
'STOP_MARKET',
4828+
'TAKE_PROFIT',
4829+
'TAKE_PROFIT_MARKET',
4830+
'TRAILING_STOP_MARKET',
4831+
];
4832+
if (in_array($type, $algoOrderTypes)) {
4833+
return true;
4834+
}
4835+
return false;
4836+
}
4837+
4838+
/**
4839+
* createAlgoFuturesOrderRequest
4840+
* helper for creating the request for algo futures order
4841+
* @param array $request regular order request
4842+
* @return array containing the request
4843+
*/
4844+
protected function createAlgoFuturesOrderRequest(array $request): array
4845+
{
4846+
$request['algoType'] = 'CONDITIONAL';
4847+
if (!isset($request['clientAlgoId'])) {
4848+
$newClientOrderId = $request['newClientOrderId'];
4849+
$request['clientAlgoId'] = $newClientOrderId;
4850+
unset($request['newClientOrderId']);
4851+
}
4852+
if (!isset($request['triggerPrice']) && isset($request['stopPrice'])) {
4853+
$request['triggerPrice'] = $request['stopPrice'];
4854+
unset($request['stopPrice']);
4855+
}
4856+
return $request;
48094857
}
48104858

48114859
/**
@@ -5143,6 +5191,36 @@ public function futuresCancel(string $symbol, $orderid, $params = [])
51435191
return $this->fapiRequest("v1/order", 'DELETE', array_merge($request, $params), true);
51445192
}
51455193

5194+
/**
5195+
* futuresCancelAlgo cancels a futures order
5196+
*
5197+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Algo-Order
5198+
*
5199+
* $algoid = "123456789";
5200+
* $order = $api->futuresCancelAlgo($algoid);
5201+
*
5202+
* @param string $algoId (optional) the algoid to cancel (mandatory if $params['clientAlgoId'] is not set)
5203+
* @param array $params (optional) additional options
5204+
* - @param string $params['clientAlgoId'] original client order id to cancel
5205+
* - @param int $params['recvWindow'] the time in milliseconds to wait for a response
5206+
*
5207+
* @return array with error message or the order details
5208+
* @throws \Exception
5209+
*/
5210+
public function futuresCancelAlgo($algoId, $params = [])
5211+
{
5212+
$request = [];
5213+
if ($algoId) {
5214+
$request['algoid'] = $algoId;
5215+
} else if (isset($params['clientAlgoId'])) {
5216+
$request['clientalgoid'] = $params['clientAlgoId'];
5217+
unset($params['clientAlgoId']);
5218+
} else if (!isset($params['clientalgoid'])) {
5219+
throw new \Exception('futuresCancelAlgo(): either algoId or clientAlgoId must be set');
5220+
}
5221+
return $this->fapiRequest("v1/algoOrder", 'DELETE', array_merge($request, $params), true);
5222+
}
5223+
51465224
/**
51475225
* futuresCancelBatchOrders canceles multiple futures orders
51485226
*
@@ -5181,11 +5259,13 @@ public function futuresCancelBatchOrders(string $symbol, $orderIdList = null, $o
51815259
* futuresCancelOpenOrders cancels all open futures orders for a symbol
51825260
*
51835261
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-All-Open-Orders
5184-
*
5262+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-All-Algo-Open-Orders
51855263
* $orders = $api->futuresCancelOpenOrders("BNBBTC");
51865264
*
51875265
* @param string $symbol (mandatory) market symbol (e.g. ETHUSDT)
5188-
* @param int $recvWindow the time in milliseconds to wait for a response
5266+
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5267+
* - @param bool $params['isAlgo'] true for canceling algo orders
5268+
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
51895269
*
51905270
* @return array with error message or the orders details
51915271
* @throws \Exception
@@ -5195,8 +5275,32 @@ public function futuresCancelOpenOrders(string $symbol, array $params = [])
51955275
$request = [
51965276
'symbol' => $symbol,
51975277
];
5278+
if (isset($params['isAlgo']) && ($params['isAlgo'] === true)) {
5279+
unset($params['isAlgo']);
5280+
return $this->fapiRequest("v1/algoOpenOrders", 'DELETE', array_merge($request, $params), true);
5281+
} else {
5282+
return $this->fapiRequest("v1/allOpenOrders", 'DELETE', array_merge($request, $params), true);
5283+
}
5284+
}
51985285

5199-
return $this->fapiRequest("v1/allOpenOrders", 'DELETE', array_merge($request, $params), true);
5286+
/**
5287+
* futuresCancelOpenAlgoOrders cancels all open futures algo orders for a symbol
5288+
*
5289+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-All-Algo-Open-Orders
5290+
*
5291+
* $orders = $api->futuresCancelOpenAlgoOrders("BNBBTC");
5292+
*
5293+
* @param string $symbol (mandatory) market symbol (e.g. ETHUSDT)
5294+
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5295+
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
5296+
*
5297+
* @return array with error message or the orders details
5298+
* @throws \Exception
5299+
*/
5300+
public function futuresCancelOpenAlgoOrders (string $symbol, array $params = [])
5301+
{
5302+
$params['isAlgo'] = true;
5303+
return $this->futuresCancelOpenOrders($symbol, $params);
52005304
}
52015305

52025306
/**
@@ -5240,7 +5344,7 @@ public function futuresCountdownCancelAllOrders(string $symbol, int $countdownTi
52405344
* @return array with error message or the order details
52415345
* @throws \Exception
52425346
*/
5243-
public function futuresOrderStatus(string $symbol, $orderId = null, $origClientOrderId = null, array $params = [])
5347+
public function futuresOrderStatus(string $symbol, $orderId = null, $origClientOrderId = null, array $params = [], )
52445348
{
52455349
$request = [
52465350
'symbol' => $symbol,
@@ -5256,6 +5360,35 @@ public function futuresOrderStatus(string $symbol, $orderId = null, $origClientO
52565360
return $this->fapiRequest("v1/order", 'GET', array_merge($request, $params), true);
52575361
}
52585362

5363+
/**
5364+
* futuresAlgoOrderStatus gets the details of a futures algo order
5365+
*
5366+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-Algo-Order
5367+
*
5368+
* $order = $api->futuresAlgoOrderStatus("123456789");
5369+
*
5370+
* @param string $algoId (optional) order id to get the response for (mandatory if clientAlgoId is not set)
5371+
* @param string $clientAlgoId (optional) original client order id to get the response for (mandatory if algoId is not set)
5372+
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5373+
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
5374+
*
5375+
* @return array with error message or the order details
5376+
* @throws \Exception
5377+
*/
5378+
public function futuresAlgoOrderStatus($algoId = null, $clientAlgoId = null, array $params = [])
5379+
{
5380+
$request = [];
5381+
if ($algoId) {
5382+
$request['algoId'] = $algoId;
5383+
} else if ($clientAlgoId) {
5384+
$request['clientAlgoId'] = $clientAlgoId;
5385+
} else {
5386+
throw new \Exception('futuresAlgoOrderStatus(): either algoId or clientAlgoId must be set');
5387+
}
5388+
5389+
return $this->fapiRequest("v1/algoOrder", 'GET', array_merge($request, $params), true);
5390+
}
5391+
52595392
/**
52605393
* futuresAllOrders gets all orders for a symbol
52615394
* query time period must be less then 7 days (default as the recent 7 days)
@@ -5270,6 +5403,7 @@ public function futuresOrderStatus(string $symbol, $orderId = null, $origClientO
52705403
* @param int $limit (optional) limit the amount of orders (default 500, max 1000)
52715404
* @param string $orderId (optional) order id to get the response from (if is set it will get orders >= that orderId)
52725405
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5406+
* - @param bool $isAlgo (optional) true for algo orders
52735407
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
52745408
*
52755409
* @return array with error message or the orders details
@@ -5289,11 +5423,42 @@ public function futuresAllOrders(string $symbol, $startTime = null, $endTime = n
52895423
if ($limit) {
52905424
$request['limit'] = $limit;
52915425
}
5292-
if ($orderId) {
5293-
$request['orderId'] = $orderId;
5426+
if (isset($params['isAlgo']) && $params['isAlgo'] === true) {
5427+
unset($params['isAlgo']);
5428+
if ($orderId) {
5429+
$request['algoId'] = $orderId;
5430+
}
5431+
return $this->fapiRequest("v1/allAlgoOrders", 'GET', array_merge($request, $params), true);
5432+
} else {
5433+
if ($orderId) {
5434+
$request['orderId'] = $orderId;
5435+
}
5436+
return $this->fapiRequest("v1/allOrders", 'GET', array_merge($request, $params), true);
52945437
}
5438+
}
52955439

5296-
return $this->fapiRequest("v1/allOrders", 'GET', array_merge($request, $params), true);
5440+
/**
5441+
* futuresAllAlgoOrders gets all open orders for a symbol
5442+
*
5443+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-All-Algo-Orders
5444+
*
5445+
* $orders = $api->futuresAllAlgoOrders("BNBBTC");
5446+
*
5447+
* @param string $symbol (mandatory) market symbol (e.g. ETHUSDT)
5448+
* @param int $startTime (optional) timestamp in ms to get orders from INCLUSIVE
5449+
* @param int $endTime (optional) timestamp in ms to get orders until INCLUSIVE
5450+
* @param int $limit (optional) limit the amount of orders (default 500, max 1000)
5451+
* @param string $algoId (optional) order id to get the response from (if is set it will get orders >= that orderId)
5452+
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5453+
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
5454+
*
5455+
* @return array with error message or the orders details
5456+
* @throws \Exception
5457+
*/
5458+
public function futuresAllAlgoOrders(string $symbol, $startTime = null, $endTime = null, $limit = null, $algoId = null, array $params = [])
5459+
{
5460+
$params['isAlgo'] = true;
5461+
return $this->futuresAllOrders($symbol, $startTime, $endTime, $limit, $algoId, $params);
52975462
}
52985463

52995464
/**
@@ -5306,6 +5471,7 @@ public function futuresAllOrders(string $symbol, $startTime = null, $endTime = n
53065471
*
53075472
* @param string $symbol (optional) market symbol (e.g. ETHUSDT)
53085473
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5474+
* - @param bool $params['isAlgo'] (optional) true for getting algo orders
53095475
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
53105476
*
53115477
* @return array with error message or the orders details
@@ -5317,10 +5483,34 @@ public function futuresOpenOrders($symbol = null, array $params = [])
53175483
if ($symbol) {
53185484
$request['symbol'] = $symbol;
53195485
}
5320-
5486+
if (isset($params['isAlgo']) && ($params['isAlgo'] === true)) {
5487+
unset($params['algo']);
5488+
return $this->fapiRequest("v1/openAlgoOrders", 'GET', array_merge($request, $params), true);
5489+
}
53215490
return $this->fapiRequest("v1/openOrders", 'GET', array_merge($request, $params), true);
53225491
}
53235492

5493+
/**
5494+
* futuresOpenAlgoOrders gets all open orders for a symbol
5495+
*
5496+
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Current-All-Algo-Open-Orders
5497+
*
5498+
* $orders = $api->futuresOpenAlgoOrders();
5499+
* $orders = $api->futuresOpenAlgoOrders("BNBBTC");
5500+
*
5501+
* @param string $symbol (optional) market symbol (e.g. ETHUSDT)
5502+
* @param array $params (optional) An array of additional parameters that the API endpoint allows
5503+
* - @param int $params['recvWindow'] (optional) the time in milliseconds to wait for the response
5504+
*
5505+
* @return array with error message or the orders details
5506+
* @throws \Exception
5507+
*/
5508+
public function futuresOpenAlgoOrders($symbol = null, array $params = [])
5509+
{
5510+
$params['isAlgo'] = true;
5511+
return $this->futuresOpenOrders($symbol, $params);
5512+
}
5513+
53245514
/**
53255515
* futuresOpenOrder gets an open futures order
53265516
*

0 commit comments

Comments
 (0)