Skip to content

BitMEX: added support for amending orders in bulk #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/ExchangeSharp/API/Exchanges/BitMEX/ExchangeBitMEXAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public async Task CancelAllOrdersAsync(string marketSymbol = null)
JToken token = await MakeJsonRequestAsync<JToken>(query, BaseUrl, payload, "DELETE");
}

public async Task Deadman(int timeoutMS)
public async Task DeadmanAsync(int timeoutMS)
{
Dictionary<string, object> payload = await GetNoncePayloadAsync();
payload["timeout"] = timeoutMS;
Expand All @@ -611,7 +611,7 @@ protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrd
return ParseOrder(token);
}

protected override async Task<ExchangeOrderResult[]> OnPlaceOrdersAsync(params ExchangeOrderRequest[] orders)
private async Task<ExchangeOrderResult[]> PlaceOrdersAsync(string requestMethod, params ExchangeOrderRequest[] orders)
{
List<ExchangeOrderResult> results = new List<ExchangeOrderResult>();
Dictionary<string, object> payload = await GetNoncePayloadAsync();
Expand All @@ -623,23 +623,39 @@ protected override async Task<ExchangeOrderResult[]> OnPlaceOrdersAsync(params E
orderRequests.Add(subPayload);
}
payload["orders"] = orderRequests;
JToken token = await MakeJsonRequestAsync<JToken>("/order/bulk", BaseUrl, payload, "POST");
JToken token = await MakeJsonRequestAsync<JToken>("/order/bulk", BaseUrl, payload, requestMethod);
foreach (JToken orderResultToken in token)
{
results.Add(ParseOrder(orderResultToken));
}
return results.ToArray();
}

protected override async Task<ExchangeOrderResult[]> OnPlaceOrdersAsync(params ExchangeOrderRequest[] orders)
{
return await PlaceOrdersAsync("POST", orders);
}

public async Task<ExchangeOrderResult[]> AmendOrdersAsync(params ExchangeOrderRequest[] orders)
{
return await PlaceOrdersAsync("PUT", orders);
}

private void AddOrderToPayload(ExchangeOrderRequest order, Dictionary<string, object> payload)
{
payload["symbol"] = order.MarketSymbol;
payload["ordType"] = order.OrderType.ToStringInvariant();
payload["side"] = order.IsBuy ? "Buy" : "Sell";
payload["orderQty"] = order.Amount;

if(order.OrderId != null)
payload["orderID"] = order.OrderId;

if(order.ClientOrderId != null)
payload["clOrdID"] = order.ClientOrderId;

if(order.OrderType!=OrderType.Market)
payload["price"] = order.Price;
payload["price"] = order.Price;

if (order.ExtraParameters.TryGetValue("execInst", out var execInst))
{
Expand Down Expand Up @@ -696,6 +712,7 @@ private ExchangeOrderResult ParseOrder(JToken token)
IsBuy = token["side"].ToStringInvariant().EqualsWithOption("Buy"),
OrderDate = token["transactTime"].ConvertInvariant<DateTime>(),
OrderId = token["orderID"].ToStringInvariant(),
ClientOrderId = token["clOrdID"].ToStringInvariant(),
MarketSymbol = token["symbol"].ToStringInvariant()
};

Expand Down
10 changes: 10 additions & 0 deletions src/ExchangeSharp/Model/ExchangeOrderRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public class ExchangeOrderRequest
/// </summary>
public bool IsMargin { get; set; }

/// <summary>Order id</summary>
public string OrderId { get; set; }

/// <summary>
/// Client Order id
/// Order IDs put here will be returned in the Order Result returned by the exchange
/// Not all exchanges support this
/// </summary>
public string ClientOrderId { get; set; }

/// <summary>
/// Whether the amount should be rounded - set to false if you know the exact amount, otherwise leave
/// as true so that the exchange does not reject the order due to too many decimal places.
Expand Down
7 changes: 7 additions & 0 deletions src/ExchangeSharp/Model/ExchangeOrderResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public sealed class ExchangeOrderResult
/// <summary>Order id</summary>
public string OrderId { get; set; }

/// <summary>
/// Client Order id
/// Order IDs put here in the Request will be returned by the exchange
/// Not all exchanges support this
/// </summary>
public string ClientOrderId { get; set; }

/// <summary>Result of the order</summary>
public ExchangeAPIOrderResult Result { get; set; }

Expand Down