Beispiel #1
0
 public void Setup()
 {
     this.queueCasePublisher                 = A.Fake <IQueueCasePublisher>();
     this.ruleBreachRepository               = A.Fake <IRuleBreachRepository>();
     this.ruleBreach                         = A.Fake <IRuleBreach>();
     this.ruleBreachOrdersRepository         = A.Fake <IRuleBreachOrdersRepository>();
     this.ruleBreachToRuleBreachOrdersMapper = A.Fake <IRuleBreachToRuleBreachOrdersMapper>();
     this.ruleBreachToRuleBreachMapper       = A.Fake <IRuleBreachToRuleBreachMapper>();
     this.logger = new NullLogger <RuleViolationService>();
 }
        public RuleBreach RuleBreachItem(IRuleBreach ruleBreach)
        {
            var oldestPosition = ruleBreach.Trades?.Get()?.Min(tr => tr.MostRecentDateEvent());
            var latestPosition = ruleBreach.Trades?.Get()?.Max(tr => tr.MostRecentDateEvent());
            var venue          = ruleBreach.Trades?.Get()?.FirstOrDefault()?.Market?.Name;

            if (oldestPosition == null)
            {
                oldestPosition = latestPosition;
            }

            if (latestPosition == null)
            {
                latestPosition = oldestPosition;
            }

            var oldestPositionValue = oldestPosition ?? DateTime.UtcNow;
            var latestPositionValue = latestPosition ?? DateTime.UtcNow;

            if (ruleBreach.UniverseDateTime < oldestPositionValue)
            {
                oldestPositionValue = ruleBreach.UniverseDateTime;
            }

            if (ruleBreach.UniverseDateTime > latestPositionValue)
            {
                latestPositionValue = ruleBreach.UniverseDateTime;
            }

            var trades = ruleBreach.Trades?.Get()?.Select(io => io.ReddeerOrderId).Where(x => x.HasValue)
                         .Select(y => y.Value).ToList();

            var ruleBreachObj = new RuleBreach(
                null,
                ruleBreach.RuleParameterId,
                ruleBreach.CorrelationId,
                ruleBreach.IsBackTestRun,
                DateTime.UtcNow,
                ruleBreach.CaseTitle ?? string.Empty,
                ruleBreach.Description ?? string.Empty,
                venue,
                oldestPositionValue,
                latestPositionValue,
                ruleBreach.Security.Cfi,
                ruleBreach.Security.Identifiers.ReddeerEnrichmentId,
                ruleBreach.SystemOperationId,
                (int?)ruleBreach?.FactorValue?.OrganisationalFactors ?? 0,
                ruleBreach?.FactorValue?.Value ?? string.Empty,
                ruleBreach.RuleParameters.TunedParameters != null,
                trades);

            return(ruleBreachObj);
        }
        public void AddRuleViolation(IRuleBreach ruleBreach)
        {
            if (ruleBreach == null)
            {
                this._logger?.LogError("received a null rule breach in add rule violations");
                return;
            }

            lock (this._lock)
            {
                this._ruleViolations.Push(ruleBreach);
            }
        }
        private void SetIsBackTest(IRuleBreach ruleBreach)
        {
            if (ruleBreach == null)
            {
                return;
            }

            ruleBreach.IsBackTestRun = this._isBackTest;

            if (this._isBackTest)
            {
                this._logger.LogInformation("noting that alert is part of a back test and setting property");
            }
        }
        public IReadOnlyCollection <RuleBreachOrder> ProjectToOrders(IRuleBreach ruleBreach, string ruleBreachId)
        {
            if (string.IsNullOrWhiteSpace(ruleBreachId))
            {
                return(new RuleBreachOrder[0]);
            }

            if (ruleBreach == null)
            {
                return(new RuleBreachOrder[0]);
            }

            var ruleBreachOrders = ruleBreach.Trades.Get().Where(i => i != null)
                                   .Select(i => new RuleBreachOrder(ruleBreachId, i.ReddeerOrderId?.ToString())).ToList();

            return(ruleBreachOrders);
        }
        protected async Task Send(IRuleBreach ruleBreach, string description)
        {
            if (ruleBreach?.Trades?.Get() == null)
            {
                this.Logger.LogInformation($"{this._messageSenderName} had null trades. Returning.");
                return;
            }

            this.Logger.LogInformation(
                $"received message to send for {this._messageSenderName} | security {ruleBreach.Security.Name}");

            // Save the rule breach
            if (string.IsNullOrWhiteSpace(ruleBreach.CaseTitle))
            {
                ruleBreach.CaseTitle = this._caseTitle;
            }

            if (string.IsNullOrWhiteSpace(ruleBreach.Description))
            {
                ruleBreach.Description = description;
            }

            var ruleBreachItem = this._ruleBreachToRuleBreachMapper.RuleBreachItem(ruleBreach);
            var ruleBreachId   = await this._ruleBreachRepository.Create(ruleBreachItem);

            if (ruleBreachId == null)
            {
                this.Logger.LogError(
                    $"{this._messageSenderName} encountered an error saving the case message. Will not transmit to bus");
                return;
            }

            // Save the rule breach orders
            var ruleBreachOrderItems =
                this._ruleBreachToRuleBreachOrdersMapper.ProjectToOrders(ruleBreach, ruleBreachId?.ToString());

            await this._ruleBreachOrdersRepository.Create(ruleBreachOrderItems);

            // Check for duplicates
            if (ruleBreach.IsBackTestRun)
            {
                var hasBackTestDuplicates = await this._ruleBreachRepository.HasDuplicateBackTest(
                    ruleBreachId?.ToString(),
                    ruleBreach.CorrelationId);

                if (hasBackTestDuplicates)
                {
                    this.Logger.LogInformation(
                        $"was going to send for {this._messageSenderName} | security {ruleBreach.Security.Name} | rule breach {ruleBreachId} but detected duplicate back test case creation");

                    return;
                }
            }

            var hasDuplicates = await this._ruleBreachRepository.HasDuplicate(ruleBreachId?.ToString());

            if (hasDuplicates && !ruleBreach.IsBackTestRun)
            {
                this.Logger.LogInformation(
                    $"was going to send for {this._messageSenderName} | security {ruleBreach.Security.Name} | rule breach {ruleBreachId} but detected duplicate case creation");
                return;
            }

            if (ruleBreach.RuleParameters.TunedParameters != null)
            {
                this.Logger.LogInformation(
                    $"was going to send for {this._messageSenderName} | security {ruleBreach.Security.Name} | rule breach {ruleBreachId} but detected run was a tuning run");
                return;
            }

            var caseMessage = new CaseMessage {
                RuleBreachId = ruleBreachId.GetValueOrDefault(0)
            };

            try
            {
                this.Logger.LogInformation(
                    $"about to send for {this._messageSenderName} | security {ruleBreach.Security.Name}");
                await this._queueCasePublisher.Send(caseMessage);

                this.Logger.LogInformation($"sent for {this._messageSenderName} | security {ruleBreach.Security.Name}");
            }
            catch (Exception e)
            {
                this.Logger.LogError(
                    $"{this._messageSenderName} encountered an error sending the case message to the bus {e}");
            }
        }
 public RuleViolationIdPair(long ruleViolationId, IRuleBreach ruleBreach)
 {
     this.RuleViolationId = ruleViolationId;
     this.RuleBreach      = ruleBreach ?? throw new ArgumentNullException(nameof(ruleBreach));
 }