Ejemplo n.º 1
0
        public async Task HandleRiskManagerCommand(MatchingEngineRouteRisksCommand command)
        {
            try
            {
                switch (command.RequiredNotNull(nameof(command)).ActionType)
                {
                case RiskManagerActionType.BlockTradingForNewOrders:
                    await HandleRiskManagerBlockTradingCommand(command);

                    break;

                case RiskManagerActionType.ExternalExchangePassThrough:
                    await HandleRiskManagerHedgingCommand(command);

                    break;

                default:
                    throw new NotSupportedException($"Command of type [{command.ActionType}] from risk manager is not supported");
                }

                await _riskSystemCommandsLogRepository.AddProcessedAsync(command.ActionType.ToString(),
                                                                         command.ToJson());
            }
            catch (Exception e)
            {
                await _riskSystemCommandsLogRepository.AddErrorAsync(command.ActionType.ToString(), command.ToJson(),
                                                                     e.Message);

                throw;
            }
        }
Ejemplo n.º 2
0
        private async Task HandleRiskManagerBlockTradingCommand(MatchingEngineRouteRisksCommand command)
        {
            switch (command.Action)
            {
            case RiskManagerAction.On:

                var routes = FindRejectRoutes(command);

                if (routes.Any())
                {
                    await _riskSystemCommandsLogRepository.AddErrorAsync(command.ActionType.ToString(),
                                                                         command.ToJson(),
                                                                         $"Route already exists: {routes.ToJson()}");

                    await _log.WriteWarningAsync(nameof(MatchingEngineRoutesManager),
                                                 nameof(HandleRiskManagerBlockTradingCommand), routes.ToJson(),
                                                 $"Route already exists. Command from risk system is not processed: {command.ToJson()} ");

                    return;
                }

                var newRoute = new MatchingEngineRoute
                {
                    Id                   = Guid.NewGuid().ToString().ToUpper(),
                    Rank                 = command.Rank,
                    Asset                = command.Asset,
                    ClientId             = command.ClientId,
                    Instrument           = command.Instrument,
                    TradingConditionId   = command.TradingConditionId,
                    Type                 = GetOrderDirection(command.Direction),
                    MatchingEngineId     = MatchingEngineConstants.Reject,
                    RiskSystemLimitType  = command.LimitType,
                    RiskSystemMetricType = command.Type
                };

                await AddOrReplaceRouteAsync(newRoute);

                break;

            case RiskManagerAction.Off:

                var existingRoutes = FindRejectRoutes(command);

                if (existingRoutes.Length != 1)
                {
                    throw new Exception(
                              $"Cannot disable BlockTradingForNewOrders route for command: {command.ToJson()}. Existing routes found for command: {existingRoutes.ToJson()}");
                }

                await DeleteRouteAsync(existingRoutes[0].Id);

                break;

            default:
                throw new NotSupportedException($"Action [{command.Action}] from risk managment system is not supported.");
            }
        }
Ejemplo n.º 3
0
        private IMatchingEngineRoute[] FindRejectRoutes(MatchingEngineRouteRisksCommand command)
        {
            var allRoutes = _routesCacheService.GetRoutes();

            return(allRoutes.Where(r => r.Rank == command.Rank &&
                                   r.Asset == GetValueOrAny(command.Asset) &&
                                   r.ClientId == GetValueOrAny(command.ClientId) &&
                                   r.Instrument == GetValueOrAny(command.Instrument) &&
                                   r.TradingConditionId ==
                                   GetValueOrAny(command.TradingConditionId) &&
                                   r.Type == GetOrderDirection(command.Direction) &&
                                   r.MatchingEngineId == MatchingEngineConstants.Reject &&
                                   r.RiskSystemLimitType == command.LimitType &&
                                   r.RiskSystemMetricType == command.Type).ToArray());
        }
Ejemplo n.º 4
0
 private Task HandleRiskManagerHedgingCommand(MatchingEngineRouteRisksCommand command)
 {
     //TODO: change when 1 to 1 hedging will be implemented
     return(HandleRiskManagerBlockTradingCommand(command));
 }