Skip to content

Commit 42fca10

Browse files
committed
Add AmendOrderAsync method to BinanceWebsocketClientSpotApiTrading
Reduce the quantity of an existing open order See https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#order-amend-keep-priority-trade
1 parent f6331af commit 42fca10

5 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
PUT
2+
/api/v3/order/amend/keepPriority
3+
true
4+
{
5+
"data": {
6+
"transactTime": 1684804350068,
7+
"executionId": 1234567890123456789,
8+
"amendedOrder": {
9+
"symbol": "BTCUSDT",
10+
"orderId": 33,
11+
"orderListId": -1,
12+
"origClientOrderId": "5xrgbMyg6z36NzBn2pbT8H",
13+
"clientOrderId": "PFaq6hIHxqFENGfdtn4J6Q",
14+
"price": "6.00000000",
15+
"qty": "5.00000000",
16+
"executedQty": "0.00000000",
17+
"preventedQty": "0.00000000",
18+
"quoteOrderQty": "0.00000000",
19+
"cumulativeQuoteQty": "0.00000000",
20+
"status": "NEW",
21+
"timeInForce": "GTC",
22+
"type": "LIMIT",
23+
"side": "SELL",
24+
"workingTime": 1741926410242,
25+
"selfTradePreventionMode": "NONE"
26+
}
27+
}
28+
}

Binance.Net.UnitTests/SocketRequestTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public async Task ValidateSpotTradingCalls()
100100
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.GetOrderAsync("ETHUSDT", 123), "GetOrder", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
101101
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.CancelOrderAsync("ETHUSDT", 123), "CancelOrder", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
102102
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.ReplaceOrderAsync("ETHUSDT", Enums.OrderSide.Buy, Enums.SpotOrderType.Limit, Enums.CancelReplaceMode.AllowFailure, 123), "ReplaceOrder", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
103+
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.AmendOrderAsync("ETHUSDT", 123, newQty: 0.5), "AmendOrder", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
103104
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.GetOpenOrdersAsync("ETHUSDT"), "GetOpenOrders", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
104105
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.CancelAllOrdersAsync("ETHUSDT"), "CancelAllOrders", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });
105106
await tester.ValidateAsync(CreateClient(), client => client.SpotApi.Trading.PlaceOcoOrderListAsync("ETHUSDT", Enums.OrderSide.Buy, 1, Enums.SpotOrderType.Limit, Enums.SpotOrderType.Limit), "PlaceOcoOrder", responseMapper: x => x.Result, nestedJsonProperty: "result", ignoreProperties: new List<string> { "" });

Binance.Net/Clients/SpotApi/BinanceSocketClientSpotApiTrading.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,45 @@ public async Task<CallResult<BinanceResponse<BinanceReplaceOrderResult>>> Replac
269269

270270
#endregion
271271

272+
#region Amend Order
273+
274+
/// <inheritdoc />
275+
public async Task<CallResult<BinanceResponse<BinanceAmendedOrderResult>>> AmendOrderAsync(string symbol, decimal newQuantity, long? orderId = null, string? clientOrderId = null, string? newClientOrderId = null, CancellationToken ct = default)
276+
{
277+
if (!orderId.HasValue && string.IsNullOrEmpty(clientOrderId))
278+
return new CallResult<BinanceResponse<BinanceAmendedOrderResult>>(ArgumentError.Invalid("Either orderId or clientOrderId must be provided", "Either orderId or clientOrderId must be provided"));
279+
280+
if (newClientOrderId != null)
281+
{
282+
newClientOrderId = LibraryHelpers.ApplyBrokerId(
283+
newClientOrderId,
284+
LibraryHelpers.GetClientReference(() => _client.ClientOptions.BrokerId, _client.Exchange, "Spot"),
285+
36,
286+
_client.ClientOptions.AllowAppendingClientOrderId);
287+
}
288+
289+
if (clientOrderId != null)
290+
{
291+
clientOrderId = LibraryHelpers.ApplyBrokerId(
292+
clientOrderId,
293+
LibraryHelpers.GetClientReference(() => _client.ClientOptions.BrokerId, _client.Exchange, "Spot"),
294+
36,
295+
_client.ClientOptions.AllowAppendingClientOrderId);
296+
}
297+
298+
var parameters = new ParameterCollection
299+
{
300+
{ "symbol", symbol },
301+
{ "newQty", newQuantity.ToString(CultureInfo.InvariantCulture) }
302+
};
303+
parameters.AddOptionalParameter("orderId", orderId?.ToString(CultureInfo.InvariantCulture));
304+
parameters.AddOptionalParameter("origClientOrderId", clientOrderId);
305+
parameters.AddOptionalParameter("newClientOrderId", newClientOrderId);
306+
return await _client.QueryAsync<BinanceAmendedOrderResult>(_client.ClientOptions.Environment.SpotSocketApiAddress.AppendPath("ws-api/v3"), $"order.amend.keepPriority", parameters, true, true, weight: 4, ct: ct).ConfigureAwait(false);
307+
}
308+
309+
#endregion
310+
272311
#region Get Open Orders
273312

