private async IAsyncEnumerable <ITradingInstrument> GetTradingInstruments(
            IReadOnlyList <Product> products,
            string tradingConditionId = null)
        {
            var clientProfiles = tradingConditionId == null
                ? await _clientProfilesRepository.GetAllAsync()
                : new[] { await _clientProfilesRepository.GetByIdAsync(tradingConditionId) };

            var assetTypes = products.Select(p => p.AssetType).Distinct().ToList();

            foreach (var clientProfile in clientProfiles)
            {
                var availableClientProfileSettingsList =
                    await _clientProfileSettingsRepository.GetAllAsync(clientProfile.Id, assetTypes, true);

                foreach (var product in products)
                {
                    var clientProfileSettings = availableClientProfileSettingsList.SingleOrDefault(x =>
                                                                                                   x.ClientProfileId == clientProfile.Id && x.AssetTypeId == product.AssetType);

                    if (clientProfileSettings == null)
                    {
                        continue;
                    }

                    var underlying = _underlyingsCache.GetByMdsCode(product.UnderlyingMdsCode);

                    yield return(TradingInstrument.CreateFromProduct(
                                     product,
                                     clientProfileSettings.ClientProfileId,
                                     clientProfileSettings.Margin,
                                     underlying?.Spread ?? _defaultTradingInstrumentSettings.Spread));
                }
            }
        }
Beispiel #2
0
        public async Task <IReadOnlyList <ITradingCondition> > GetAsync()
        {
            var profiles = await _clientProfilesRepository.GetAllAsync();

            var settlementCurrency = await _settlementCurrencyService.GetSettlementCurrencyAsync();

            return(profiles.Select(x => MapTradingCondition(x, settlementCurrency)).ToList());
        }
Beispiel #3
0
        public async Task InsertAsync(AssetTypeWithTemplate model, string username, string correlationId)
        {
            var brokerSettingsResponse = await _brokerSettingsApi.GetByIdAsync(_brokerId);

            if (brokerSettingsResponse.ErrorCode == BrokerSettingsErrorCodesContract.BrokerSettingsDoNotExist)
            {
                throw new BrokerSettingsDoNotExistException();
            }

            await ValidateUnderlyingCategory(model.UnderlyingCategoryId);

            var regulationId = brokerSettingsResponse.BrokerSettings.RegulationId;

            await ValidateRegulatoryType(model.RegulatoryTypeId, regulationId);

            List <ClientProfileSettings> clientProfileSettings;

            //duplicate settings if we use template
            if (!string.IsNullOrEmpty(model.AssetTypeTemplateId))
            {
                var regulatoryProfileTemplateExists =
                    await _assetTypesRepository.ExistsAsync(model.AssetTypeTemplateId);

                if (!regulatoryProfileTemplateExists)
                {
                    throw new AssetTypeDoesNotExistException();
                }

                clientProfileSettings = await
                                        _clientProfileSettingsRepository.GetAllAsync(null, model.AssetTypeTemplateId);

                foreach (var clientProfileSetting in clientProfileSettings)
                {
                    clientProfileSetting.AssetTypeId = model.Id;

                    var regulatorySettings = await _regulatorySettingsApi.GetRegulatorySettingsByIdsAsync(
                        clientProfileSetting.RegulatoryProfileId, model.RegulatoryTypeId);

                    ValidateRegulatoryConstraint(regulatorySettings, clientProfileSetting);
                }
            }
            else
            {
                clientProfileSettings = new List <ClientProfileSettings>();
                var allRegulatorySettings =
                    await _regulatorySettingsApi.GetRegulatorySettingsByRegulationAsync(regulationId);

                var clientProfiles = await _clientProfilesRepository.GetAllAsync();

                foreach (var clientProfile in clientProfiles)
                {
                    var regulatorySettings = allRegulatorySettings.RegulatorySettings.Single(x =>
                                                                                             x.TypeId == model.RegulatoryTypeId && x.ProfileId == clientProfile.RegulatoryProfileId);

                    clientProfileSettings.Add(new ClientProfileSettings
                    {
                        AssetTypeId     = model.Id,
                        ClientProfileId = clientProfile.Id,
                        Margin          = regulatorySettings.MarginMinPercent,
                        IsAvailable     = regulatorySettings.IsAvailable,
                    });
                }
            }

            await _assetTypesRepository.InsertAsync(model, clientProfileSettings);

            await _auditService.TryAudit(correlationId, username, model.Id, AuditDataType.AssetType,
                                         model.ToJson());

            await _entityChangedSender.SendEntityCreatedEvent <AssetType, AssetTypeContract, AssetTypeChangedEvent>(model,
                                                                                                                    username, correlationId);

            foreach (var profileSettings in clientProfileSettings)
            {
                await _entityChangedSender
                .SendEntityCreatedEvent <ClientProfileSettings, ClientProfileSettingsContract,
                                         ClientProfileSettingsChangedEvent>(profileSettings, username, correlationId);
            }
        }