예제 #1
0
        public async Task <PositionCloseResponse> CloseAsync([CanBeNull][FromRoute] string positionId,
                                                             [FromBody] PositionCloseRequest request = null,
                                                             [FromQuery] string accountId            = null)
        {
            if (!_ordersCache.Positions.TryGetPositionById(positionId, out var position))
            {
                throw new InvalidOperationException("Position not found");
            }

            ValidationHelper.ValidateAccountId(position, accountId);

            try
            {
                ValidateDayOff(position.AssetPairId);
            }
            catch (Exception e)
            {
                if (e.Message.Contains(CommonErrorCodes.InstrumentTradingDisabled))
                {
                    return(new PositionCloseResponse()
                    {
                        PositionId = positionId,
                        Result = PositionCloseResultContract.FailedToClose,
                        ErrorCode = CommonErrorCodes.InstrumentTradingDisabled,
                    });
                }
                throw;
            }

            var originator = GetOriginator(request?.Originator);

            var closeResult = await _tradingEngine.ClosePositionsAsync(
                new PositionsCloseData(
                    position,
                    position.AccountId,
                    position.AssetPairId,
                    position.OpenMatchingEngineId,
                    position.ExternalProviderId,
                    originator,
                    request?.AdditionalInfo,
                    position.EquivalentAsset), true);

            _operationsLogService.AddLog("action order.close", position.AccountId, request?.ToJson(),
                                         closeResult.ToJson());

            return(new PositionCloseResponse
            {
                PositionId = positionId,
                OrderId = closeResult.order?.Id,
                Result = closeResult.result.ToType <PositionCloseResultContract>()
            });
        }
예제 #2
0
        public async Task CloseAsync([CanBeNull][FromRoute] string positionId,
                                     [FromBody] PositionCloseRequest request = null)
        {
            if (!_ordersCache.Positions.TryGetPositionById(positionId, out var position))
            {
                throw new InvalidOperationException("Position not found");
            }

            ValidateDayOff(position.AssetPairId);

            var originator = GetOriginator(request?.Originator);

            var correlationId = request?.CorrelationId ?? _identityGenerator.GenerateGuid();

            var order = await _tradingEngine.ClosePositionsAsync(
                new PositionsCloseData(
                    new List <Position> {
                position
            },
                    position.AccountId,
                    position.AssetPairId,
                    position.Volume,
                    position.OpenMatchingEngineId,
                    position.ExternalProviderId,
                    originator,
                    request?.AdditionalInfo,
                    correlationId,
                    position.EquivalentAsset));

            if (order.Status != OrderStatus.Executed && order.Status != OrderStatus.ExecutionStarted)
            {
                throw new InvalidOperationException(order.RejectReasonText);
            }

            _operationsLogService.AddLog("action order.close", order.AccountId, request?.ToJson(),
                                         order.ToJson());
        }
예제 #3
0
        public async Task <PositionsGroupCloseResponse> CloseGroupAsync([FromQuery] string assetPairId = null,
                                                                        [FromQuery] string accountId   = null, [FromQuery] PositionDirectionContract?direction = null, [FromBody] PositionCloseRequest request = null)
        {
            var originator = GetOriginator(request?.Originator);

            var closeResult = await _tradingEngine.ClosePositionsGroupAsync(accountId, assetPairId, direction?.ToType <PositionDirection>(), originator, request?.AdditionalInfo);

            _operationsLogService.AddLog("Position liquidation started", string.Empty,
                                         $"instrument = [{assetPairId}], account = [{accountId}], direction = [{direction}], request = [{request.ToJson()}]",
                                         closeResult?.ToJson());

            return(new PositionsGroupCloseResponse
            {
                Responses = closeResult.Select(r => new PositionCloseResponse
                {
                    PositionId = r.Key,
                    Result = r.Value.Item1.ToType <PositionCloseResultContract>(),
                    OrderId = r.Value.Item2?.Id
                }).ToArray()
            });
        }
예제 #4
0
        public Task CloseGroupAsync([FromQuery] string assetPairId = null, [FromQuery] string accountId = null,
                                    [FromQuery] PositionDirectionContract?direction = null, [FromBody] PositionCloseRequest request = null)
        {
            if (string.IsNullOrWhiteSpace(accountId))
            {
                throw new ArgumentNullException(nameof(accountId), "AccountId must be set.");
            }

            var operationId = string.IsNullOrWhiteSpace(request?.CorrelationId)
                ? _identityGenerator.GenerateGuid()
                : request.CorrelationId;

            var originator = GetOriginator(request?.Originator);

            _cqrsSender.SendCommandToSelf(new StartLiquidationInternalCommand
            {
                OperationId     = operationId,
                CreationTime    = _dateService.Now(),
                AccountId       = accountId,
                AssetPairId     = assetPairId,
                Direction       = direction?.ToType <PositionDirection>(),
                QuoteInfo       = null,
                LiquidationType = LiquidationType.Forced,
                OriginatorType  = originator,
                AdditionalInfo  = request?.AdditionalInfo,
            });

            _operationsLogService.AddLog("Position liquidation started", string.Empty,
                                         $"instrument = [{assetPairId}], account = [{accountId}], direction = [{direction}], request = [{request.ToJson()}]",
                                         $"Started operation {operationId}");

            return(Task.CompletedTask);
        }