Skip to content

Commit ddc7138

Browse files
authored
added ability to CancelOrder() by ClientOrderId (#734)
- many, but not all, exchanges support this
1 parent ddfb36e commit ddc7138

29 files changed

+98
-68
lines changed

src/ExchangeSharp/API/Exchanges/Aquanow/ExchangeAquanowAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
233233
return orderDetails;
234234
}
235235

236-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
236+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
237237
{
238+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
238239
var payload = await GetNoncePayloadAsync();
239240
payload["orderId"] = orderId;
240241
JToken token = await MakeJsonRequestAsync<JToken>("/trades/v1/order", null, payload, "DELETE");

src/ExchangeSharp/API/Exchanges/BL3P/ExchangeBL3PAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,11 @@ protected override async Task<ExchangeOrderBook> OnGetOrderBookAsync(string mark
292292
return ConvertToExchangeOrderBook(maxCount, bl3pOrderBook);
293293
}
294294

295-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
295+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
296296
{
297297
if (string.IsNullOrWhiteSpace(marketSymbol))
298298
throw new ArgumentException("Value cannot be null or whitespace.", nameof(marketSymbol));
299+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
299300

300301
var resultBody = await MakeRequestAsync(
301302
$"/{marketSymbol}/money/order/cancel",

src/ExchangeSharp/API/Exchanges/BTSE/ExchangeBTSEAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(strin
6767
this.ParseCandle(token, marketSymbol, periodSeconds, 1, 2, 3, 4, 0, TimestampType.UnixMilliseconds, 5));
6868
}
6969

70-
protected override async Task OnCancelOrderAsync(string orderId, string? marketSymbol = null)
70+
protected override async Task OnCancelOrderAsync(string orderId, string? marketSymbol = null, bool isClientOrderId = false)
7171
{
7272
var payload = await GetNoncePayloadAsync();
7373

src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,18 @@ private async Task<IEnumerable<ExchangeOrderResult>> OnGetMyTradesAsync(string?
733733
return trades;
734734
}
735735

736-
protected override async Task OnCancelOrderAsync(string orderId, string? marketSymbol = null)
736+
protected override async Task OnCancelOrderAsync(string orderId, string? marketSymbol = null, bool isClientOrderId = false)
737737
{
738738
Dictionary<string, object> payload = await GetNoncePayloadAsync();
739739
if (string.IsNullOrWhiteSpace(marketSymbol))
740740
{
741741
throw new ArgumentNullException("Binance cancel order request requires symbol");
742742
}
743743
payload["symbol"] = marketSymbol!;
744-
payload["orderId"] = orderId;
744+
if (isClientOrderId) // Either orderId or origClientOrderId must be sent.
745+
payload["origClientOrderId"] = orderId;
746+
else
747+
payload["orderId"] = orderId;
745748
var token = await MakeJsonRequestAsync<JToken>("/order", BaseUrlApi, payload, "DELETE");
746749
var cancelledOrder = ParseOrder(token);
747750
if (cancelledOrder.OrderId != orderId)

src/ExchangeSharp/API/Exchanges/BitBank/ExchangeBitBankAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
141141
return ParseOrder(token);
142142
}
143143

144-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
144+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
145145
{
146+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
146147
Dictionary<string, object> payload = await GetNoncePayloadAsync();
147148
if (marketSymbol == null)
148149
throw new APIException("Bitbank requries market symbol when cancelling orders");

src/ExchangeSharp/API/Exchanges/BitMEX/ExchangeBitMEXAPI.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,10 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
614614
return orders[0];
615615
}
616616

617-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
617+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
618618
{
619619
Dictionary<string, object> payload = await GetNoncePayloadAsync();
620-
payload["orderID"] = orderId;
620+
payload[isClientOrderId ? "clOrdID" : "orderID"] = orderId;
621621
JToken token = await MakeJsonRequestAsync<JToken>("/order", BaseUrl, payload, "DELETE");
622622
}
623623

src/ExchangeSharp/API/Exchanges/Bitfinex/ExchangeBitfinexAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,9 @@ protected override Task<IWebSocket> OnGetCompletedOrderDetailsWebSocketAsync(Act
569569
});
570570
}
571571

572-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
572+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
573573
{
574+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
574575
Dictionary<string, object> payload = await GetNoncePayloadAsync();
575576
payload["order_id"] = orderId.ConvertInvariant<long>();
576577
var token = await MakeJsonRequestAsync<JToken>("/order/cancel", BaseUrlV1, payload);

src/ExchangeSharp/API/Exchanges/Bitstamp/ExchangeBitstampAPI.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,10 @@ protected override async Task<IEnumerable<ExchangeOrderResult>> OnGetCompletedOr
487487
return orders2;
488488
}
489489

490-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
491-
{
492-
if (string.IsNullOrWhiteSpace(orderId))
490+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
491+
{
492+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
493+
if (string.IsNullOrWhiteSpace(orderId))
493494
{
494495
throw new APIException("OrderId is needed for canceling order");
495496
}

src/ExchangeSharp/API/Exchanges/Bittrex/ExchangeBittrexAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ protected override async Task<IEnumerable<ExchangeOrderResult>> OnGetCompletedOr
456456
return orders;
457457
}
458458

459-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
459+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
460460
{
461+
if (isClientOrderId) throw new NotSupportedException("Cancelling by client order ID is not supported in ExchangeSharp. Please submit a PR if you are interested in this feature");
461462
await MakeJsonRequestAsync<JToken>("/orders/" + orderId, null, await GetNoncePayloadAsync(), "DELETE");
462463
}
463464

src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitAPI.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,13 @@ protected override async Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string
778778
}
779779
}
780780

781-
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null)
781+
protected override async Task OnCancelOrderAsync(string orderId, string marketSymbol = null, bool isClientOrderId = false)
782782
{
783783
var extraParams = new Dictionary<string, object>();
784-
extraParams["order_id"] = orderId;
784+
if (isClientOrderId)
785+
extraParams["order_link_id"] = orderId;
786+
else
787+
extraParams["order_id"] = orderId;
785788
if (!string.IsNullOrWhiteSpace(marketSymbol))
786789
{
787790
extraParams["symbol"] = marketSymbol;

0 commit comments

Comments
 (0)