コード例 #1
0
        public void TestOutputCsv(RegulationRole regulationRole, String csvFile)
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;
            ReadOnlyCollection <DataEntity> dataEntities = m_TestsFixture.DataEntities;
            OutputWriterCsv writer = m_TestsFixture.WriterCsv;

            Engine engine = Engine.Of(Currency.Usd, ratesProvider);

            String csvExpectedFile = Utilities.GetStaticFilePath(csvFile);
            String csvActualFile   = Utilities.GetRandomFilePath(".csv");

            MarginTotal margin = engine.CalculateDetailed(regulationRole, dataEntities);

            writer.Write(csvActualFile, margin);

            String expected = Utilities.ComputeHash(File.ReadAllText(csvExpectedFile));
            String actual   = Utilities.ComputeHash(File.ReadAllText(csvActualFile));

            try
            {
                File.Delete(csvActualFile);
            }
            catch { }

            Assert.Equal(expected, actual);
        }
コード例 #2
0
        /// <summary>Opens a rates file and reads its content, using the specified delimiter. A parameter specifies whether the file contains a header that must be skipped.</summary>
        /// <param name="filePath">The <see cref="T:System.String"/> representing the file to open for reading.</param>
        /// <param name="delimiter">The <see cref="T:System.Char"/> representing the delimiter.</param>
        /// <param name="header"><c>true</c> if the rates file contains a header that must be skipped; otherwise, <c>false</c>.</param>
        /// <returns>An <see cref="T:InitialMargin.Core.FxRatesProvider"/> object containing the rates defined in the file.</returns>
        /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file, or when <paramref name="delimiter">delimiter</paramref> is invalid (see <see cref="M:InitialMargin.IO.CsvUtilities.IsValidDelimiter(System.Char)"/>).</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">Thrown when <paramref name="filePath">filePath</paramref> could not be found.</exception>
        /// <exception cref="T:System.IO.InvalidDataException">Thrown when the rates file contains invalid or malformed data.</exception>
        public FxRatesProvider Read(String filePath, Char delimiter, Boolean header)
        {
            List <String[]> fieldsMatrix = CsvParser.Parse(filePath, m_Encoding, delimiter, header);

            FxRatesProvider ratesProvider = new FxRatesProvider();

            if (fieldsMatrix.Count == 0)
            {
                return(ratesProvider);
            }

            foreach (var tuple in fieldsMatrix.Select((x, i) => new { Index = i, Value = x }))
            {
                Int32    index  = tuple.Index + (header ? 2 : 1);
                String[] values = tuple.Value;

                if (values.Length != 3)
                {
                    throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains an entry whose number of columns is not equal to 3.");
                }

                if (!Currency.TryParse(values[0], out Currency currency1))
                {
                    throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains an entry whose first currency ({values[0]}) is invalid.");
                }

                if (!Currency.TryParse(values[1], out Currency currency2))
                {
                    throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains an entry whose second currency ({values[1]}) is invalid.");
                }

                if (!Decimal.TryParse(values[2], NumberStyles.Any, m_FormatProvider, out Decimal rate) || (rate <= 0m))
                {
                    throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains an entry whose rate is invalid.");
                }

                if (currency1 == currency2)
                {
                    if (rate != 1m)
                    {
                        throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains an entry with two identical currencies and a rate not equal to 1.");
                    }

                    continue;
                }

                CurrencyPair currencyPair = CurrencyPair.Of(currency1, currency2);

                if (ratesProvider.OriginalRates.ContainsKey(currencyPair))
                {
                    throw new InvalidDataException($"[{filePath}, Line {index}] The rates file contains a duplicate entry.");
                }

                ratesProvider.AddRate(currencyPair, rate);
            }

            return(ratesProvider);
        }
コード例 #3
0
        public void TestRateTriangulationKo(String currencyBase, String currencyCounter, Boolean useTriangulation)
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;

            Currency oCurrencyBase    = Currency.Parse(currencyBase);
            Currency oCurrencyCounter = Currency.Parse(currencyCounter);

            Assert.Throws <InvalidOperationException>(() => ratesProvider.GetRate(oCurrencyBase, oCurrencyCounter, useTriangulation));
        }
