public static List <ExchangeRateBO> GenerateExchangeRates(DateTime targetDate, List <decimal> rates)
        {
            var            samples = new List <ExchangeRateBO>();
            DateTime       date;
            ExchangeRateBO exchangeRateItem;

            for (var i = 1; i < 13; i++)
            {
                date = targetDate.AddMonths(-1 * i);
                Dictionary <string, decimal> ratesMap = new Dictionary <string, decimal>
                {
                    { "TRY", rates[i - 1] }
                };
                exchangeRateItem = new ExchangeRateBO(
                    date,
                    rates: ratesMap);

                samples.Add(exchangeRateItem);
            }

            return(samples);
        }
Exemplo n.º 2
0
        private void SetAmountByCurrency(Currency currency)
        {
            if (currency == null)
            {
                return;
            }
            //Get default currency
            CurrencyBO   currencyBO          = new CurrencyBO();
            CurrencyType defaultCurrencyType = currencyBO.GetDefaultCurrencyType(session);

            //If selected currency has type is default type
            if (defaultCurrencyType.Equals(currency.CurrencyTypeId))
            {
                spinExchangeRate.Number = 1;
            }
            else
            {
                ExchangeRateBO exchangeRateBO = new ExchangeRateBO();
                ExchangeRate   exchangeRate   = exchangeRateBO.GetLatest(session, currency.CurrencyId);
                if (exchangeRate != null)
                {
                    spinExchangeRate.Number = (decimal)exchangeRate.Rate;
                    if (spinAmount.Number >= 0 && spinExchangeRate.Number >= 0)
                    {
                        spinConvertedAmount.Number = spinAmount.Number * spinExchangeRate.Number;
                        number = spinExchangeRate.Number.ToString();
                    }
                    else
                    {
                        spinConvertedAmount.Text = null;
                    }
                }
                else
                {
                    spinExchangeRate.Text    = null;
                    spinConvertedAmount.Text = null;
                }
            }
        }
Exemplo n.º 3
0
        public async Task PredictShouldCorrect()
        {
            var targetDate = new DateTime(2017, 1, 15);

            var numOfSamples   = 12;
            var baseCurrency   = "USD";
            var targetCurrency = "TRY";
            var timeSeries     = Enumerable.Range(1, numOfSamples).Select(t => targetDate.AddMonths(-t));
            var rates          = new List <decimal>
            {
                3.043590m, 2.945926m, 2.893866m,
                2.854589m, 2.970913m, 2.925991m,
                2.990882m, 2.944941m, 2.970704m,
                3.089218m, 3.284672m, 3.503773m
            };

            rates.Reverse();

            var timeSeriesGenerator               = new Mock <ITimeSeriesService>();
            var exchangeRatesResource             = new Mock <IExchangeRateSourceService>();
            var regressionEquationFactory         = new Mock <IRegressionFomularRepository>();
            IExchangeRatePredictService predictor = new ExchangeRatePredictService(
                timeSeriesGenerator.Object,
                exchangeRatesResource.Object,
                regressionEquationFactory.Object);

            timeSeriesGenerator
            .Setup(t => t.MoveBackwardByMonth(targetDate, numOfSamples, false))
            .Returns(timeSeries);

            foreach (var date in timeSeries)
            {
                var exchangeRate = new ExchangeRateBO
                {
                    Base      = baseCurrency,
                    TimeStamp = date,
                    Rates     = new Dictionary <string, decimal> {
                        { targetCurrency, rates[date.Month - 1] }
                    }
                };

                var exR = exchangeRatesResource.Setup(t =>
                                                      t.GetHistorical(new Models.API.ExchangeRateRequest
                {
                    Date             = date,
                    Base             = It.IsAny <string>(),
                    TargetCurrencies = It.IsAny <string[]>(),
                }, It.IsAny <CancellationToken>()));


                exR.ReturnsAsync(exchangeRate);
            }

            regressionEquationFactory
            .Setup(t => t.Create(It.IsAny <List <DataPoint2dBO> >()))
            .Returns(new RegressionFomularBO(0.035126416m, 2.806600379m));

            decimal expectedResult = 3.263m;


            var result = await predictor.PredictAsync("USD", "TRY", targetDate, numOfSamples);

            result.AlmostEquals(expectedResult, 3).Should().BeTrue();
        }