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));
                }
            }
        }
Exemple #2
0
        private async Task <Result <Product, ProductsErrorCodes> > UnderlyingMustExist(Product value, string userName,
                                                                                       string correlationId, Product existing = null)
        {
            var underlying = _underlyingsCache.GetByMdsCode(value.UnderlyingMdsCode);

            if (underlying == null)
            {
                return(new Result <Product, ProductsErrorCodes>(ProductsErrorCodes.UnderlyingDoesNotExist));
            }

            value.TradingCurrency = underlying.TradingCurrency;

            DateTime?startDate;

            if (existing == null)
            {
                // we use StartDate from the request, if possible, and fallback to the underlying's StartDate otherwise
                startDate = value.StartDate ?? underlying.StartDate;
            }
            else
            {
                // for existing products we should not update StartDate from underlying
                startDate = value.StartDate ?? existing.StartDate;
            }

            if (existing != null && existing.IsStarted && startDate > DateTime.UtcNow)
            {
                return(new Result <Product, ProductsErrorCodes>(ProductsErrorCodes.CannotChangeStartDateFromPastToFuture));
            }

            value.StartDate = startDate;
            value.IsStarted = startDate < DateTime.UtcNow;

            return(new Result <Product, ProductsErrorCodes>(value));
        }