コード例 #4
0
        public void TestRateTriangulationOk(String currencyBase, String currencyCounter, Double result)
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;

            Currency oCurrencyBase    = Currency.Parse(currencyBase);
            Currency oCurrencyCounter = Currency.Parse(currencyCounter);

            Decimal expected = Utilities.Round(result, 10);
            Decimal actual   = Utilities.Round(ratesProvider.GetRate(oCurrencyBase, oCurrencyCounter, true), 10);

            Assert.Equal(expected, actual);
        }
コード例 #5
0
        public void TestConversions(Double valueBase, String currencyBase, String currencyCounter, Double valueExpected, String currencyExpected)
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;

            Amount   amountBase       = Amount.Of(Currency.Parse(currencyBase), Utilities.Round(valueBase, 2));
            Currency oCurrencyCounter = Currency.Parse(currencyCounter);

            Amount expected = Amount.Of(Currency.Parse(currencyExpected), Utilities.Round(valueExpected, 2));
            Amount actual   = Amount.Round(ratesProvider.Convert(amountBase, oCurrencyCounter), 2, MidpointRounding.AwayFromZero);

            Assert.Equal(expected, actual);
        }
コード例 #6
0
        public void TestResult(RegulationRole regulationRole, Double result)
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;
            ReadOnlyCollection <DataEntity> dataEntities = m_TestsFixture.DataEntities;

            Engine engine = Engine.Of(Currency.Usd, ratesProvider);

            Decimal expected = Convert.ToDecimal(result);
            Decimal actual   = Math.Round(engine.Calculate(regulationRole, dataEntities).Value, 0, MidpointRounding.AwayFromZero);

            Assert.Equal(expected, actual);
        }
コード例 #7
0
        public void TestInputRates(String ratesFile)
        {
            FxRatesManager  ratesManager  = m_TestsFixture.RatesManager;
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;

            String          ratesFilePath       = Utilities.GetStaticFilePath(ratesFile);
            FxRatesProvider ratesProviderParsed = ratesManager.Read(ratesFilePath);

            Dictionary <CurrencyPair, Decimal> expected = ratesProvider.Rates
                                                          .ToDictionary(x => x.Key, x => x.Value);

            Dictionary <CurrencyPair, Decimal> actual = ratesProviderParsed.Rates
                                                        .ToDictionary(x => x.Key, x => x.Value);

            Assert.Equal(expected, actual);
        }
コード例 #8
0
        public void TestConsistency()
        {
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;
            ReadOnlyCollection <DataEntity> dataEntities = m_TestsFixture.DataEntities;

            Engine engine = Engine.Of(Currency.Usd, ratesProvider);

            Amount amount = engine.Calculate(RegulationRole.Secured, dataEntities);

            List <Amount> amounts = new List <Amount>(3)
            {
                engine.CalculateByRole(RegulationRole.Secured, dataEntities).Single().Value,
                engine.CalculateWorstOf(RegulationRole.Secured, dataEntities)
            };

            Assert.True(amounts.TrueForAll(x => x.Equals(amount)));
        }
コード例 #9
0
        private Engine(DateTime valuationDate, Currency calculationCurrency, FxRatesProvider ratesProvider)
        {
            m_CalculationCurrency = calculationCurrency;
            m_ValuationDate       = valuationDate;
            m_RatesProvider       = ratesProvider;

            List <Type> processorTypes = Assembly.GetExecutingAssembly().GetTypes()
                                         .Where(x => x.IsSubclassOf(typeof(Processor)))
                                         .ToList();

            m_Processors = new List <Processor>(processorTypes.Count);

            foreach (Type processorType in processorTypes)
            {
                m_Processors.Add((Processor)Activator.CreateInstance(processorType, valuationDate, calculationCurrency, ratesProvider));
            }
        }