274313
/// <inheritdoc />

Binance.Net/Converters/BinanceSourceGenerationContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ namespace Binance.Net.Converters
9595
[JsonSerializable(typeof(BinanceResponse<BinanceOrder>))]
9696
[JsonSerializable(typeof(BinanceResponse<BinanceOrder[]>))]
9797
[JsonSerializable(typeof(BinanceResponse<BinanceReplaceOrderResult>))]
98+
[JsonSerializable(typeof(BinanceResponse<BinanceAmendedOrderResult>))]
9899
[JsonSerializable(typeof(BinanceResponse<BinanceOrderOcoList>))]
99100
[JsonSerializable(typeof(BinanceResponse<BinanceOrderOcoList[]>))]
100101
[JsonSerializable(typeof(BinanceResponse<BinanceTrade[]>))]
@@ -386,6 +387,7 @@ namespace Binance.Net.Converters
386387
[JsonSerializable(typeof(Objects.Models.Spot.BinanceRecentTradeQuote[]))]
387388
[JsonSerializable(typeof(Objects.Models.Spot.BinanceRecentTradeBase[]))]
388389
[JsonSerializable(typeof(Objects.Models.Spot.BinanceReplaceOrderResult[]))]
390+
[JsonSerializable(typeof(Objects.Models.Spot.BinanceAmendedOrderResult[]))]
389391
[JsonSerializable(typeof(Objects.Models.Spot.BinanceSpotAccountSnapshot[]))]
390392
[JsonSerializable(typeof(Objects.Models.Spot.BinanceSpotFuturesTransfer[]))]
391393
[JsonSerializable(typeof(Objects.Models.Spot.BinanceSpotKline[]))]

Binance.Net/Interfaces/Clients/SpotApi/IBinanceSocketClientSpotApiTrading.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,18 @@ Task<CallResult<BinanceResponse<BinanceOrderOcoList>>> PlaceOcoOrderListAsync(
277277
/// <param name="ct">Cancellation token</param>
278278
/// <returns>Cancel-replace order response</returns>
279279
Task<CallResult<BinanceResponse<BinanceReplaceOrderResult>>> ReplaceOrderAsync(string symbol, OrderSide side, SpotOrderType type, CancelReplaceMode cancelReplaceMode, long? cancelOrderId = null, string? cancelClientOrderId = null, string? newCancelClientOrderId = null, string? newClientOrderId = null, decimal? quantity = null, decimal? quoteQuantity = null, decimal? price = null, TimeInForce? timeInForce = null, decimal? stopPrice = null, decimal? icebergQty = null, OrderResponseType? orderResponseType = null, int? trailingDelta = null, int? strategyId = null, int? strategyType = null, CancellationToken ct = default);
280+
281+
/// <summary>
282+
/// Amendments an existing order quantity
283+
/// <para><a href="https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/trading-requests#order-amend-keep-priority-trade" /></para>
284+
/// </summary>
285+
/// <param name="symbol">The symbol the order is for, for example `ETHUSDT`</param>
286+
/// <param name="newQuantity">The new quantity for the order</param>
287+
/// <param name="orderId">The order id to amend. Either this or clientOrderId should be provided</param>
288+
/// <param name="clientOrderId">The client order id to amend. Either this or orderId should be provided</param>
289+
/// <param name="newClientOrderId">New client order id for the amended order</param>
290+
/// <param name="ct">Cancellation token</param>
291+
/// <returns>Amended order response</returns>
292+
Task<CallResult<BinanceResponse<BinanceAmendedOrderResult>>> AmendOrderAsync(string symbol, decimal newQuantity, long? orderId = null, string? clientOrderId = null, string? newClientOrderId = null, CancellationToken ct = default);
280293
}
281294
}

0 commit comments

Comments
 (0)