Пример #1
0
        public async Task <BackendResponse <bool> > CloseOrder([FromBody] CloseOrderBackendRequest request)
        {
            if (!_ordersCache.ActiveOrders.TryGetOrderById(request.OrderId, out var order))
            {
                return(BackendResponse <bool> .Error("Order not found"));
            }

            if (_assetDayOffService.IsDayOff(order.Instrument))
            {
                return(BackendResponse <bool> .Error("Trades for instrument are not available"));
            }

            if (order.ClientId != request.ClientId || order.AccountId != request.AccountId)
            {
                return(BackendResponse <bool> .Error("Order is not available for user"));
            }

            if (request.IsForcedByBroker && string.IsNullOrEmpty(request.Comment))
            {
                return(BackendResponse <bool> .Error("For operation forced by broker, comment is mandatory"));
            }

            var reason = request.IsForcedByBroker ? OrderCloseReason.ClosedByBroker : OrderCloseReason.Close;

            order = await _tradingEngine.CloseActiveOrderAsync(request.OrderId, reason, request.Comment);

            var result = new BackendResponse <bool>
            {
                Result  = order.Status == OrderStatus.Closed || order.Status == OrderStatus.Closing,
                Message = order.CloseRejectReasonText
            };

            _consoleWriter.WriteLine(
                $"action order.close for clientId = {request.ClientId}, orderId = {request.OrderId}");
            _operationsLogService.AddLog("action order.close", request.ClientId, order.AccountId, request.ToJson(),
                                         result.ToJson());

            return(result);
        }
Пример #2
0
        public async Task <BackendResponse <bool> > CancelOrder([FromBody] CloseOrderBackendRequest request)
        {
            if (!_ordersCache.WaitingForExecutionOrders.TryGetOrderById(request.OrderId, out var order))
            {
                return(BackendResponse <bool> .Error("Order not found"));
            }

            if (_assetDayOffService.IsDayOff(order.Instrument))
            {
                return(BackendResponse <bool> .Error("Trades for instrument are not available"));
            }

            if (order.ClientId != request.ClientId || order.AccountId != request.AccountId)
            {
                return(BackendResponse <bool> .Error("Order is not available for user"));
            }

            if (request.IsForcedByBroker && string.IsNullOrEmpty(request.Comment))
            {
                return(BackendResponse <bool> .Error("For operation forced by broker, comment is mandatory"));
            }

            var reason = request.IsForcedByBroker ? OrderCloseReason.CanceledByBroker : OrderCloseReason.Canceled;

            _tradingEngine.CancelPendingOrder(order.Id, reason, request.Comment);

            var result = new BackendResponse <bool> {
                Result = true
            };

            _consoleWriter.WriteLine(
                $"action order.cancel for clientId = {request.ClientId}, orderId = {request.OrderId}");
            _operationsLogService.AddLog("action order.cancel", request.ClientId, order.AccountId, request.ToJson(),
                                         result.ToJson());

            return(result);
        }