Skip to content

Commit fb52096

Browse files
committed
editOrder added
1 parent 9e7029e commit fb52096

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

php-binance-api.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,44 @@ protected function createSpotOrderRequest(string $side, string $symbol, $quantit
19681968
return array_merge($request, $params);
19691969
}
19701970

1971+
/**
1972+
* editOrder - edits a quantity of an existing order
1973+
*
1974+
* @link https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#order-amend-keep-priority-trade
1975+
* @link https://developers.binance.com/docs/binance-spot-api-docs/faqs/order_amend_keep_priority
1976+
*
1977+
* @param string $symbol (mandatory) market symbol
1978+
* @param string $quantity (mandatory) new quantity (must be greater than 0 and less than the original order quantity)
1979+
* @param string $orderId (optional) order id to be amended (mandatory if origClientOrderId is not set)
1980+
* @param string $origClientOrderId (optional) original client order id to be amended (mandatory if orderId is not set)
1981+
* @param array $params (optional) additional transaction options
1982+
* - @param string $params['newClientOrderId'] - custom client order id
1983+
*
1984+
* @return array containing the response
1985+
* @throws \Exception
1986+
*/
1987+
public function editOrder(string $symbol, $quantity, ?string $orderId = null, ?string $origClientOrderId = null, array $params = [])
1988+
{
1989+
$request = [
1990+
"symbol" => $symbol,
1991+
"newQty" => $quantity,
1992+
];
1993+
1994+
if (!is_null($orderId)) {
1995+
$request['orderId'] = $orderId;
1996+
} else if (is_null($origClientOrderId)) {
1997+
throw new \Exception('editOrder(): Either orderId or origClientOrderId must be set');
1998+
} else {
1999+
$request['origClientOrderId'] = $origClientOrderId;
2000+
}
2001+
if (isset($params['newClientOrderId'])) {
2002+
$request['newClientOrderId'] = $params['newClientOrderId'];
2003+
} else {
2004+
$request['newClientOrderId'] = $this->generateSpotClientOrderId();
2005+
}
2006+
return $this->apiRequest("v3/order/amend/keepPriority", "PUT", array_merge($request, $params), true);
2007+
}
2008+
19712009
/**
19722010
* sorOrder creates an order using the SOR endpoint
19732011
*

tests/BinanceStaticTests.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ public function testSpotReplaceOrder()
164164
$this->assertTrue(str_starts_with($params['newClientOrderId'], $this->SPOT_ORDER_PREFIX));
165165
}
166166

167+
public function testSpotEditOrder()
168+
{
169+
try {
170+
$this->binance->editOrder('ETHUSDT', 0.009, '123456789');
171+
} catch(\Throwable $e) {
172+
173+
}
174+
$endpoint = "https://api.binance.com/api/v3/order/amend/keepPriority?";
175+
$this->assertTrue(str_starts_with(self::$capturedUrl, $endpoint));
176+
177+
$queryString = substr(self::$capturedUrl, strlen($endpoint));
178+
parse_str($queryString, $params);
179+
180+
$this->assertEquals("ETHUSDT", $params['symbol']);
181+
$this->assertEquals(0.009, $params['newQty']);
182+
$this->assertEquals('123456789', $params['orderId']);
183+
$this->assertTrue(str_starts_with($params['newClientOrderId'], $this->SPOT_ORDER_PREFIX));
184+
}
185+
167186
public function testSpotBuy()
168187
{
169188
try {

0 commit comments

Comments
 (0)