コード例 #10
0
        /// <summary>Creates a new rates file and writes the content of the specified rates provider to it, using the specified delimiter. A parameter specifies whether the file must contain a header. If the target file already exists, it is overwritten.</summary>
        /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param>
        /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object to write.</param>
        /// <param name="delimiter">The <see cref="T:System.Char"/> representing the delimiter.</param>
        /// <param name="header"><c>true</c> if the rates file must contain a header; otherwise, <c>false</c>.</param>
        /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file, or when <paramref name="delimiter">delimiter</paramref> is invalid (see <see cref="M:InitialMargin.IO.CsvUtilities.IsValidDelimiter(System.Char)"/>).</exception>
        /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
        public void Write(String filePath, FxRatesProvider ratesProvider, Char delimiter, Boolean header)
        {
            if (String.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException("Invalid file path specified.", nameof(filePath));
            }

            if (Path.GetExtension(filePath).ToUpperInvariant() != ".CSV")
            {
                throw new ArgumentException("The specified file must be a CSV.", nameof(filePath));
            }

            if (ratesProvider == null)
            {
                throw new ArgumentNullException(nameof(ratesProvider));
            }

            if (!CsvUtilities.IsValidDelimiter(delimiter))
            {
                throw new ArgumentException($"Invalid delimiter specified (accepted delimiters are: {CsvUtilities.ValidDelimiters}).", nameof(delimiter));
            }

            ReadOnlyDictionary <CurrencyPair, Decimal> rates = ratesProvider.OriginalRates;
            List <String[]> fieldsMatrix = new List <String[]>(rates.Count);

            foreach (KeyValuePair <CurrencyPair, Decimal> rate in rates)
            {
                CurrencyPair currencyPair = rate.Key;

                String[] row = new String[3];
                row[0] = currencyPair.CurrencyBase.ToString().ToUpperInvariant();
                row[1] = currencyPair.CurrencyCounter.ToString().ToUpperInvariant();
                row[2] = rate.Value.ToString(m_FormatProvider);

                fieldsMatrix.Add(row);
            }

            String result = CsvUtilities.FinalizeFieldsMatrix(header ? s_HeaderFields : null, fieldsMatrix, delimiter, Environment.NewLine);

            File.WriteAllText(filePath, result, m_Encoding);
        }
コード例 #11
0
        public void TestOutputRates(String ratesFile)
        {
            FxRatesManager  ratesManager  = m_TestsFixture.RatesManager;
            FxRatesProvider ratesProvider = m_TestsFixture.RatesProvider;

            String ratesExpectedFile = Utilities.GetStaticFilePath(ratesFile);
            String ratesActualFile   = Utilities.GetRandomFilePath(".csv");

            ratesManager.Write(ratesActualFile, ratesProvider);

            String expected = Utilities.ComputeHash(File.ReadAllText(ratesExpectedFile));
            String actual   = Utilities.ComputeHash(File.ReadAllText(ratesActualFile));

            try
            {
                File.Delete(ratesActualFile);
            }
            catch { }

            Assert.Equal(expected, actual);
        }
コード例 #12
0
        /// <summary>Initializes and returns a new instance using the specified parameters.</summary>
        /// <param name="valuationDate">The <see cref="T:System.DateTime"/> representing the valuation date of the engine.</param>
        /// <param name="calculationCurrency">The <see cref="T:InitialMargin.Core.Currency"/> object representing the calculation currency of the engine.</param>
        /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object supplying the rates.</param>
        /// <returns>A new instance of <see cref="T:InitialMargin.Engine"/> initialized with the specified parameters.</returns>
        /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="calculationCurrency">calculationCurrency</paramref> is <c>null</c> or when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
        public static Engine Of(DateTime valuationDate, Currency calculationCurrency, FxRatesProvider ratesProvider)
        {
            if (calculationCurrency == null)
            {
                throw new ArgumentNullException(nameof(calculationCurrency));
            }

            if (ratesProvider == null)
            {
                throw new ArgumentNullException(nameof(ratesProvider));
            }

            return(new Engine(valuationDate.Date, calculationCurrency, ratesProvider));
        }
コード例 #13
0
 /// <summary>Creates a new rates file and writes the content of the specified rates provider to it, using the specified delimiter and without prepending a header. If the target file already exists, it is overwritten.</summary>
 /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param>
 /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object to write.</param>
 /// <param name="delimiter">The <see cref="T:System.Char"/> representing the delimiter.</param>
 /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file, or when <paramref name="delimiter">delimiter</paramref> is invalid (see <see cref="M:InitialMargin.IO.CsvUtilities.IsValidDelimiter(System.Char)"/>).</exception>
 /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
 public void Write(String filePath, FxRatesProvider ratesProvider, Char delimiter)
 {
     Write(filePath, ratesProvider, delimiter, false);
 }
コード例 #14
0
 public ScheduleProcessor(DateTime valuationDate, Currency calculationCurrency, FxRatesProvider ratesProvider) : base(valuationDate, calculationCurrency, ratesProvider)
 {
 }
