Skip to content

Commit f6331af

Browse files
committed
Add AmendOrderAsync method to BinanceRestClientSpotApiTrading
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 bb500a8 commit f6331af

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

Binance.Net.UnitTests/RestRequestTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public async Task ValidateSpotTradingCalls()
160160
await tester.ValidateAsync(client => client.SpotApi.Trading.CancelOrderAsync("ETHUSDT", 123), "CancelOrder");
161161
await tester.ValidateAsync(client => client.SpotApi.Trading.CancelAllOrdersAsync("ETHUSDT"), "CancelAllOrders");
162162
await tester.ValidateAsync(client => client.SpotApi.Trading.ReplaceOrderAsync("ETHUSDT", Enums.OrderSide.Sell, Enums.SpotOrderType.Limit, Enums.CancelReplaceMode.AllowFailure, 123, quantity: 1), "ReplaceOrder", nestedJsonProperty: "data");
163+
await tester.ValidateAsync(client => client.SpotApi.Trading.AmendOrderAsync("ETHUSDT", 123, newQty: 0.5), "AmendOrder");
163164
await tester.ValidateAsync(client => client.SpotApi.Trading.GetOrderAsync("ETHUSDT", 123), "GetOrder");
164165
await tester.ValidateAsync(client => client.SpotApi.Trading.GetOpenOrdersAsync("ETHUSDT"), "GetOpenOrders");
165166
await tester.ValidateAsync(client => client.SpotApi.Trading.GetOrdersAsync("ETHUSDT"), "GetOrders");

Binance.Net/Clients/SpotApi/BinanceRestClientSpotApiTrading.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,46 @@ public async Task<WebCallResult<BinanceReplaceOrderResult>> ReplaceOrderAsync(st
308308
}
309309
#endregion
310310

311+
#region Amend Order
312+
313+
/// <inheritdoc />
314+
public async Task<WebCallResult<BinanceAmendedOrderResult>> AmendOrderAsync(
315+
string symbol,
316+
decimal newQty,
317+
long? orderId = null,
318+
string? origClientOrderId = null,
319+
string? newClientOrderId = null,
320+
int? receiveWindow = null,
321+
CancellationToken ct = default)
322+
{
323+
if (!orderId.HasValue && string.IsNullOrEmpty(origClientOrderId))
324+
throw new ArgumentException("Either orderId or origClientOrderId must be sent");
325+
326+
if (newClientOrderId != null)
327+
{
328+
newClientOrderId = LibraryHelpers.ApplyBrokerId(
329+
newClientOrderId,
330+
LibraryHelpers.GetClientReference(() => _baseClient.ClientOptions.BrokerId, _baseClient.Exchange, "Spot"),
331+
36,
332+
_baseClient.ClientOptions.AllowAppendingClientOrderId);
333+
}
334+
335+
var parameters = new ParameterCollection
336+
{
337+
{ "symbol", symbol },
338+
{ "newQty", newQty.ToString(CultureInfo.InvariantCulture) }
339+
};
340+
parameters.AddOptionalParameter("orderId", orderId?.ToString(CultureInfo.InvariantCulture));
341+
parameters.AddOptionalParameter("origClientOrderId", origClientOrderId);
342+
parameters.AddOptionalParameter("newClientOrderId", newClientOrderId);
343+
parameters.AddOptionalParameter("recvWindow", receiveWindow?.ToString(CultureInfo.InvariantCulture) ?? _baseClient.ClientOptions.ReceiveWindow.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
344+
345+
var request = _definitions.GetOrCreate(HttpMethod.Put, "api/v3/order/amend/keepPriority", BinanceExchange.RateLimiter.SpotRestIp, 4, true);
346+
return await _baseClient.SendAsync<BinanceAmendedOrderResult>(request, parameters, ct).ConfigureAwait(false);
347+
}
348+
349+
#endregion
350+
311351
#region Query Order
312352

313353
/// <inheritdoc />

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,32 @@ Task<WebCallResult<BinanceReplaceOrderResult>> ReplaceOrderAsync(string symbol,
197197
int? receiveWindow = null,
198198
CancellationToken ct = default);
199199

200+
/// <summary>
201+
/// Amends an existing open order (reduces quantity)
202+
/// <para>
203+
/// Docs:<br />
204+
/// <a href="https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#order-amend-keep-priority-trade" /><br />
205+
/// Endpoint:<br />
206+
/// PUT /api/v3/order/amend/keepPriority
207+
/// </para>
208+
/// </summary>
209+
/// <param name="symbol">["<c>symbol</c>"] The symbol the order is for, for example `ETHUSDT`</param>
210+
/// <param name="orderId">["<c>orderId</c>"] The order id of the order. Either this or origClientOrderId should be provided</param>
211+
/// <param name="origClientOrderId">["<c>origClientOrderId</c>"] The client order id of the order. Either this or orderId should be provided</param>
212+
/// <param name="newClientOrderId">["<c>newClientOrderId</c>"] The new client order id for the order after being amended</param>
213+
/// <param name="newQuantity">["<c>newQuantity</c>"] The new quantity (must be greater than 0 and less than the order's quantity)</param>
214+
/// <param name="receiveWindow">The receive window for which this request is active</param>
215+
/// <param name="ct">Cancellation token</param>
216+
/// <returns>The amended order details</returns>
217+
Task<WebCallResult<BinanceAmendedOrderResult>> AmendOrderAsync(
218+
string symbol,
219+
decimal newQuantity,
220+
long? orderId = null,
221+
string? origClientOrderId = null,
222+
string? newClientOrderId = null,
223+
int? receiveWindow = null,
224+
CancellationToken ct = default);
225+
200226
/// <summary>
201227
/// Retrieves data for a specific order. Either orderId or origClientOrderId should be provided.
202228
/// <para>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Binance.Net.Objects.Models.Spot
2+
{
3+
/// <summary>
4+
/// The result of amending an order
5+
/// </summary>
6+
[SerializationModel]
7+
public record BinanceAmendedOrderResult
8+
{
9+
/// <summary>
10+
/// ["<c>transactTime</c>"] Transaction time
11+
/// </summary>
12+
[JsonPropertyName("transactTime")]
13+
public DateTime TransactTime { get; set; }
14+
15+
/// <summary>
16+
/// ["<c>executionId</c>"] Execution ID
17+
/// </summary>
18+
[JsonPropertyName("executionId")]
19+
public long ExecutionId { get; set; }
20+
21+
/// <summary>
22+
/// ["<c>amendedOrder</c>"] The amended order details
23+
/// </summary>
24+
[JsonPropertyName("amendedOrder")]
25+
public BinanceOrder AmendedOrder { get; set; } = default!;
26+
}
27+
}

0 commit comments

Comments
 (0)