public async Task <IReadOnlyList <OvernightSwapRate> > GetOvernightSwapRatesAsync(IList <string> assetPairIds = null)
        {
            var products = (await _productsRepository.GetByProductsIdsAsync(assetPairIds)).ToDictionary(x => x.ProductId, v => v);

            //If filter is empty we should get all products(asset pairs)
            if (assetPairIds == null || !assetPairIds.Any())
            {
                assetPairIds = products.Keys.ToList();
            }

            var productTradingCurrencyMap = products.ToDictionary(x => x.Key, v => v.Value.TradingCurrency);

            var tradingCurrencies =
                (await _currenciesRepository.GetByIdsAsync(productTradingCurrencyMap.Values)).ToDictionary(x => x.Id, v => v);

            var underlyings = products.Select(x => x.Value.UnderlyingMdsCode).Distinct()
                              .Select(_underlyingsCache.GetByMdsCode)
                              .ToDictionary(x => x.MdsCode, v => v);

            var baseCurrenciesIds = underlyings.Values.Where(x => !string.IsNullOrEmpty(x.BaseCurrency))
                                    .Select(x => x.BaseCurrency).Distinct();

            var baseCurrencies =
                (await _currenciesRepository.GetByIdsAsync(baseCurrenciesIds)).ToDictionary(x => x.Id, v => v);

            var result = new List <OvernightSwapRate>();

            foreach (var assetPairId in assetPairIds)
            {
                products.TryGetValue(assetPairId, out var product);
                if (product == null)
                {
                    _log.WriteWarning(nameof(RateSettingsService), nameof(GetOvernightSwapRatesAsync),
                                      $"Missing product with id: {assetPairId} , default values will be used to create OvernightSwapRate");

                    result.Add(OvernightSwapRate.FromDefault(_defaultRateSettings.DefaultOvernightSwapSettings, assetPairId));
                    continue;
                }

                var productId = product.ProductId;
                underlyings.TryGetValue(product.UnderlyingMdsCode, out var underlying);
                var baseCurrencyId = underlying?.BaseCurrency;
                var baseCurrency   = string.IsNullOrEmpty(baseCurrencyId) ? null :
                                     baseCurrencies.ContainsKey(baseCurrencyId) ? baseCurrencies[baseCurrencyId] : null;
                var tradingCurrencyId = productTradingCurrencyMap[productId];
                var tradingCurrency   = tradingCurrencies[tradingCurrencyId];

                var rate = new OvernightSwapRate
                {
                    AssetPairId          = productId,
                    VariableRateQuote    = tradingCurrency.InterestRateMdsCode,
                    RepoSurchargePercent = underlying?.RepoSurchargePercent ?? _defaultRateSettings.DefaultOvernightSwapSettings.RepoSurchargePercent,
                    VariableRateBase     = baseCurrency?.InterestRateMdsCode,
                };

                result.Add(rate);
            }

            return(result);
        }
