private async Task TakeProfitForExistingPositionAsync()
        {
            var command = OrderExtensions.NewLimitOrder(CommonFuncs.NewClOrdId("stop-order"), "XBTUSD", Side.Sell, 1M, MarginAccountId, 10000);

            command.ForPosition(12345);
            await _wsClient.SendCommandAsync(command).ConfigureAwait(false);
        }
        private async Task SltpGroupAsync(CancellationToken cancellationToken)
        {
            var id      = CommonFuncs.NewClOrdId("limit-sltp-1");
            var command = OrderExtensions.NewLimitOrder(id, "XBTUSD", Side.Sell, 1M, MarginAccountId, 10600);

            command.AddTrailingStopLoss(500);
            command.AddTakeProfit(10000);

            var executionReport = await _restClient.NewOrderAsync(command, cancellationToken).ConfigureAwait(false);

            HandleOrderReport(executionReport);

            // OR
            executionReport = await _restClient.NewLimitOrderAsync(
                CommonFuncs.NewClOrdId("limit-sltp-2"),
                "XBTUSD",
                Side.Sell,
                1M,
                MarginAccountId,
                10600,
                trailingOffset : 500,
                takeProfitPrice : 10000,
                cancellationToken : cancellationToken).ConfigureAwait(false);

            HandleOrderReport(executionReport);
        }
        private async Task TakeProfitForExistingPositionAsync(CancellationToken cancellationToken)
        {
            var id      = CommonFuncs.NewClOrdId("stop-order");
            var command = OrderExtensions.NewLimitOrder(id, "XBTUSD", Side.Sell, 1M, MarginAccountId, 10000);

            command.ForPosition(12345);

            var executionReport = await _restClient.NewOrderAsync(command, cancellationToken).ConfigureAwait(false);

            HandleOrderReport(executionReport);
        }
Exemple #4
0
        public async Task Test_NewOrder()
        {
            var command = OrderExtensions.NewLimitOrder(NewClOrdId("limit"), "XBTUSD", Side.Sell, 1M,
                                                        MarginAccountId, 10000M);

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

            er.Should().NotBeNull();
            er.Should().NotBeNull();
            er.ExecType.Should().Be(ExecType.PendingNewExec);
        }
        private async Task SltpGroupAsync()
        {
            var command = OrderExtensions.NewLimitOrder(CommonFuncs.NewClOrdId("limit-sltp"), "XBTUSD", Side.Sell, 1M, MarginAccountId, 10600);

            command.AddTrailingStopLoss(500);
            command.AddTakeProfit(10000);
            await _wsClient.SendCommandAsync(command).ConfigureAwait(false);

            await _wsClient.NewLimitOrderAsync(
                CommonFuncs.NewClOrdId("limit-sltp"),
                "XBTUSD",
                Side.Sell,
                1M,
                MarginAccountId,
                10600,
                trailingOffset : 500,
                takeProfitPrice : 10000).ConfigureAwait(false);
        }
Exemple #6
0
        public async Task Test_CancelOrder()
        {
            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 cancelOrderCmd      = er.ToOrderCancelRequest(NewClOrdId("cancel"));
            var cancelOrderResponse = await _restClient.CancelOrderAsync(cancelOrderCmd, _token).ConfigureAwait(false);

            cancelOrderResponse.Should().NotBeNull();
            cancelOrderResponse.Should().NotBeNull();
            cancelOrderResponse.ExecType.Should().Be(ExecType.CanceledExec);
        }
