private async Task CancelOrdersAsync(CancellationToken cancellationToken)
        {
            for (var i = 0; i < 3; i++)
            {
                var executionReport = await _restClient.NewLimitOrderAsync(
                    CommonFuncs.NewClOrdId($"limit-{i}"),
                    "BTC/USDT",
                    Side.Buy,
                    0.01M,
                    SpotAccountId,
                    10000,
                    cancellationToken : cancellationToken)
                                      .ConfigureAwait(false);

                if (executionReport.ExecType != ExecType.NewExec)
                {
                    continue;
                }

                switch (i)
                {
                case 0:
                    var cancelResponse1 = await _restClient.CancelOrderByOrderIdAsync(
                        CommonFuncs.NewClOrdId("cancel-1"),
                        executionReport.OrderId,
                        "BTC/USDT",
                        Side.Buy,
                        SpotAccountId,
                        cancellationToken : cancellationToken).ConfigureAwait(false);

                    HandleOrderReport(cancelResponse1);
                    break;

                case 1:
                    // Actually it is possible to cancel order by client id at any time. No need to wait for ExecutionReport confirmation.
                    var cancelResponse2 = await _restClient.CancelOrderByClOrdIdAsync(
                        CommonFuncs.NewClOrdId("cancel-2"),
                        executionReport.ClOrdId,
                        "BTC/USDT",
                        Side.Buy,
                        SpotAccountId,
                        cancellationToken : cancellationToken).ConfigureAwait(false);

                    HandleOrderReport(cancelResponse2);
                    break;

                case 2:
                    var cancelCommand   = executionReport.ToOrderCancelRequest(CommonFuncs.NewClOrdId("cancel-3"));
                    var cancelResponse3 = await _restClient.CancelOrderAsync(cancelCommand, cancellationToken)
                                          .ConfigureAwait(false);

                    HandleOrderReport(cancelResponse3);
                    break;
                }
            }
        }
Example #2
0
        public async Task Test_CancelOrderByClOrdId()
        {
            var newOrderCmd = OrderExtensions.NewLimitOrder(NewClOrdId("limit"), "XBTUSD", Side.Sell, 1M,
                                                            MarginAccountId, 10000M);

            var er = await _restClient.NewOrderAsync(newOrderCmd, _token).ConfigureAwait(false);

            er.Should().NotBeNull();
            er.Should().NotBeNull();
            er.ExecType.Should().Be(ExecType.PendingNewExec);

            var cancelOrderResponse = await _restClient.CancelOrderByClOrdIdAsync(
                NewClOrdId("cancel-2"),
                er.ClOrdId,
                "XBTUSD",
                Side.Sell,
                MarginAccountId,
                cancellationToken : _token).ConfigureAwait(false);

            cancelOrderResponse.Should().NotBeNull();
            cancelOrderResponse.Should().NotBeNull();
            cancelOrderResponse.ExecType.Should().Be(ExecType.CanceledExec);
        }