private Boolean PerformTriangulation(CurrencyPair currencyPair, out Decimal rate) { rate = 0m; foreach (CurrencyPair key1 in m_Rates.Keys) { if (key1.CurrencyBase != currencyPair.CurrencyBase) { continue; } foreach (CurrencyPair key2 in m_Rates.Keys) { if (key2.CurrencyCounter != currencyPair.CurrencyCounter) { continue; } if (key1.CurrencyCounter != key2.CurrencyBase) { continue; } rate = m_Rates[key1] * m_Rates[key2]; m_Rates[currencyPair] = rate; m_Rates[currencyPair.Invert()] = 1m / rate; return(true); } } return(false); }
/// <summary>Adds the specified rate for the specified currency pair, using the specified update criterion.</summary> /// <param name="currencyPair">The <see cref="T:InitialMargin.Core.CurrencyPair"/> object defining the base and counter currencies.</param> /// <param name="rate">A <see cref="T:System.Decimal"/> representing the rate.</param> /// <param name="update"><c>true</c> to update the lookup table if the currency pair already exists; otherwise, <c>false</c>.</param> /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="rate">rate</paramref> is less than or equal to <c>0</c>.</exception> /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="currencyPair">currencyPair</paramref> is <c>null</c>.</exception> /// <exception cref="T:System.InvalidOperationException">Thrown when <paramref name="currencyPair">currencyPair</paramref> already exists in the lookup table and <paramref name="update">update</paramref> is <c>false</c>.</exception> public void AddRate(CurrencyPair currencyPair, Decimal rate, Boolean update) { if (currencyPair == null) { throw new ArgumentNullException(nameof(currencyPair)); } if (rate <= 0m) { throw new ArgumentException("Invalid rate specified.", nameof(rate)); } if (update || !m_Rates.ContainsKey(currencyPair)) { CurrencyPair currencyPairInverse = currencyPair.Invert(); m_Rates[currencyPair] = rate; m_OriginalCurrencyPairs.Add(currencyPair); m_Rates[currencyPairInverse] = 1m / rate; m_OriginalCurrencyPairs.Remove(currencyPairInverse); return; } throw new InvalidDataException("The specified rate has already been defined, directly or implicitly."); }