コード例 #15
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <IBucket>      buckets       = sensitivities.Select(x => x.Bucket).Distinct().OrderBy(x => x.Name).ToList();
                List <MarginBucket> bucketMargins = new List <MarginBucket>(buckets.Count);
                Dictionary <IBucket, Dictionary <String, Decimal> > thresholdFactors = new Dictionary <IBucket, Dictionary <String, Decimal> >();

                foreach (IBucket bucket in buckets)
                {
                    List <Sensitivity> sensitivitiesByBucket = sensitivities.Where(x => x.Bucket == bucket).ToList();

                    thresholdFactors[bucket] = sensitivitiesByBucket
                                               .GroupBy(x => new { x.Bucket, x.Qualifier })
                                               .ToDictionary(x => x.Key.Qualifier, x => CalculateThresholdFactor(calculationCurrency, ratesProvider, risk, category, x.ToList()));

                    List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(category, thresholdFactors[bucket], sensitivitiesByBucket);
                    Amount sumSquared         = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                    Amount sumCorrelated      = CalculateCorrelatedSumWeights(calculationCurrency, risk, weightingMargins, thresholdFactors[bucket]);
                    Amount bucketMarginAmount = Amount.SquareRoot(sumSquared + sumCorrelated);

                    bucketMargins.Add(MarginBucket.Of(bucket, bucketMarginAmount, weightingMargins));
                }

                List <MarginBucket> bucketMarginsNonResidual = bucketMargins.Where(x => !x.Bucket.IsResidual).ToList();
                Amount sumSquaredNonResidual = Amount.Sum(bucketMarginsNonResidual.Select(x => Amount.Square(x.Value)), calculationCurrency);

                List <MarginBucket> bucketMarginsResidual = bucketMargins.Where(x => x.Bucket.IsResidual).ToList();
                Amount sumSquaredResidual = Amount.Sum(bucketMarginsResidual.Select(x => Amount.Square(x.Value)), calculationCurrency);

                Amount sumCorrelatedNonResidual;

                if ((risk == SensitivityRisk.Rates) && (category == SensitivityCategory.Delta))
                {
                    Dictionary <IBucket, Decimal> bucketThresholdFactors = thresholdFactors.ToDictionary(x => x.Key, x => x.Value.Values.Single());
                    sumCorrelatedNonResidual = CalculateCorrelatedSumBuckets(calculationCurrency, risk, bucketThresholdFactors, bucketMarginsNonResidual);
                }
                else
                {
                    sumCorrelatedNonResidual = CalculateCorrelatedSumBuckets(calculationCurrency, risk, bucketMarginsNonResidual);
                }

                Amount sensitivityMarginAmount = Amount.SquareRoot(sumSquaredNonResidual + sumCorrelatedNonResidual) + Amount.SquareRoot(sumSquaredResidual);

                return(MarginSensitivity.Of(category, sensitivityMarginAmount, bucketMargins));
            }
コード例 #16
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <IBucket>      buckets       = sensitivities.Select(x => x.Bucket).Distinct().OrderBy(x => x.Name).ToList();
                List <MarginBucket> bucketMargins = new List <MarginBucket>(buckets.Count);

                foreach (IBucket bucket in buckets)
                {
                    List <Sensitivity> sensitivitiesByBucket = sensitivities.Where(x => x.Bucket == bucket).ToList();

                    List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(SensitivityCategory.Curvature, sensitivitiesByBucket);
                    Amount sumSquared         = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                    Amount sumCorrelated      = CalculateCorrelatedSumWeights(calculationCurrency, risk, weightingMargins);
                    Amount bucketMarginAmount = Amount.SquareRoot(sumSquared + sumCorrelated);

                    bucketMargins.Add(MarginBucket.Of(bucket, bucketMarginAmount, weightingMargins));
                }

                Amount marginNonResidual       = CalculateMarginByBuckets(calculationCurrency, risk, false, bucketMargins);
                Amount marginResidual          = CalculateMarginByBuckets(calculationCurrency, risk, true, bucketMargins);
                Amount sensitivityMarginAmount = ModelParameters.GetCurvatureScaleFactor(risk) * (marginNonResidual + marginResidual);

                return(MarginSensitivity.Of(SensitivityCategory.Curvature, sensitivityMarginAmount, bucketMargins));
            }