Exemple #7
0
        public async Task Test_ReplaceOrderSingle()
        {
            var newOrderCmd = OrderExtensions.NewLimitOrder(NewClOrdId("limit"), "XBTUSD", Side.Sell, 1M,
                                                            MarginAccountId, 10000M);

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

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

            var replaceOrderCmd = newOrderEr.ToOrderCancelReplaceRequest(NewClOrdId("cancel"));

            replaceOrderCmd.Price = 10500M.ToFixString();
            var replaceOrderEr = await _restClient.ReplaceOrderAsync(replaceOrderCmd, _token).ConfigureAwait(false);

            replaceOrderEr.Should().NotBeNull();
            replaceOrderEr.Should().NotBeNull();
            replaceOrderEr.ExecType.Should().Be(ExecType.PendingReplaceExec);
        }
        private async Task InfinitePlaceCancelAsync()
        {
            _wsClient.RemoveListener <ExecutionReport>();
            _wsClient.RemoveListener <BalanceIncrementalRefresh>();
            var placeInterval = TimeSpan.FromMilliseconds(500);

            _wsClient.Listen <ExecutionReport>(async(client, er) =>
            {
                _logger.LogInformation($"ER {er.ClOrdId} ExecType {er.ExecType}");
                if (er.ExecType != ExecType.NewExec)
                {
                    return;
                }

                var cancelCommand = er.ToOrderCancelRequest(CommonFuncs.NewClOrdId("cancel-order"));
                try
                {
                    await client.SendCommandAsync(cancelCommand).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Failed to cancel order {er.ClOrdId}: {ex.Message}");
                }
            });

            while (true)
            {
                var limitCommand = OrderExtensions.NewLimitOrder(CommonFuncs.NewClOrdId("limit-order"), "BTC/USDT", Side.Sell, 0.01M, SpotAccountId, 10500);
                try
                {
                    await _wsClient.SendCommandAsync(limitCommand).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Failed to place limit order: {ex.Message}");
                }
                await Task.Delay(placeInterval).ConfigureAwait(false);
            }
        }
        private async Task LimitOrderAsync()
        {
            await _wsClient.NewLimitOrderAsync(
                CommonFuncs.NewClOrdId("limit-order"),
                "BTC/USDT",
                Side.Sell,
                0.01M,
                SpotAccountId,
                10500,
                text : "order comment 1")
            .ConfigureAwait(false);

            var command = OrderExtensions.NewLimitOrder(
                CommonFuncs.NewClOrdId("limit-order"),
                "BTC/USDT",
                Side.Sell,
                0.01M,
                SpotAccountId,
                10500,
                text: "order comment 2");

            await _wsClient.SendCommandAsync(command).ConfigureAwait(false);
        }
        /// <inheritdoc />
        public async Task NewLimitOrderAsync(
            string clOrdId,
            string symbol,
            string side,
            decimal orderQty,
            ulong account,
            decimal price,
            string timeInForce      = null,
            string[] execInst       = null,
            ulong positionId        = 0,
            decimal stopLossPrice   = 0,
            decimal takeProfitPrice = 0,
            decimal trailingOffset  = 0,
            decimal capPrice        = 0,
            string text             = null,
            string groupId          = null)
        {
            var command = OrderExtensions.NewLimitOrder(
                clOrdId,
                symbol,
                side,
                orderQty,
                account,
                price,
                timeInForce,
                execInst,
                positionId,
                stopLossPrice,
                takeProfitPrice,
                trailingOffset,
                capPrice,
                text,
                groupId);

            await SendCommandAsync(command).ConfigureAwait(false);
        }
        /// <inheritdoc />
        public async Task <ExecutionReport> NewLimitOrderAsync(string clOrdId,
                                                               string symbol,
                                                               string side,
                                                               decimal orderQty,
                                                               ulong account,
                                                               decimal price,
                                                               string timeInForce                  = null,
                                                               string[] execInst                   = null,
                                                               ulong positionId                    = 0,
                                                               decimal stopLossPrice               = 0,
                                                               decimal takeProfitPrice             = 0,
                                                               decimal trailingOffset              = 0,
                                                               decimal capPrice                    = 0,
                                                               string text                         = null,
                                                               string groupId                      = null,
                                                               CancellationToken cancellationToken = default)
        {
            var command = OrderExtensions.NewLimitOrder(
                clOrdId,
                symbol,
                side,
                orderQty,
                account,
                price,
                timeInForce,
                execInst,
                positionId,
                stopLossPrice,
                takeProfitPrice,
                trailingOffset,
                capPrice,
                text,
                groupId);

            return(await NewOrderAsync(command, cancellationToken).ConfigureAwait(false));
        }