Beispiel #1
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);
        }
Beispiel #2
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);
        }