예제 #1
0
        /// <summary>
        /// Parse Json data from Open Exchange Rates web service
        /// </summary>
        /// <param name="jsonString">received json string</param>
        /// <returns>list of parsed exchange rates history object</returns>
        public static List <ExchangeRatesHistory> ParseHistoricalJsonString(string jsonString)
        {
            List <ExchangeRatesHistory> parsedObjects = new List <ExchangeRatesHistory>();
            JObject parsedJObject = JObject.Parse(jsonString);

            if (parsedJObject.HasValues && parsedJObject.SelectToken("rates") != null && parsedJObject.SelectToken("timestamp") != null)
            {
                double timestamp = parsedJObject.SelectToken("timestamp").Value <double>();
                //convert from javascript timestamp to DateTime
                DateTime parsedDate = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(timestamp).Date;
                foreach (var allowedCurrency in Parameters.Instance.Config.AllowedCurrencyCodes)
                {
                    AllowedCurrencyConfigElement configElement = (AllowedCurrencyConfigElement)allowedCurrency;

                    if (parsedJObject.SelectToken("rates").SelectToken(configElement.Code) != null)
                    {
                        double parsedRate = parsedJObject.SelectToken("rates").SelectToken(configElement.Code).Value <double>();
                        ExchangeRatesHistory parsedRateHistoryItem = new ExchangeRatesHistory
                        {
                            CurrencyRate = parsedRate,
                            Date         = parsedDate,
                            Currency     = new Currency {
                                ServiceCode = configElement.Code
                            }
                        };
                        parsedObjects.Add(parsedRateHistoryItem);
                    }
                }
            }

            return(parsedObjects);
        }
예제 #2
0
        public void MakePrediction_WithSimpleSample_ReturnCorrectResult()
        {
            var currencyHistory = new ExchangeRatesHistory(new List <Tuple <long, double> >
            {
                new Tuple <long, double>(1, 10),
                new Tuple <long, double>(2, 15),
                new Tuple <long, double>(3, 20),
                new Tuple <long, double>(4, 25)
            });
            var unixTimeStamp    = DateTimeOffset.FromUnixTimeSeconds(5).UtcDateTime;
            var predictionResult = currencyHistory.MakePrediction(unixTimeStamp);
            var rSquared         = currencyHistory.GetRSquared();

            Assert.Equal(30, predictionResult);
            Assert.Equal(1, rSquared);
        }
예제 #3
0
        public void MakePrediction_WithTimeStampSample_ReturnCorrectResult()
        {
            var currencyHistory = new ExchangeRatesHistory(new List <Tuple <long, double> >
            {
                new Tuple <long, double>(CreateUnixTimeStamp(2018, 8, 15), 23114.292672),
                new Tuple <long, double>(CreateUnixTimeStamp(2018, 9, 15), 23096.240172),
                new Tuple <long, double>(CreateUnixTimeStamp(2018, 10, 15), 23245.690172),
                new Tuple <long, double>(CreateUnixTimeStamp(2018, 11, 15), 23234.180172),
                new Tuple <long, double>(CreateUnixTimeStamp(2018, 12, 15), 23236.342257)
            });

            var predictionResult = currencyHistory.MakePrediction(new DateTime(2019, 2, 15));
            var rSquared         = currencyHistory.GetRSquared();

            Assert.Equal(23338.5313, predictionResult);
            Assert.Equal(0.6729, rSquared);
        }
예제 #4
0
        /// <summary>
        /// Get list of exchange rates chart datapoints by model's criteria.
        /// </summary>
        /// <param name="statisticsModel"></param>
        /// <returns></returns>
        private List <CurrencyChartDataPoint> GetExchangeRates(ExchangeRatesStatisticsModel statisticsModel)
        {
            List <ExchangeRatesHistory>   baseRatesHistory       = GetExchangeRatesForCurrency(statisticsModel, statisticsModel.BaseCurrencyCode);
            List <ExchangeRatesHistory>   relationalRatesHistory = GetExchangeRatesForCurrency(statisticsModel, statisticsModel.RelationalCurrencyCode);
            List <CurrencyChartDataPoint> resultingDataForChart  = new List <CurrencyChartDataPoint>();

            foreach (ExchangeRatesHistory baseRateHistoryItem in baseRatesHistory)
            {
                //database keeps rates only for USD, so we have to calculate any other rate
                ExchangeRatesHistory relationalRateHistoryItem = relationalRatesHistory.Where(r => r.Date == baseRateHistoryItem.Date).First();
                double calculatedRate = calculatedRate = relationalRateHistoryItem.CurrencyRate / baseRateHistoryItem.CurrencyRate;
                //remember calculated rate in chart data
                resultingDataForChart.Add(new CurrencyChartDataPoint(baseRateHistoryItem.Date, calculatedRate));
            }

            return(resultingDataForChart.OrderBy(er => er.Date).ToList());
        }
        /// <exception cref="CurrencyNotFoundException">Throw if target currencies are not found from sample list.</exception>
        /// <exception cref="ArgumentException">Throw if sample is null or has less than 2 items</exception>
        /// <returns></returns>
        public PredictionResult MakePredictionFromSample(string fromCurrency, string toCurrency, DateTime targetDate, IEnumerable <OpenExchangeRateResult> sample)
        {
            if (sample == null)
            {
                throw new ArgumentException("Sample cannot be null", nameof(sample));
            }

            var specificSample = sample.Select(s => MakeSamplePoint(s, fromCurrency, toCurrency))
                                 .ToList();

            var ratesHistory = new ExchangeRatesHistory(specificSample);

            return(new PredictionResult
            {
                PredictionRate = ratesHistory.MakePrediction(targetDate),
                RSquared = ratesHistory.GetRSquared()
            });
        }