コード例 #17
0
        private static Decimal CalculateThresholdFactor(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
        {
            if ((risk == SensitivityRisk.Rates) && (category == SensitivityCategory.Delta))
            {
                sensitivities = sensitivities.Where(x => x.Subrisk != SensitivitySubrisk.CrossCurrencyBasis).ToList();

                if (sensitivities.Count == 0)
                {
                    return(1m);
                }
            }

            Amount sum = Amount.Abs(Amount.Sum(sensitivities.Select(x => x.Amount), calculationCurrency));

            IThresholdIdentifier thresholdIdentifier = sensitivities.Select(y => y.ThresholdIdentifier).Distinct().Single();
            Amount threshold = ratesProvider.Convert(ModelParameters.GetThreshold(risk, category, thresholdIdentifier), calculationCurrency);

            return(Math.Max(1m, MathUtilities.SquareRoot((sum / threshold).Value)));
        }
コード例 #18
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(SensitivityCategory.BaseCorrelation, sensitivities);
                Amount sumSquared    = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                Amount sumCorrelated = CalculateCorrelatedSumWeights(calculationCurrency, weightingMargins);
                Amount amount        = Amount.SquareRoot(sumSquared + sumCorrelated);

                MarginBucket bucketMargin = MarginBucket.Of(Placeholder.Instance, amount, weightingMargins);

                return(MarginSensitivity.Of(SensitivityCategory.BaseCorrelation, amount, (new List <MarginBucket> {
                    bucketMargin
                })));
            }
コード例 #19
0
 public abstract MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities);
