/// <summary> /// The collate subscriptions. /// </summary> /// <param name="execution"> /// The execution. /// </param> /// <param name="ruleParameters"> /// The rule parameters. /// </param> /// <param name="operationContext"> /// The operation context. /// </param> /// <param name="dataRequestSubscriber"> /// The data request subscriber. /// </param> /// <param name="judgementService"> /// The judgement service. /// </param> /// <param name="alertStream"> /// The alert stream. /// </param> /// <returns> /// The <see cref="IUniverseRule"/>. /// </returns> public IReadOnlyCollection <IUniverseRule> CollateSubscriptions( ScheduledExecution execution, RuleParameterDto ruleParameters, ISystemProcessOperationContext operationContext, IUniverseDataRequestsSubscriber dataRequestSubscriber, IJudgementService judgementService, IUniverseAlertStream alertStream) { if (!execution.Rules?.Select(_ => _.Rule)?.Contains(Rules.PlacingOrderWithNoIntentToExecute) ?? true) { return(new IUniverseRule[0]); } var filteredParameters = execution.Rules.SelectMany(_ => _.Ids).Where(_ => _ != null).ToList(); var dtos = ruleParameters.PlacingOrders .Where(_ => filteredParameters.Contains(_.Id, StringComparer.InvariantCultureIgnoreCase)).ToList(); var placingOrderParameters = this.ruleParameterMapper.Map(execution, dtos); var subscriptions = this.SubscribeToUniverse( execution, operationContext, alertStream, placingOrderParameters, dataRequestSubscriber); return(subscriptions); }
/// <summary> /// The build subscriptions. /// </summary> /// <param name="execution"> /// The execution. /// </param> /// <param name="player"> /// The player. /// </param> /// <param name="alertStream"> /// The alert stream. /// </param> /// <param name="dataRequestSubscriber"> /// The data request subscriber. /// </param> /// <param name="judgementService"> /// The judgement service. /// </param> /// <param name="operationContext"> /// The operation context. /// </param> /// <param name="ruleParameters"> /// The rule parameters. /// </param> /// <param name="args"> /// The args. /// </param> /// <returns> /// The <see cref="IUniverseRule"/>. /// </returns> private IReadOnlyCollection <IUniverseRule> BuildSubscriptions( ScheduledExecution execution, IUniversePlayer player, IUniverseAlertStream alertStream, IUniverseDataRequestsSubscriber dataRequestSubscriber, IJudgementService judgementService, ISystemProcessOperationContext operationContext, RuleParameterDto ruleParameters, params Func <ScheduledExecution, RuleParameterDto, ISystemProcessOperationContext, IUniverseDataRequestsSubscriber, IJudgementService, IUniverseAlertStream, IReadOnlyCollection <IUniverseRule> >[] args) { var universeRules = new List <IUniverseRule>(); foreach (var func in args) { var result = func.Invoke( execution, ruleParameters, operationContext, dataRequestSubscriber, judgementService, alertStream); universeRules.AddRange(result); } return(universeRules); }
/// <summary> /// The collate subscriptions. /// </summary> /// <param name="execution"> /// The execution. /// </param> /// <param name="ruleParameters"> /// The rule parameters. /// </param> /// <param name="operationContext"> /// The operation context. /// </param> /// <param name="dataRequestSubscriber"> /// The data request subscriber. /// </param> /// <param name="judgementService"> /// The judgement service. /// </param> /// <param name="alertStream"> /// The alert stream. /// </param> /// <returns> /// The <see cref="IUniverseEvent"/>. /// </returns> public IReadOnlyCollection <IUniverseRule> CollateSubscriptions( ScheduledExecution execution, RuleParameterDto ruleParameters, ISystemProcessOperationContext operationContext, IUniverseDataRequestsSubscriber dataRequestSubscriber, IJudgementService judgementService, IUniverseAlertStream alertStream) { if (!execution.Rules?.Select(ru => ru.Rule).Contains(Rules.FixedIncomeWashTrades) ?? true) { return(new IUniverseRule[0]); } var filteredParameters = execution.Rules.SelectMany(ru => ru.Ids).Where(ru => ru != null).ToList(); var dtos = ruleParameters.FixedIncomeWashTrades.Where( wt => filteredParameters.Contains(wt.Id, StringComparer.InvariantCultureIgnoreCase)).ToList(); var fixedIncomeWashTradeParameters = this.ruleParameterMapper.Map(execution, dtos); var subscriptions = this.SubscribeToUniverse( execution, operationContext, alertStream, dataRequestSubscriber, fixedIncomeWashTradeParameters); return(subscriptions); }
public void LeadingTimespan_Returns_Value_When_TwoAlternatives_WashTrade() { var calculator = new RuleParameterAdjustedTimespanService(); var dto = new RuleParameterDto { WashTrades = new[] { new WashTradeRuleParameterDto { WindowSize = TimeSpan.FromMinutes(8) }, new WashTradeRuleParameterDto { WindowSize = TimeSpan.FromMinutes(11) }, new WashTradeRuleParameterDto { WindowSize = TimeSpan.FromMinutes(2) } } }; var result = calculator.LeadingTimespan(dto); Assert.AreEqual(result, TimeSpan.FromMinutes(11)); }
public void TrailingTimespanThreeDaysIsTimespanThreeDays() { var service = this.BuildService(); var dto = new RuleParameterDto { HighProfits = new HighProfitsRuleParameterDto[] { new HighProfitsRuleParameterDto { ForwardWindow = TimeSpan.FromDays(2) }, new HighProfitsRuleParameterDto { ForwardWindow = TimeSpan.FromDays(3) }, new HighProfitsRuleParameterDto { ForwardWindow = TimeSpan.FromDays(1) } }, }; var trail = service.TrailingTimeSpan(dto); Assert.AreEqual(TimeSpan.FromDays(3), trail); }
public void LeadingTimespan_Returns_Zero_For_Null_Collections() { var calculator = new RuleParameterAdjustedTimespanService(); var dto = new RuleParameterDto(); var result = calculator.LeadingTimespan(dto); Assert.AreEqual(result, TimeSpan.Zero); }
public TimeSpan TrailingTimeSpan(RuleParameterDto dto) { var forwardWindows = dto?.HighProfits?.Select(_ => _.ForwardWindow)?.ToList() ?? new List<TimeSpan>(); if (!forwardWindows.Any()) { return TimeSpan.Zero; } return forwardWindows.Max(_ => _); }
public void LeadingTimespanIsTimespanTwelveDays() { var service = this.BuildService(); var dto = new RuleParameterDto { CancelledOrders = new[] { new CancelledOrderRuleParameterDto { WindowSize = TimeSpan.FromDays(1) } }, HighProfits = new[] { new HighProfitsRuleParameterDto { WindowSize = TimeSpan.FromDays(2) } }, MarkingTheCloses = new[] { new MarkingTheCloseRuleParameterDto { WindowSize = TimeSpan.FromDays(3) } }, Spoofings = new[] { new SpoofingRuleParameterDto { WindowSize = TimeSpan.FromDays(4) } }, Layerings = new[] { new LayeringRuleParameterDto { WindowSize = TimeSpan.FromDays(5) } }, HighVolumes = new[] { new HighVolumeRuleParameterDto { WindowSize = TimeSpan.FromDays(6) } }, WashTrades = new[] { new WashTradeRuleParameterDto { WindowSize = TimeSpan.FromDays(7) } }, Rampings = new[] { new RampingRuleParameterDto { WindowSize = TimeSpan.FromDays(8) } }, PlacingOrders = new[] { new PlacingOrdersWithNoIntentToExecuteRuleParameterDto { WindowSize = TimeSpan.FromDays(9) } }, FixedIncomeWashTrades = new[] { new FixedIncomeWashTradeRuleParameterDto { WindowSize = TimeSpan.FromDays(10) } }, FixedIncomeHighProfits = new[] { new FixedIncomeHighProfitRuleParameterDto { WindowSize = TimeSpan.FromDays(11) } }, FixedIncomeHighVolumeIssuance = new[] { new FixedIncomeHighVolumeRuleParameterDto { WindowSize = TimeSpan.FromDays(12) } } }; var trail = service.LeadingTimespan(dto); Assert.AreEqual(TimeSpan.FromDays(12), trail); }
public void LeadingTimespan_Returns_Value_When_Only_Layering() { var calculator = new RuleParameterAdjustedTimespanService(); var dto = new RuleParameterDto { Layerings = new[] { new LayeringRuleParameterDto { WindowSize = TimeSpan.FromMinutes(9) } } }; var result = calculator.LeadingTimespan(dto); Assert.AreEqual(result, TimeSpan.FromMinutes(9)); }
public TimeSpan LeadingTimespan(RuleParameterDto dto) { if (dto == null) { return TimeSpan.Zero; } var cancelledOrders = dto.CancelledOrders?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var highProfit = dto.HighProfits?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var markingTheClose = dto.MarkingTheCloses?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var spoofing = dto.Spoofings?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var layering = dto.Layerings?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var highVolume = dto.HighVolumes?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var washTrade = dto.WashTrades?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var ramping = dto.Rampings?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var placingOrders = dto.PlacingOrders?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var fixedIncomeWashTrades = dto.FixedIncomeWashTrades?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var fixedIncomeHighProfit = dto.FixedIncomeHighProfits?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var fixedIncomeHighVolumeIssuance = dto.FixedIncomeHighVolumeIssuance?.OfType<IIdentifiableRule>().ToList() ?? new List<IIdentifiableRule>(); var identifiableRuleList = cancelledOrders .Concat(highProfit) .Concat(markingTheClose) .Concat(spoofing) .Concat(layering) .Concat(highVolume) .Concat(washTrade) .Concat(ramping) .Concat(placingOrders) .Concat(fixedIncomeHighProfit) .Concat(fixedIncomeWashTrades) .Concat(fixedIncomeHighVolumeIssuance) .Where(_ => _ != null) .ToList(); if (!identifiableRuleList.Any()) { return TimeSpan.Zero; } var date = identifiableRuleList.Max(i => i.WindowSize); return date; }
public void LeadingTimespan_Returns_Value_When_Only_Cancelled() { var calculator = new RuleParameterAdjustedTimespanService(); var dto = new RuleParameterDto { CancelledOrders = new[] { new CancelledOrderRuleParameterDto { WindowSize = TimeSpan.FromMinutes(5) } } }; var result = calculator.LeadingTimespan(dto); Assert.AreEqual(result, TimeSpan.FromMinutes(5)); }
public void TrailingTimespanThreeDaysOnlyInCancelledOrderIsTimespanThreeDays() { var service = this.BuildService(); var dto = new RuleParameterDto { CancelledOrders = new CancelledOrderRuleParameterDto[] { new CancelledOrderRuleParameterDto { WindowSize = TimeSpan.FromDays(3) } } }; var trail = service.TrailingTimeSpan(dto); Assert.AreEqual(TimeSpan.FromDays(0), trail); }
public IReadOnlyCollection <string> ExtractIds(RuleParameterDto dtos) { if (dtos == null) { return(new string[0]); } var cancelledOrderIds = dtos.CancelledOrders?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var highProfitIds = dtos.HighProfits?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var markingTheCloseIds = dtos.MarkingTheCloses?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var spoofingIds = dtos.Spoofings?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var layeringIds = dtos.Layerings?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var highVolumeIds = dtos.HighVolumes?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var washTradeIds = dtos.WashTrades?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var rampingIds = dtos.Rampings?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var placingOrderWithNoIntentionToExecute = dtos.PlacingOrders?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var fixedIncomeHighProfitIds = dtos.FixedIncomeHighProfits?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var fixedIncomeHighVolumeIssuanceIds = dtos.FixedIncomeHighVolumeIssuance?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var fixedIncomeWashTradeIds = dtos.FixedIncomeWashTrades?.Select(_ => _.Id)?.ToList() ?? new List <string>(); var response = new List <string>(); response.AddRange(cancelledOrderIds); response.AddRange(highProfitIds); response.AddRange(markingTheCloseIds); response.AddRange(spoofingIds); response.AddRange(layeringIds); response.AddRange(highVolumeIds); response.AddRange(washTradeIds); response.AddRange(rampingIds); response.AddRange(placingOrderWithNoIntentionToExecute); response.AddRange(fixedIncomeHighProfitIds); response.AddRange(fixedIncomeHighVolumeIssuanceIds); response.AddRange(fixedIncomeWashTradeIds); return(response); }
/// <summary> /// The subscribe rules. /// </summary> /// <param name="execution"> /// The execution. /// </param> /// <param name="player"> /// The player. /// </param> /// <param name="alertStream"> /// The alert stream. /// </param> /// <param name="dataRequestSubscriber"> /// The data request subscriber. /// </param> /// <param name="judgementService"> /// The judgement service. /// </param> /// <param name="operationContext"> /// The operation context. /// </param> /// <param name="ruleParameters"> /// The rule parameters. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public async Task <IUniverseRuleSubscriptionSummary> SubscribeRules( ScheduledExecution execution, IUniversePlayer player, IUniverseAlertStream alertStream, IUniverseDataRequestsSubscriber dataRequestSubscriber, IJudgementService judgementService, ISystemProcessOperationContext operationContext, RuleParameterDto ruleParameters) { if (execution == null || player == null) { this.logger.LogInformation("received null execution or player. Returning"); return(new UniverseRuleSubscriptionSummary(null, null)); } var subscriptions = this.BuildSubscriptions( execution, player, alertStream, dataRequestSubscriber, judgementService, operationContext, ruleParameters, this.highVolumeEquitySubscriber.CollateSubscriptions, this.washTradeEquitySubscriber.CollateSubscriptions, this.highProfitEquitySubscriber.CollateSubscriptions, this.cancelledOrderEquitySubscriber.CollateSubscriptions, this.markingTheCloseEquitySubscriber.CollateSubscriptions, this.spoofingEquitySubscriber.CollateSubscriptions, this.rampingEquitySubscriber.CollateSubscriptions, this.placingOrdersEquitySubscriber.CollateSubscriptions, this.washTradeFixedIncomeSubscriber.CollateSubscriptions, this.highVolumeFixedIncomeSubscriber.CollateSubscriptions, this.highProfitFixedIncomeSubscriber.CollateSubscriptions); foreach (var subscription in subscriptions) { if (subscription == null) { continue; } this.logger.LogInformation($"Subscribe Rules subscribing a {subscription.Rule.GetDescription()}"); player.Subscribe(subscription); } var ids = this.idExtractor.ExtractIds(ruleParameters); if (ids == null || !ids.Any()) { this.logger.LogError("did not have any ids successfully extracted from the rule parameters"); return(new UniverseRuleSubscriptionSummary(null, null)); } var jointIds = ids.Aggregate((a, b) => $"{a} {b}"); this.logger.LogInformation($"subscriber processed for the following ids {jointIds}"); return(await Task.FromResult(new UniverseRuleSubscriptionSummary(ids, subscriptions))); }
/// <summary> /// The schedule rule. /// </summary> /// <param name="execution"> /// The execution. /// </param> /// <param name="parameters"> /// The parameters. /// </param> /// <param name="ruleContext"> /// The rule context. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> private async Task ScheduleRule( ScheduledExecution execution, RuleParameterDto parameters, ISystemProcessOperationDistributeRuleContext ruleContext) { foreach (var rule in execution.Rules.Where(ru => ru != null)) { switch (rule.Rule) { case Rules.CancelledOrders: var cancelledOrderRuleRuns = parameters.CancelledOrders?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, cancelledOrderRuleRuns, rule, ruleContext); break; case Rules.HighProfits: var highProfitRuleRuns = parameters.HighProfits?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, highProfitRuleRuns, rule, ruleContext); break; case Rules.HighVolume: var highVolumeRuleRuns = parameters.HighVolumes?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, highVolumeRuleRuns, rule, ruleContext); break; case Rules.MarkingTheClose: var markingTheCloseRuleRuns = parameters.MarkingTheCloses?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, markingTheCloseRuleRuns, rule, ruleContext); break; case Rules.WashTrade: var washTradeRuleRuns = parameters.WashTrades?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, washTradeRuleRuns, rule, ruleContext); break; case Rules.UniverseFilter: break; case Rules.Spoofing: var spoofingRuleRuns = parameters.Spoofings?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, spoofingRuleRuns, rule, ruleContext); break; case Rules.PlacingOrderWithNoIntentToExecute: var placingOrderRuleRuns = parameters.PlacingOrders?.Select(_ => _ as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, placingOrderRuleRuns, rule, ruleContext); break; case Rules.Layering: // var layeringRuleRuns = parameters.Layerings?.Select(co => co as IIdentifiableRule)?.ToList(); // await ScheduleRuleRuns(execution, layeringRuleRuns, rule, ruleCtx); break; case Rules.FixedIncomeHighProfits: var fixedIncomeHighProfits = parameters.FixedIncomeHighProfits ?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, fixedIncomeHighProfits, rule, ruleContext); break; case Rules.FixedIncomeHighVolumeIssuance: var fixedIncomeHighVolumeIssuance = parameters.FixedIncomeHighVolumeIssuance ?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, fixedIncomeHighVolumeIssuance, rule, ruleContext); break; case Rules.FixedIncomeWashTrades: var fixedIncomeWashTrade = parameters.FixedIncomeWashTrades ?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, fixedIncomeWashTrade, rule, ruleContext); break; case Rules.Ramping: var ramping = parameters.Rampings?.Select(co => co as IIdentifiableRule)?.ToList(); await this.ScheduleRuleRuns(execution, ramping, rule, ruleContext); break; default: this.logger.LogError( $"{rule.Rule} was scheduled but not recognised by the Schedule Rule method in distributed rule."); ruleContext.EventError( $"{rule.Rule} was scheduled but not recognised by the Schedule Rule method in distributed rule."); break; } } }