public ForexModel MapToJsonModel(string updatedOn, IEnumerable <Rate> rates)
        {
            var currencyModels = new List <CurrencyModel>();

            foreach (var rate in rates)
            {
                var detailsModel = new CurrencyDetailsModel(rate.CurrencyCode, rate.CurrencyName);

                var rateModel = new CurrencyRateModel(
                    rate.BuyRate.ToDouble(),
                    rate.SellRate.ToDouble(),
                    rate.Multiply.ToInt()
                    );

                var guideModel = new CurrencyGuideModel(
                    rate.CurrencyGuide.From,
                    rate.CurrencyGuide.To,
                    rate.CurrencyGuide.Amount.ToInt(),
                    rate.CurrencyGuide.Value.ToDouble()
                    );

                var currencyModel = new CurrencyModel(detailsModel, rateModel, guideModel);

                currencyModels.Add(currencyModel);
            }

            var mappedModel = new ForexModel(DateTime.Parse(updatedOn), currencyModels);

            return(mappedModel);
        }
Example #2
0
        private void Form2_Load(object sender, EventArgs e)
        {
            cmbCurrency1.Items.AddRange(data.ToArray());
            cmbCurrency1.DisplayMember = "CurrencyCode";

            cmbCurrency2.Items.AddRange(data.ToArray());
            cmbCurrency2.DisplayMember = "CurrencyCode";
            //combobox içerisinde görüntülenecek olan alanları displayMember ile yazıyoruz.

            cmbCurrencyType.DataSource = Enum.GetValues(typeof(ForexType));


            //Kullanım Şekli
            ForexModel selection1 = f.GetByCurrencyCode("EUR");
            ForexModel selection2 = f.GetByCurrencyCode("USD");
            ForexType  type       = ForexType.ForexSelling;

            //ForexManager ex = new ForexManager();
            //decimal result = ex.Exchange(type, selection1, selection2, 100);

            //lblForexValue.Text = result +" "+ selection2.CurrencyCode;


            ForexManagerTwo t = new ForexManagerTwo(new ForexExchangeUnitOne());

            t.Exchange(selection1, selection2, 100, type);
        }
        public static void CalculateCpiDifferenceQuaterly()
        {
            using (ForexModel context = new ForexModel())
            {
                var ukRates = context.CpiQuaterlyRates.Where(c => c.Country == "\"GBR\"").OrderBy(y => y.Date).ToList();
                var usRates = context.CpiQuaterlyRates.Where(c => c.Country == "\"USA\"").OrderBy(y => y.Date).ToList();

                for (int i = 0; i < ukRates.Count; i++)
                {
                    CpiQuaterlyRate ukRate = ukRates[i];
                    CpiQuaterlyRate usRate = usRates[i];

                    var year   = Int32.Parse(ukRate.Date.Substring(1, 4));
                    var quater = Int32.Parse(ukRate.Date.Substring(7, 1));

                    CpiQuaterlyDifference difference = new CpiQuaterlyDifference
                    {
                        Id         = i,
                        Year       = year,
                        Quarter    = quater,
                        Difference = ukRate.Rate - usRate.Rate
                    };

                    context.CpiQuaterlyDifferences.Add(difference);
                }

                context.SaveChanges();
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //unboxing
            //tüm propertyleri bir arada tag den yakalarız.
            ForexModel model = (ForexModel)listView1.FocusedItem.Tag;

            lblName.Text    = model.CurrencyCode;
            lblSelling.Text = model.EffectiveBankNoteSelling;
            lblBuying.Text  = model.EffectiveBankNoteBuying;
        }
Example #5
0
        private void btnExchange_Click(object sender, EventArgs e)
        {   //unboxing iÅŸlemleri
            ForexType  _type       = (ForexType)cmbCurrencyType.SelectedItem;
            ForexModel _selection1 = (ForexModel)cmbCurrency1.SelectedItem;
            ForexModel _selection2 = (ForexModel)cmbCurrency2.SelectedItem;
            decimal    amount      = decimal.Parse(txtCurrencyAmount.Text);

            ForexManager manager = new ForexManager();

            lblForexValue.Text = manager.Exchange(_type, _selection1, _selection2, amount).ToString() + " " + _selection2.CurrencyCode;
        }
Example #6
0
        public decimal Exchange(ForexType type, ForexModel from, ForexModel to, decimal amount)
        {
            decimal result = 0;

            if (type == ForexType.ForexBuying)
            {
                result = service.Exchange(decimal.Parse(from.ForexBuying), decimal.Parse(to.ForexBuying), amount);
            }
            else if (type == ForexType.ForexSelling)
            {
                result = service.Exchange(decimal.Parse(from.ForexSelling), decimal.Parse(to.ForexSelling), amount);
            }

            return(result);
        }
Example #7
0
        public async Task <ForexModel> GetHTTPForexAsync()
        {
            this.logger.LogWarning("GetHTTPForex starts at " + Environment.TickCount.ToString());
            ForexModel forexModel = new ForexModel();

            List <ForexTemp> currencyList = await GetCurrenciesListAsync();

            HtmlNodeCollection table = GetTable();

            foreach (HtmlNode node in table)
            {
                HtmlNodeCollection trs = node.SelectNodes("tr");
                foreach (HtmlNode nodeTRs in trs)
                {
                    HtmlNodeCollection tds = nodeTRs.SelectNodes("td");
                    if (tds.Count == 3)
                    {
                        foreach (ForexTemp forexTemp in currencyList)
                        {
                            if (tds[0].InnerText.ToUpper().Trim() == forexTemp.LongName.ToUpper().Trim())
                            {
                                HtmlNodeCollection links = tds[1].SelectNodes("a");
                                if (links.Count > 0)
                                {
                                    string val = links[0].InnerText;
                                    forexTemp.Value = decimal.Parse(val);
                                }
                            }
                        }
                    }
                }
            }

            forexModel.USD = currencyList.Where(w => w.Name == "USD").FirstOrDefault().Value;
            forexModel.SGD = currencyList.Where(w => w.Name == "SGD").FirstOrDefault().Value;
            forexModel.NZD = currencyList.Where(w => w.Name == "NZD").FirstOrDefault().Value;
            forexModel.MYR = currencyList.Where(w => w.Name == "MYR").FirstOrDefault().Value;
            forexModel.JPY = currencyList.Where(w => w.Name == "JPY").FirstOrDefault().Value;
            forexModel.IDR = currencyList.Where(w => w.Name == "IDR").FirstOrDefault().Value;
            forexModel.GBP = currencyList.Where(w => w.Name == "GBP").FirstOrDefault().Value;
            forexModel.EUR = currencyList.Where(w => w.Name == "EUR").FirstOrDefault().Value;
            forexModel.CNY = currencyList.Where(w => w.Name == "CNY").FirstOrDefault().Value;
            forexModel.CAD = currencyList.Where(w => w.Name == "CAD").FirstOrDefault().Value;

            this.logger.LogWarning("GetHTTPForex stops at " + Environment.TickCount.ToString());
            return(forexModel);
        }
        public static void UploadAnalyticQuarterlyRecords()
        {
            using (ForexModel context = new ForexModel())
            {
                var cpiQuarterly          = context.CpiQuaterlyDifferences.ToList();
                var interestRateQuarterly = context.InterestRateQuaterlyDifferences.ToList();

                for (int i = 4; i < interestRateQuarterly.Count; i++)
                {
                    int year          = interestRateQuarterly[i].Year;
                    int quarter       = interestRateQuarterly[i].Quarter;
                    int eligibleMonth = DetermineEligibleStartMonth(quarter);
                    AnalyticQuarterlyRecord record = new AnalyticQuarterlyRecord
                    {
                        Id                     = i,
                        Year                   = year,
                        Quarter                = quarter,
                        CpiDifference          = cpiQuarterly[i].Difference,
                        CpiTendency            = CalculateTendencyForQuarterly(cpiQuarterly, year, quarter),
                        InterestRateDifference = interestRateQuarterly
                                                 .Where(c => c.Year == year && c.Quarter == quarter).Select(v => v.Difference).Single(),
                        InterestRateTendency = CalculateTendencyForQuarterly(interestRateQuarterly, year + 1, quarter),
                        TradeBalanceByUk     = context.TradeBalances.Where(c => c.Country == "UK" && c.Year == year)
                                               .Select(v => v.Balance).Single(),
                        TradeBalanceByUs = context.TradeBalances.Where(c => c.Country == "USA" && c.Year == year)
                                           .Select(v => v.Balance).Single(),
                        DebtGrowthUk = context.DebtGrowths.Where(c => c.Country == "UK" && c.Year == year)
                                       .Select(v => v.Growth)
                                       .Single(),
                        DebtGrowthUs = context.DebtGrowths.Where(c => c.Country == "USA" && c.Year == year)
                                       .Select(v => v.Growth)
                                       .Single(),
                        ForexTendency = context.GbpUsdGrowthQuaterlies
                                        .Where(y => y.Date.Year == year - 1 ||
                                               (y.Date.Year == year && y.Date.Month <= eligibleMonth))
                                        .Select(v => v.CloseGrowth).Average(),
                        Outcome = GetOutcomeLabel(context.GbpUsdGrowthQuaterlies
                                                  .Where(y => y.Date.Year == year && y.Date.Month == eligibleMonth)
                                                  .Select(v => v.CloseGrowth).Single())
                    };


                    context.AnalyticQuarterlyRecords.Add(record);
                }
                context.SaveChanges();
            }
        }
        public static void UploadAnalyticDailyRecords()
        {
            using (ForexModel context = new ForexModel())
            {
                var forexGrowths = context.GbpUsdGrowth.ToList();

                for (int i = 260; i < forexGrowths.Count; i++)
                {
                    var date    = forexGrowths[i].Date;
                    var recDate = date.AddMonths(-3);
                    AnalyticDailyRecord record = new AnalyticDailyRecord();
                    record.Id            = i;
                    record.Date          = date;
                    record.CpiDifference = context.CpiYearlyDifferences.Where(d => d.Year == date.Year)
                                           .Select(v => v.Difference).Single();
                    record.CpiTendency            = CalculateTendency(context.CpiQuaterlyDifferences.ToList(), date.Year);
                    record.InterestRateDifference = context.InterestRateYearlyDifferences.Where(c => c.Year == date.Year)
                                                    .Select(v => v.Difference).Single();
                    record.InterestRateTendency = CalculateTendency(context.InterestRateQuaterlyDifferences.ToList(), date.Year);
                    record.TradeBalanceByUk     = context.TradeBalances.Where(c => c.Country == "UK" && c.Year == date.Year)
                                                  .Select(v => v.Balance).Single();
                    record.TradeBalanceByUs = context.TradeBalances.Where(c => c.Country == "USA" && c.Year == date.Year)
                                              .Select(v => v.Balance).Single();
                    record.DebtGrowthUk = context.DebtGrowths.Where(c => c.Country == "UK" && c.Year == date.Year)
                                          .Select(v => v.Growth)
                                          .Single();
                    record.DebtGrowthUs = context.DebtGrowths.Where(c => c.Country == "USA" && c.Year == date.Year)
                                          .Select(v => v.Growth)
                                          .Single();
                    record.ForexTendency = context.GbpUsdGrowth.Where(y => y.Date <date && y.Date> recDate)
                                           .Select(v => v.CloseGrowth).Average();
                    record.Outcome = GetOutcomeLabel(forexGrowths.Where(d => d.Date == date).Select(v => v.CloseGrowth)
                                                     .Single());

                    context.AnalyticDailyRecords.Add(record);
                }
                context.SaveChanges();
            }
        }
Example #10
0
        private async void RunForex()
        {
            logger.LogInformation("RunForex starts at " + Environment.TickCount);
            DataContext dataContext = GetDataContext();

            logger.LogInformation("dataContext == null ? " + (dataContext == null));
            ForexService forexService = new ForexService(dataContext, configuration, logger);

            if (forexService != null)
            {
                ForexModel forexModel = forexService.GetHTTPForex();
                if (forexModel != null)
                {
                    Forex forex = new Models.Forex(forexModel);
                    if (forex != null)
                    {
                        bool result = await forexService.Update(forex);
                    }
                }
            }
            logger.LogInformation("RunForex stops at " + Environment.TickCount);
        }
        public static void CalculateTradeBalance(string country)
        {
            using (ForexModel context = new ForexModel())
            {
                var data    = context.Trades.Where(c => c.Country == country);
                var exports = data.Where(f => f.Flow == 2).OrderBy(y => y.Year).ToList();
                var imports = data.Where(f => f.Flow == 1).OrderBy(y => y.Year).ToList();

                for (int i = 0; i < exports.Count; i++)
                {
                    TradeBalance balance = new TradeBalance
                    {
                        Country = country,
                        Year    = exports[i].Year,
                        Balance = exports[i].Value - imports[i].Value
                    };

                    context.TradeBalances.Add(balance);
                }

                context.SaveChanges();
            }
        }
Example #12
0
        public static void CalculateDebtGrowth(string country)
        {
            using (ForexModel context = new ForexModel())
            {
                var data = context.Debts.Where(c => c.Country == country).ToList();
                for (int i = 1; i < data.Count; i++)
                {
                    double currentValue  = data[i].PercentageDebt;
                    double previousValue = data[i - 1].PercentageDebt;

                    var item = new DebtGrowth
                    {
                        Country = country,
                        Year    = data[i].Year,
                        Growth  = CalculateGrowth(previousValue, currentValue)
                    };

                    context.DebtGrowths.Add(item);
                }

                context.SaveChanges();
            }
        }
        public static void UploadAnalyticRecords()
        {
            using (ForexModel context = new ForexModel())
            {
                var cpiYearly = context.CpiYearlyDifferences.ToList();
                for (int i = 1; i < cpiYearly.Count; i++)
                {
                    int            year   = cpiYearly[i].Year;
                    AnalyticRecord record = new AnalyticRecord();
                    record.Id                     = i;
                    record.Year                   = year;
                    record.CpiDifference          = cpiYearly[i].Difference;
                    record.CpiTendency            = CalculateTendency(context.CpiQuaterlyDifferences.ToList(), year);
                    record.InterestRateDifference = context.InterestRateYearlyDifferences.Where(c => c.Year == year)
                                                    .Select(v => v.Difference).Single();
                    record.InterestRateTendency =
                        CalculateTendency(context.InterestRateQuaterlyDifferences.ToList(), year);
                    record.TradeBalanceByUk = context.TradeBalances.Where(c => c.Country == "UK" && c.Year == year)
                                              .Select(v => v.Balance).Single();
                    record.TradeBalanceByUs = context.TradeBalances.Where(c => c.Country == "USA" && c.Year == year)
                                              .Select(v => v.Balance).Single();
                    record.DebtGrowthUk = context.DebtGrowths.Where(c => c.Country == "UK" && c.Year == year)
                                          .Select(v => v.Growth)
                                          .Single();
                    record.DebtGrowthUs = context.DebtGrowths.Where(c => c.Country == "USA" && c.Year == year)
                                          .Select(v => v.Growth)
                                          .Single();
                    record.ForexTendency = context.GbpUsdGrowthQuaterlies.Where(y => y.Date.Year == year - 1)
                                           .Select(v => v.CloseGrowth).Average();
                    record.Outcome = GetOutcomeLabel(context.GbpUsdGrowth.Where(y => y.Date.Year == year)
                                                     .Select(v => v.CloseGrowth).First());

                    context.AnalyticRecords.Add(record);
                }
                context.SaveChanges();
            }
        }
Example #14
0
        public void GetHTTPForex_Test()
        {
            ForexModel model = forexService.GetHTTPForex();

            Assert.IsTrue(true);
        }
        public static void CalculateInterestRateDifference(Period period)
        {
            using (ForexModel context = new ForexModel())
            {
                var ukDataRaw = context.UkInterestRates.ToList();

                var ukData = ukDataRaw.Select(v => new InterestRate
                {
                    Country = "UK",
                    Rate    = v.Rate,
                    Date    = ParseDate(v.Date)
                }).OrderBy(d => d.Date)
                             .ToList();

                var usData = context.UsMonthlyInterestRates.Select(v => new InterestRate
                {
                    Country = "USA",
                    Rate    = v.Rate,
                    Date    = v.Date
                }).ToList();

                int ukMinYear = ukData.Select(y => y.Date.Year).Min();
                int usMinYear = usData.Select(y => y.Date.Year).Min();
                int minYear   = ukMinYear >= usMinYear ? ukMinYear : usMinYear;

                int ukMaxYear = ukData.Select(y => y.Date.Year).Max();
                int usMaxYear = usData.Select(y => y.Date.Year).Max();
                int maxYear   = ukMaxYear <= usMaxYear ? ukMaxYear : usMaxYear;

                for (int i = minYear; i <= maxYear; i++)
                {
                    if (period == Period.Quaterly)
                    {
                        int month = 1;
                        for (int j = 1; j <= 4; j++)
                        {
                            if (!(i == 2017 && j > 1))
                            {
                                DateTime ukDate = DateHelpers.GetFirstAvailableDay(ukData, i, month);
                                DateTime usDate = DateHelpers.GetFirstAvailableDay(usData, i, month);

                                double ukRate = ukData.Where(d => d.Date == ukDate).Select(r => r.Rate).First();
                                double usRate = usData.Where(d => d.Date == usDate).Select(r => r.Rate).First();

                                InterestRateQuaterlyDifference difference = new InterestRateQuaterlyDifference
                                {
                                    Year       = i,
                                    Quarter    = j,
                                    Difference = ukRate - usRate
                                };

                                context.InterestRateQuaterlyDifferences.Add(difference);
                                month += 3;
                            }
                        }
                    }
                    else if (period == Period.Yearly)
                    {
                        DateTime ukDate = DateHelpers.GetFirstAvailableDay(ukData, i, 1);
                        DateTime usDate = DateHelpers.GetFirstAvailableDay(usData, i, 1);

                        double ukRate = ukData.Where(d => d.Date == ukDate).Select(r => r.Rate).First();
                        double usRate = usData.Where(d => d.Date == usDate).Select(r => r.Rate).First();

                        InterestRateYearlyDifference difference = new InterestRateYearlyDifference
                        {
                            Year       = i,
                            Difference = ukRate - usRate
                        };

                        context.InterestRateYearlyDifferences.Add(difference);
                    }
                }

                context.SaveChanges();
            }
        }
 public decimal Exchange(ForexModel from, ForexModel to, decimal amount, ForexType type)
 {
     return(exchangeService.Exchange(decimal.Parse(from.ForexSelling), decimal.Parse(to.ForexSelling), amount));
 }