コード例 #20
0
        public static List <MarginProduct> CalculateMarginsProduct(Currency calculationCurrency, FxRatesProvider ratesProvider, List <Sensitivity> sensitivities)
        {
            if (calculationCurrency == null)
            {
                throw new ArgumentNullException(nameof(calculationCurrency));
            }

            if (ratesProvider == null)
            {
                throw new ArgumentNullException(nameof(ratesProvider));
            }

            if (sensitivities == null)
            {
                throw new ArgumentNullException(nameof(sensitivities));
            }

            List <Product>       products       = sensitivities.Select(x => x.Product).Distinct().OrderBy(x => x).ToList();
            List <MarginProduct> productMargins = new List <MarginProduct>(products.Count);

            foreach (Product product in products)
            {
                List <Sensitivity> sensitivitiesByProduct = sensitivities.Where(x => x.Product == product).ToList();

                List <MarginRisk> riskMargins         = CalculateRiskMargins(calculationCurrency, ratesProvider, sensitivitiesByProduct);
                Amount            sumSquared          = Amount.Sum(riskMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                Amount            sumCorrelated       = CalculateCorrelatedSumRisks(calculationCurrency, riskMargins);
                Amount            productMarginAmount = Amount.SquareRoot(sumSquared + sumCorrelated);

                productMargins.Add(MarginProduct.Of(product, productMarginAmount, riskMargins));
            }

            return(productMargins);
        }
コード例 #21
0
        private static List <MarginRisk> CalculateRiskMargins(Currency calculationCurrency, FxRatesProvider ratesProvider, List <Sensitivity> sensitivities)
        {
            List <SensitivityRisk> risks       = sensitivities.Select(x => x.Risk).Distinct().OrderBy(x => x.ToString()).ToList();
            List <MarginRisk>      riskMargins = new List <MarginRisk>(risks.Count);

            foreach (SensitivityRisk risk in risks)
            {
                List <Sensitivity> sensitivitiesByRisk = sensitivities.Where(x => x.Risk == risk).ToList();

                List <SensitivityCategory> sensitivityCategories = sensitivitiesByRisk.Select(x => x.Category).Distinct().OrderBy(x => x.ToString()).ToList();
                List <MarginSensitivity>   sensitivityMargins    = new List <MarginSensitivity>(sensitivityCategories.Count);

                foreach (SensitivityCategory category in sensitivityCategories)
                {
                    List <Sensitivity> sensitivitiesByClass = sensitivitiesByRisk.Where(x => x.Category == category).ToList();
                    MarginSensitivity  sensitivityMargin    = s_MarginCalculators[category].CalculateMargin(calculationCurrency, ratesProvider, risk, category, sensitivitiesByClass);

                    sensitivityMargins.Add(sensitivityMargin);
                }

                Amount riskMarginAmount = Amount.Sum(sensitivityMargins.Select(x => x.Value), calculationCurrency);

                riskMargins.Add(MarginRisk.Of(risk, riskMarginAmount, sensitivityMargins));
            }

            return(riskMargins);
        }
コード例 #22
0
        private static FxRatesProvider CreateRatesProvider()
        {
            FxRatesProvider ratesProvider = new FxRatesProvider();

            ratesProvider.AddRate(Currency.Usd, Currency.Aed, 3.6725m);
            ratesProvider.AddRate(Currency.Usd, Currency.Afn, 78.5310333944m);
            ratesProvider.AddRate(Currency.Usd, Currency.All, 109.9428418518m);
            ratesProvider.AddRate(Currency.Usd, Currency.Amd, 480.5582473128m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ang, 1.7899999938m);
            ratesProvider.AddRate(Currency.Usd, Currency.Aoa, 326.6060255636m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ars, 45.0412132395m);
            ratesProvider.AddRate(Currency.Usd, Currency.Aud, 1.4432844156m);
            ratesProvider.AddRate(Currency.Usd, Currency.Awg, 1.79m);
            ratesProvider.AddRate(Currency.Usd, Currency.Azn, 1.696492363m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bam, 1.7453610606m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bbd, 2m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bdt, 84.4767286935m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bgn, 1.7453610606m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bhd, 0.376m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bif, 1836.0857892173m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bmd, 1m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bnd, 1.3687250827m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bob, 6.906313269m);
            ratesProvider.AddRate(Currency.Usd, Currency.Brl, 3.9871915722m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bsd, 1m);
            ratesProvider.AddRate(Currency.Usd, Currency.Btn, 70.2600231444m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bwp, 10.6937002014m);
            ratesProvider.AddRate(Currency.Usd, Currency.Byn, 2.0891453577m);
            ratesProvider.AddRate(Currency.Usd, Currency.Bzd, 2.0141543654m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cad, 1.3436510634m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cdf, 1648.3875977128m);
            ratesProvider.AddRate(Currency.Usd, Currency.Chf, 1.009007713m);
            ratesProvider.AddRate(Currency.Usd, Currency.Clp, 692.3058554072m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cny, 6.8754746235m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cop, 3291.62306082m);
            ratesProvider.AddRate(Currency.Usd, Currency.Crc, 594.6918709224m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cup, 26.5m);
            ratesProvider.AddRate(Currency.Usd, Currency.Cve, 98.4037284167m);
            ratesProvider.AddRate(Currency.Usd, Currency.Czk, 22.9538661477m);
            ratesProvider.AddRate(Currency.Usd, Currency.Djf, 177.7698994864m);
            ratesProvider.AddRate(Currency.Usd, Currency.Dkk, 6.6630941262m);
            ratesProvider.AddRate(Currency.Usd, Currency.Dop, 50.67066023m);
            ratesProvider.AddRate(Currency.Usd, Currency.Dzd, 119.753693494m);
            ratesProvider.AddRate(Currency.Usd, Currency.Egp, 17.0798292155m);
            ratesProvider.AddRate(Currency.Usd, Currency.Etb, 28.9968088106m);
            ratesProvider.AddRate(Currency.Usd, Currency.Eur, 0.89238894m);
            ratesProvider.AddRate(Currency.Usd, Currency.Fjd, 2.1568427232m);
            ratesProvider.AddRate(Currency.Usd, Currency.Fkp, 0.776923679m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gbp, 0.776923679m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gel, 2.7439449439m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ghs, 5.1602827518m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gip, 0.776923679m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gmd, 49.6033761727m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gnf, 9223.6167422552m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gtq, 7.6534246994m);
            ratesProvider.AddRate(Currency.Usd, Currency.Gyd, 209.4767888422m);
            ratesProvider.AddRate(Currency.Usd, Currency.Hkd, 7.8495450225m);
            ratesProvider.AddRate(Currency.Usd, Currency.Hnl, 24.4354235709m);
            ratesProvider.AddRate(Currency.Usd, Currency.Hrk, 6.6231045402m);
            ratesProvider.AddRate(Currency.Usd, Currency.Htg, 87.2867040791m);
            ratesProvider.AddRate(Currency.Usd, Currency.Huf, 289.739854453m);
            ratesProvider.AddRate(Currency.Usd, Currency.Idr, 14449.5907749033m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ils, 3.5698607201m);
            ratesProvider.AddRate(Currency.Usd, Currency.Inr, 70.2600231444m);
            ratesProvider.AddRate(Currency.Usd, Currency.Iqd, 1195.9945328397m);
            ratesProvider.AddRate(Currency.Usd, Currency.Irr, 42103.6181049991m);
            ratesProvider.AddRate(Currency.Usd, Currency.Isk, 122.4311723316m);
            ratesProvider.AddRate(Currency.Usd, Currency.Jmd, 135.6515136135m);
            ratesProvider.AddRate(Currency.Usd, Currency.Jod, 0.709m);
            ratesProvider.AddRate(Currency.Usd, Currency.Jpy, 109.5921338456m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kes, 101.0653013309m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kgs, 69.8502684734m);
            ratesProvider.AddRate(Currency.Usd, Currency.Khr, 4061.2711289118m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kmf, 439.0265789494m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kpw, 900.0815039499m);
            ratesProvider.AddRate(Currency.Usd, Currency.Krw, 1187.6063062343m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kwd, 0.3042077699m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kyd, 0.8199999711m);
            ratesProvider.AddRate(Currency.Usd, Currency.Kzt, 379.5988461204m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lak, 8684.956674892m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lbp, 1507.5m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lkr, 176.2598885557m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lrd, 179.1975234039m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lsl, 14.199591614m);
            ratesProvider.AddRate(Currency.Usd, Currency.Lyd, 1.3996581554m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mad, 9.6455562128m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mdl, 17.8999249372m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mga, 3603.3783484999m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mkd, 54.9892842944m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mmk, 1527.3447085636m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mnt, 2641.5839544202m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mop, 8.0850313732m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mru, 36.6981098071m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mur, 35.1553187233m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mvr, 15.3886642328m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mwk, 724.7354535121m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mxn, 19.0718694767m);
            ratesProvider.AddRate(Currency.Usd, Currency.Myr, 4.173265079m);
            ratesProvider.AddRate(Currency.Usd, Currency.Mzn, 63.6898302664m);
            ratesProvider.AddRate(Currency.Usd, Currency.Nad, 14.199591614m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ngn, 359.9966790011m);
            ratesProvider.AddRate(Currency.Usd, Currency.Nio, 33.1270570298m);
            ratesProvider.AddRate(Currency.Usd, Currency.Nok, 8.7204439365m);
            ratesProvider.AddRate(Currency.Usd, Currency.Npr, 112.9429872046m);
            ratesProvider.AddRate(Currency.Usd, Currency.Nzd, 1.5242499724m);
            ratesProvider.AddRate(Currency.Usd, Currency.Omr, 0.3845m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pab, 1m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pen, 3.3231897163m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pgk, 3.3783976041m);
            ratesProvider.AddRate(Currency.Usd, Currency.Php, 52.3089323526m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pkr, 141.613183941m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pln, 3.8402645992m);
            ratesProvider.AddRate(Currency.Usd, Currency.Pyg, 6356.2253938512m);
            ratesProvider.AddRate(Currency.Usd, Currency.Qar, 3.64m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ron, 4.2488317391m);
            ratesProvider.AddRate(Currency.Usd, Currency.Rsd, 105.2421166253m);
            ratesProvider.AddRate(Currency.Usd, Currency.Rub, 64.5309626263m);
            ratesProvider.AddRate(Currency.Usd, Currency.Rwf, 905.9739553945m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sar, 3.75m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sbd, 8.0413360072m);
            ratesProvider.AddRate(Currency.Usd, Currency.Scr, 13.6115590477m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sdg, 45.0960499485m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sek, 9.6029787646m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sgd, 1.3687250827m);
            ratesProvider.AddRate(Currency.Usd, Currency.Shp, 0.776923679m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sll, 8874.1549767724m);
            ratesProvider.AddRate(Currency.Usd, Currency.Sos, 580.4139856458m);
            ratesProvider.AddRate(Currency.Usd, Currency.Srd, 7.4574277811m);
            ratesProvider.AddRate(Currency.Usd, Currency.Stn, 21.8786640996m);
            ratesProvider.AddRate(Currency.Usd, Currency.Svc, 8.75m);
            ratesProvider.AddRate(Currency.Usd, Currency.Syp, 514.9989266974m);
            ratesProvider.AddRate(Currency.Usd, Currency.Szl, 14.199591614m);
            ratesProvider.AddRate(Currency.Usd, Currency.Thb, 31.5504179073m);
            ratesProvider.AddRate(Currency.Usd, Currency.Tjs, 9.4394984415m);
            ratesProvider.AddRate(Currency.Usd, Currency.Tmt, 3.5000004519m);
            ratesProvider.AddRate(Currency.Usd, Currency.Tnd, 2.993029268m);
            ratesProvider.AddRate(Currency.Usd, Currency.Top, 2.2760793695m);
            ratesProvider.AddRate(Currency.Usd, Currency.Try, 6.0045772074m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ttd, 6.7718865774m);
            ratesProvider.AddRate(Currency.Usd, Currency.Tvd, 1.4432844156m);
            ratesProvider.AddRate(Currency.Usd, Currency.Twd, 31.0892037106m);
            ratesProvider.AddRate(Currency.Usd, Currency.Tzs, 2298.999049136m);
            ratesProvider.AddRate(Currency.Usd, Currency.Uah, 26.3635670612m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ugx, 3772.9608374011m);
            ratesProvider.AddRate(Currency.Usd, Currency.Uyu, 35.2496745779m);
            ratesProvider.AddRate(Currency.Usd, Currency.Uzs, 8458.1403796178m);
            ratesProvider.AddRate(Currency.Usd, Currency.Ves, 5245.403525587m);
            ratesProvider.AddRate(Currency.Usd, Currency.Vnd, 23316.2645357692m);
            ratesProvider.AddRate(Currency.Usd, Currency.Vuv, 115.9205515476m);
            ratesProvider.AddRate(Currency.Usd, Currency.Wst, 2.634341304m);
            ratesProvider.AddRate(Currency.Usd, Currency.Yer, 250.3138618604m);
            ratesProvider.AddRate(Currency.Usd, Currency.Zar, 14.199591614m);
            ratesProvider.AddRate(Currency.Usd, Currency.Zmw, 13.4769973161m);
            ratesProvider.AddRate(Currency.Usd, Currency.Zwl, 361.9m);

            return(ratesProvider);
        }
コード例 #23
0
 /// <summary>Creates a new rates file and writes the content of the specified rates provider to it, using the default delimiter (see <see cref="P:InitialMargin.IO.CsvUtilities.DefaultDelimiter"/>) and without prepending a header. If the target file already exists, it is overwritten.</summary>
 /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param>
 /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object to write.</param>
 /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file.</exception>
 /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
 public void Write(String filePath, FxRatesProvider ratesProvider)
 {
     Write(filePath, ratesProvider, CsvUtilities.DefaultDelimiter, false);
 }
コード例 #24
0
 /// <summary>Initializes and returns a new instance using the specified parameters and assuming that the valuation takes place today (see <see cref="P:System.DateTime.Today"/>).</summary>
 /// <param name="calculationCurrency">The <see cref="T:InitialMargin.Core.Currency"/> object representing the calculation currency of the engine.</param>
 /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object supplying the rates.</param>
 /// <returns>A new instance of <see cref="T:InitialMargin.Engine"/> initialized with the specified parameters.</returns>
 /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="calculationCurrency">calculationCurrency</paramref> is <c>null</c> or when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
 public static Engine Of(Currency calculationCurrency, FxRatesProvider ratesProvider)
 {
     return(Of(DateTime.Today, calculationCurrency, ratesProvider));
 }
コード例 #25
0
 /// <summary>Creates a new rates file and writes the content of the specified rates provider to it, using the default delimiter (see <see cref="P:InitialMargin.IO.CsvUtilities.DefaultDelimiter"/>). A parameter specifies whether the file must contain a header. If the target file already exists, it is overwritten.</summary>
 /// <param name="filePath">The <see cref="T:System.String"/> representing the file to write to.</param>
 /// <param name="ratesProvider">The <see cref="T:InitialMargin.Core.FxRatesProvider"/> object to write.</param>
 /// <param name="header"><c>true</c> if the rates file must contain a header; otherwise, <c>false</c>.</param>
 /// <exception cref="T:System.ArgumentException">Thrown when <paramref name="filePath">filePath</paramref> is invalid or does not refer to a CSV file.</exception>
 /// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="ratesProvider">ratesProvider</paramref> is <c>null</c>.</exception>
 public void Write(String filePath, FxRatesProvider ratesProvider, Boolean header)
 {
     Write(filePath, ratesProvider, CsvUtilities.DefaultDelimiter, header);
 }