Esempio n. 1
0
        private async Task HandleTradingDisabled(ProductContract product, string username)
        {
            if (product.IsTradingDisabled)
            {
                _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                               $"Trading disabled for product {product.ProductId}");
                var allRfq = await RetrieveAllRfq(product.ProductId, canBePaused : true);

                _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                               $"Found rfqs to pause: {allRfq.Select(x => x.Id).ToJson()}");

                foreach (var rfq in allRfq)
                {
                    _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                                   $"Trying to pause rfq: {rfq.Id}");
                    await _rfqPauseService.AddAsync(rfq.Id,
                                                    PauseSource.TradingDisabled,
                                                    username);
                }
            }
            else
            {
                _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                               $"Trading enabled for product {product.ProductId}");
                var allRfq = await RetrieveAllRfq(product.ProductId, canBeResumed : true, canBeStopped : true);

                _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                               $"Found rfqs to resume or stop: {allRfq.Select(x => x.Id).ToJson()}");

                foreach (var rfq in allRfq)
                {
                    if (rfq.PauseSummary?.CanBeResumed ?? false)
                    {
                        _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                                       $"Trying to resume rfq: {rfq.Id}");
                        await _rfqPauseService.ResumeAsync(rfq.Id,
                                                           PauseCancellationSource.TradingEnabled,
                                                           username);
                    }
                    else if (rfq.PauseSummary?.CanBeStopped ?? false)
                    {
                        _log.WriteInfo(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                                       $"Trying to stop pending pause for rfq: {rfq.Id}");
                        await _rfqPauseService.StopPendingAsync(rfq.Id,
                                                                PauseCancellationSource.TradingEnabled,
                                                                username);
                    }
                    else
                    {
                        _log.WriteWarning(nameof(ProductChangedProjection), nameof(HandleTradingDisabled),
                                          $"Unexpected state for rfq: {rfq.Id}, {rfq.ToJson()}");
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task Handle(PriceForSpecialLiquidationCalculatedEvent e, ICommandSender sender)
        {
            var executionInfo = await _operationExecutionInfoRepository.GetAsync <SpecialLiquidationOperationData>(
                operationName : OperationName,
                id : e.OperationId);

            if (executionInfo?.Data == null)
            {
                return;
            }

            var pause = await _rfqPauseService.GetCurrentAsync(e.OperationId);

            if (pause?.State == PauseState.Active)
            {
                return;
            }

            if (executionInfo.Data.SwitchState(SpecialLiquidationOperationState.PriceRequested,
                                               SpecialLiquidationOperationState.PriceReceived))
            {
                //validate that volume didn't changed to peek either to execute order or request the price again
                var currentVolume = GetNetPositionCloseVolume(executionInfo.Data.PositionIds, executionInfo.Data.AccountId);
                if (currentVolume != 0 && currentVolume != executionInfo.Data.Volume)
                {
                    // if RFQ is paused we will not continue
                    var pauseAcknowledged = await _rfqPauseService.AcknowledgeAsync(e.OperationId);

                    if (pauseAcknowledged)
                    {
                        return;
                    }

                    executionInfo.Data.RequestNumber++;
                    executionInfo.Data.Volume = currentVolume;

                    RequestPrice(sender, executionInfo);

                    await _operationExecutionInfoRepository.Save(executionInfo);

                    return;//wait for the new price
                }

                await _rfqPauseService.StopPendingAsync(e.OperationId, PauseCancellationSource.PriceReceived, nameof(SpecialLiquidationSaga));

                executionInfo.Data.Price = e.Price;

                //execute order in Gavel by API
                sender.SendCommand(new ExecuteSpecialLiquidationOrderCommand
                {
                    OperationId  = e.OperationId,
                    CreationTime = _dateService.Now(),
                    Instrument   = executionInfo.Data.Instrument,
                    Volume       = executionInfo.Data.Volume,
                    Price        = e.Price,
                }, _cqrsContextNamesSettings.TradingEngine);

                _chaosKitty.Meow(e.OperationId);

                await _operationExecutionInfoRepository.Save(executionInfo);
            }
        }