コード例 #1
0
        internal async Task <MetalCurrencyCombined> GetMetalPricesInCurrency(
            Currency currency,
            Metal metal,
            DateTime start,
            DateTime end)
        {
            //sets up order of execution
            var filters = new List <IFilter>
            {
                (IFilter)_serviceProvider.GetService(typeof(RequesterFilter)),
                (IFilter)_serviceProvider.GetService(typeof(FillerFilter)),
                (IFilter)_serviceProvider.GetService(typeof(MetalCurrencyConverterFilter))
            };

            var metalCurrencyCombined = new MetalCurrencyCombined
            {
                Currency        = currency,
                Metal           = metal,
                Start           = start,
                End             = end,
                OperationStatus = new StringBuilder()
            };

            try
            {
                metalCurrencyCombined = filters.Aggregate(metalCurrencyCombined,
                                                          (current, operation) => operation.Execute(current));
            }
            catch (Exception e)
            {
                metalCurrencyCombined.OperationStatus.AppendLine(e.Message);
            }

            return(metalCurrencyCombined);
        }
コード例 #2
0
ファイル: RequesterFilter.cs プロジェクト: mjoniec/GoldCharts
        public MetalCurrencyCombined Execute(MetalCurrencyCombined metalCurrencyCombined)
        {
            if (metalCurrencyCombined.Start >= metalCurrencyCombined.End)
            {
                throw new Exception("start date must be before end date");
            }

            //unknow in which currency metal service is going to provide data
            metalCurrencyCombined.MetalPrices = GetMetalPrices(
                metalCurrencyCombined.Metal,
                metalCurrencyCombined.Start,
                metalCurrencyCombined.End)
                                                .Result;

            metalCurrencyCombined.OperationStatus.AppendLine("loaded metal prices");

            //need to get exchange rate to calculate metal price in different currency
            if (metalCurrencyCombined.MetalPrices.Currency != metalCurrencyCombined.Currency)
            {
                metalCurrencyCombined.CurrencyRates = GetCurrencyRates(
                    metalCurrencyCombined.MetalPrices.Currency,
                    metalCurrencyCombined.Currency,
                    metalCurrencyCombined.Start,
                    metalCurrencyCombined.End)
                                                      .Result;

                metalCurrencyCombined.OperationStatus.AppendLine("loaded currency exchanges");
            }
            else
            {
                metalCurrencyCombined.OperationStatus.AppendLine("no need to load currency");
            }

            if (metalCurrencyCombined.MetalPrices == null)
            {
                throw new Exception("metal data not provided");
            }

            //currency exchange rates can be null if metal is already in expected currency
            if (metalCurrencyCombined.MetalPrices.Currency != metalCurrencyCombined.Currency &&
                metalCurrencyCombined.CurrencyRates == null)
            {
                throw new Exception("currency data not provided when needed");
            }

            return(metalCurrencyCombined);
        }
コード例 #3
0
        public MetalCurrencyCombined Execute(MetalCurrencyCombined metalCurrencyCombined)
        {
            var metalPricesFilled =
                FillMissingDates(metalCurrencyCombined.MetalPrices.Prices,
                                 metalCurrencyCombined.Start, metalCurrencyCombined.End);

            metalCurrencyCombined.MetalPrices.Prices = metalPricesFilled;

            if (metalCurrencyCombined.CurrencyRates != null)
            {
                var currencyRatesFilled =
                    FillMissingDates(metalCurrencyCombined.CurrencyRates.Rates,
                                     metalCurrencyCombined.Start, metalCurrencyCombined.End);

                metalCurrencyCombined.CurrencyRates.Rates = currencyRatesFilled;
            }

            return(metalCurrencyCombined);
        }
コード例 #4
0
        public MetalCurrencyCombined Execute(MetalCurrencyCombined metalCurrencyCombined)
        {
            var metalPricesFilled = metalCurrencyCombined.MetalPrices.Prices.Cast <ValueDate>().ToList();
            var ratesFilled       = metalCurrencyCombined.CurrencyRates.Rates.Cast <ValueDate>().ToList();//TODO: null thrown here on localhost - investigate https://localhost:44314/api/GoldCharts/USD/Silver/2000-1-1/2009-3-31

            if (metalPricesFilled.Count != ratesFilled.Count)
            {
                throw new Exception("number of days for currency and metal not equal");
            }

            var prices = new List <ValueDate>();

            foreach (var p in metalPricesFilled)
            {
                var r = ratesFilled.FirstOrDefault(e => e.Date == p.Date);

                if (r == null)
                {
                    continue;
                }

                prices.Add(new ValueDate
                {
                    Date  = p.Date,
                    Value = p.Value * r.Value
                });
            }

            metalCurrencyCombined.MetalPrices = new MetalPrices
            {
                DataSource = metalCurrencyCombined.MetalPrices.DataSource,
                Currency   = metalCurrencyCombined.MetalPrices.Currency,
                Prices     = prices
            };

            return(metalCurrencyCombined);
        }