Skip to content

Commit

Permalink
Added SpotApiV2.Trading.PlaceMultipleOrdersAsync, PlaceMultipleStopOr…
Browse files Browse the repository at this point in the history
…dersAsync, CancelOrdersAsync and CancelStopOrdersAsync batch endpoints
  • Loading branch information
JKorf committed Aug 19, 2024
1 parent 991859a commit 9d6493f
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 0 deletions.
78 changes: 78 additions & 0 deletions CoinEx.Net/Clients/SpotApiV2/CoinExRestClientSpotApiTrading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System;
using CoinEx.Net.Interfaces.Clients.SpotApiV2;
using CryptoExchange.Net;
using System.Collections.Generic;
using System.Linq;

namespace CoinEx.Net.Clients.SpotApiV2
{
Expand Down Expand Up @@ -92,6 +94,48 @@ public async Task<WebCallResult<CoinExStopId>> PlaceStopOrderAsync(
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExBatchOrderResult>>> PlaceMultipleOrdersAsync(
IEnumerable<CoinExPlaceOrderRequest> requests,
CancellationToken ct = default)
{
foreach(var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);

var parameters = new ParameterCollection()
{
{ "orders", requests }
};
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExBatchOrderResult>>(_baseClient.GetUri("v2/spot/batch-order"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
{
foreach(var order in result.Data.Where(x => x.Success))
_baseClient.InvokeOrderPlaced(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.Id.ToString(), SourceObject = order });
}
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExStopId>>>> PlaceMultipleStopOrdersAsync(
IEnumerable<CoinExPlaceStopOrderRequest> requests,
CancellationToken ct = default)
{
foreach (var order in requests)
order.ClientOrderId ??= ExchangeHelpers.AppendRandomString("x-" + _baseClient._brokerId + "-", 32);

var parameters = new ParameterCollection()
{
{ "orders", requests }
};
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExBatchResult<CoinExStopId>>>(_baseClient.GetUri("v2/spot/batch-stop-order"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
{
foreach (var order in result.Data.Where(x => x.Success))
_baseClient.InvokeOrderPlaced(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.Data!.StopOrderId.ToString(), SourceObject = order });
}
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<CoinExOrder>> GetOrderAsync(string symbol, long orderId, CancellationToken ct = default)
{
Expand Down Expand Up @@ -224,6 +268,23 @@ public async Task<WebCallResult<CoinExOrder>> CancelOrderAsync(string symbol, Ac
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExOrder>>>> CancelOrdersAsync(string symbol, IEnumerable<long> orderIds, CancellationToken ct = default)
{
var parameters = new ParameterCollection()
{
{ "market", symbol },
{ "order_ids", orderIds }
};
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExBatchResult<CoinExOrder>>>(_baseClient.GetUri("v2/spot/cancel-batch-order"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
{
foreach(var order in result.Data)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.Data!.Id.ToString(), SourceObject = result.Data });
}
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderAsync(string symbol, AccountType accountType, long stopOrderId, CancellationToken ct = default)
{
Expand Down Expand Up @@ -269,6 +330,23 @@ public async Task<WebCallResult<CoinExStopOrder>> CancelStopOrderByClientOrderId
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExBatchResult<CoinExStopOrder>>>> CancelStopOrdersAsync(string symbol, IEnumerable<long> orderIds, CancellationToken ct = default)
{
var parameters = new ParameterCollection()
{
{ "market", symbol },
{ "stop_ids", orderIds }
};
var result = await _baseClient.ExecuteAsync<IEnumerable<CoinExBatchResult<CoinExStopOrder>>>(_baseClient.GetUri("v2/spot/cancel-batch-stop-order"), HttpMethod.Post, ct, parameters, true).ConfigureAwait(false);
if (result)
{
foreach (var order in result.Data)
_baseClient.InvokeOrderCanceled(new CryptoExchange.Net.CommonObjects.OrderId { Id = order.Data!.StopOrderId.ToString(), SourceObject = result.Data });
}
return result;
}

/// <inheritdoc />
public async Task<WebCallResult<CoinExPaginated<CoinExUserTrade>>> GetUserTradesAsync(string symbol, AccountType accountType, OrderSide? side = null, DateTime? startTime = null, DateTime? endTime = null, int? page = null, int? pageSize = null, CancellationToken ct = default)
{
Expand Down
211 changes: 211 additions & 0 deletions CoinEx.Net/CoinEx.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d6493f

Please sign in to comment.