Beispiel #2
0
        public async Task <List <Asset> > GetLegacyAssets(IEnumerable <string> productIds = null)
        {
            var products =
                (await _productsRepository.GetByProductsIdsAsync(productIds))
                .Where(x => x.IsStarted)
                .ToDictionary(x => x.ProductId, v => v);

            var brokerSettingsResponse = await _brokerSettingsApi.GetByIdAsync(_brokerId);

            if (brokerSettingsResponse.ErrorCode != BrokerSettingsErrorCodesContract.None)
            {
                throw new InvalidOperationException($"Unexpected error code {brokerSettingsResponse.ErrorCode}, " +
                                                    $"while retrieving settings for broker id {_brokerId}");
            }

            var productTradingCurrencyMap = products.ToDictionary(x => x.Key, v => v.Value.TradingCurrency);
            var productMarketSettingsMap  = products.ToDictionary(x => x.Key, v => v.Value.Market);
            var productTickFormulaMap     = products.ToDictionary(x => x.Key, v => v.Value.TickFormula);
            var productToCategoryMap      = products.ToDictionary(x => x.Key, v => v.Value.Category);
            var productAssetTypeIdMap     = products.ToDictionary(x => x.Key, v => v.Value.AssetType);

            var tradingCurrencies =
                (await _currenciesRepository.GetByIdsAsync(productTradingCurrencyMap.Values.Distinct())).ToDictionary(x => x.Id, v => v);

            var availableClientProfileSettingsDict =
                (await _clientProfileSettingsRepository.GetAllAsync(string.Empty, productAssetTypeIdMap.Values.Distinct(), true))
                .GroupBy(x => x.AssetTypeId)
                .ToDictionary(x => x.Key, v => v.AsEnumerable());

            var underlyings = products.Select(x => x.Value.UnderlyingMdsCode).Distinct()
                              .Select(_underlyingsCache.GetByMdsCode)
                              .ToDictionary(x => x.MdsCode, v => v);

            var baseCurrenciesIds = underlyings.Values.Where(x => !string.IsNullOrEmpty(x.BaseCurrency))
                                    .Select(x => x.BaseCurrency).Distinct();

            var baseCurrencies =
                (await _currenciesRepository.GetByIdsAsync(baseCurrenciesIds)).ToDictionary(x => x.Id, v => v);

            var productCategories =
                (await _productCategoriesRepository.GetByIdsAsync(productToCategoryMap.Values.Distinct())).ToDictionary(x => x.Id, v => v);

            var productMarketSettings =
                (await _marketSettingsRepository.GetByIdsAsync(productMarketSettingsMap.Values.Distinct())).ToDictionary(x => x.Id, v => v);

            var productTickFormulas =
                (await _tickFormulaRepository.GetByIdsAsync(productTickFormulaMap.Values.Distinct())).ToDictionary(x => x.Id, v => v);

            var assetTypes = (await _assetTypesRepository.GetAllAsync()).ToDictionary(x => x.Id, v => v);

            var result = new List <Asset>();

            foreach (var product in products.Values)
            {
                var id    = product.ProductId;
                var asset = CreateEmptyAsset();

                underlyings.TryGetValue(product.UnderlyingMdsCode, out var underlying);
                var baseCurrencyId = underlying?.BaseCurrency;
                var baseCurrency   = string.IsNullOrEmpty(baseCurrencyId) ? null :
                                     baseCurrencies.ContainsKey(baseCurrencyId) ? baseCurrencies[baseCurrencyId] : null;

                if (underlying != null)
                {
                    asset.SetAssetFieldsFromUnderlying(underlying);
                }
                else
                {
                    _log.WriteWarning(nameof(LegacyAssetsService), nameof(GetLegacyAssets),
                                      $"Missing underlying in cache for product with mdsCode:{product.UnderlyingMdsCode}");
                }

                asset.SetAssetFieldsFromProduct(product);

                if (baseCurrency != null)
                {
                    asset.SetAssetFieldsFromBaseCurrency(baseCurrency, _assetTypesWithZeroInterestRate);
                }

                asset.SetAssetFieldsFromTradingCurrency(tradingCurrencies[productTradingCurrencyMap[id]], _assetTypesWithZeroInterestRate);

                if (availableClientProfileSettingsDict.TryGetValue(asset.Underlying.AssetType, out var clientProfileSettingsList))
                {
                    var availableClientProfileSettingsForAssetType = clientProfileSettingsList.Select(x =>
                                                                                                      x.ToClientProfileWithRate(product.GetMarginRate(x.Margin)));
                    asset.Underlying.AvailableClientProfiles.AddRange(availableClientProfileSettingsForAssetType);
                }

                asset.SetAssetFieldsFromCategory(productCategories[productToCategoryMap[id]]);
                asset.SetAssetFieldsFromMarketSettings(productMarketSettings[productMarketSettingsMap[id]]);
                asset.SetAssetFieldsFromTickFormula(productTickFormulas[productTickFormulaMap[id]]);
                asset.SetAssetFieldsFromAssetType(assetTypes[productAssetTypeIdMap[id]]);
                asset.SetDividendFactorFields(productMarketSettings[productMarketSettingsMap[id]],
                                              brokerSettingsResponse.BrokerSettings,
                                              product);

                result.Add(asset);
            }

            return(result);
        }