private void PopulateLatestFromHistoricExchangeRates()
        {
            try
            {
                if (this.HistoricCurrencyExchangeRatesList != null && this.HistoricCurrencyExchangeRatesList.Count() > 0)
                {
                    //Get Latest Currency Exchange Rates From HistoricCurrencyExchangeRatesList
                    string latestDate = this.HistoricCurrencyExchangeRatesList.Select(type => DateTime.Parse(type.Date)).Max(type => type.Date).ToString(Constants.Constants.DateFormat);
                    this.LatestCurrencyExchangeRatesList = this.HistoricCurrencyExchangeRatesList.Where(type => type.Date == latestDate).ToList();
                }

                if (this.LatestCurrencyExchangeRatesList == null || this.LatestCurrencyExchangeRatesList.Count() <= 0)
                {
                    try
                    {
                        //Get Latest Currency Exchange Rates From Hosted Database
                        this.LatestCurrencyExchangeRatesList = CurrencyExchangeRateCalculatorLibrary.GetLatestCurrencyExchangeRatesFromDatabase_HostedDB();
                    }
                    catch (Exception ex)
                    {
                        //string ExceptionMessage = string.Empty;
                        //if (string.IsNullOrWhiteSpace(ex.Message) == false)
                        //{
                        //    ExceptionMessage = Environment.NewLine + "Exception Message: " + ex.Message;
                        //}
                        //DisplayAlert("Exception! Currency Exchange Dataset Load", "Error During Currency Exchange Dataset Load." + ExceptionMessage, "OK");
                    }

                    if (this.LatestCurrencyExchangeRatesList == null || this.LatestCurrencyExchangeRatesList.Count() <= 0)
                    {
                        //Get Latest Currency Exchange Rates From WebAPI Call
                        this.LatestCurrencyExchangeRatesList = CurrencyExchangeRateCalculatorLibrary.GetLatestCurrencyExchangeRatesFromWebAPI();
                    }

                    if (this.LatestCurrencyExchangeRatesList == null || this.LatestCurrencyExchangeRatesList.Count() <= 0)
                    {
                        //Get Latest Currency Exchange Rates From Database
                        this.LatestCurrencyExchangeRatesList = DatabaseServices.GetLatestCurrencyExchangeRatesFromDatabase();
                    }
                }
            }
            catch (Exception ex)
            {
                string ExceptionMessage = string.Empty;
                if (string.IsNullOrWhiteSpace(ex.Message) == false)
                {
                    ExceptionMessage = Environment.NewLine + "Exception Message: " + ex.Message;
                }
                DisplayAlert("Exception! PopulateLatestFromHistoricExchangeRates", "Error During Latest Currency Exchange Rates Load." + ExceptionMessage, "OK");
            }
        }
        private void CalculateCurrencyArbitrage(bool ignoreMissingInputs = false)
        {
            //Hide Unused Controls
            HideUnusedControls();

            if (picker_BaseCurrencyPicker.SelectedItem != null &&
                picker_TargetCurrencyPicker.SelectedItem != null &&
                picker_DegreePicker.SelectedItem != null)
            {
                string baseCurrencyCode          = ((CurrencyCodes)picker_BaseCurrencyPicker.SelectedItem).CurrencyCode;
                string baseCurrencyDescription   = ((CurrencyCodes)picker_BaseCurrencyPicker.SelectedItem).CurrencyDescription;
                string targetCurrencyCode        = ((CurrencyCodes)picker_TargetCurrencyPicker.SelectedItem).CurrencyCode;
                string targetCurrencyDescription = ((CurrencyCodes)picker_TargetCurrencyPicker.SelectedItem).CurrencyDescription;
                int    degreeKey   = ((DegreeKeyValue)picker_DegreePicker.SelectedItem).Key;
                string degreeValue = ((DegreeKeyValue)picker_DegreePicker.SelectedItem).Value;

                string latestDate = this.LatestCurrencyExchangeRatesList.Select(type => DateTime.Parse(type.Date)).Max(type => type.Date).ToString(Constants.Constants.DateFormat);

                List <CurrencyArbitrageLog> CurrencyArbitrageLogList = new List <CurrencyArbitrageLog>();

                try
                {
                    //Get Currency Arbitrage Rates From Hosted Database
                    CurrencyArbitrageLogList = CurrencyExchangeRateCalculatorLibrary.GetCurrencyArbitrageLogFromDatabase_HostedDB
                                               (
                        paramBaseCurrencyCode: baseCurrencyCode,
                        paramTargetCurrencyCode: targetCurrencyCode,
                        paramDegree: degreeKey
                                               );

                    if (CurrencyArbitrageLogList != null && CurrencyArbitrageLogList.Count() > 0)
                    {
                        //Get Latest CurrencyArbitrageLogDate From CurrencyArbitrageLogList
                        string latestCurrencyArbitrageLogDate = CurrencyArbitrageLogList.Select(type => DateTime.Parse(type.Date)).Max(type => type.Date).ToString(Constants.Constants.DateFormat);
                        CurrencyArbitrageLogList = CurrencyArbitrageLogList.Where(type => type.Date == latestCurrencyArbitrageLogDate && type.ActualValue > type.ImpliedValue).OrderByDescending(type => type.ActualValue).ThenByDescending(type => type.ImpliedValue).ToList();
                    }
                }
                catch (Exception ex)
                {
                    //string ExceptionMessage = string.Empty;
                    //if (string.IsNullOrWhiteSpace(ex.Message) == false)
                    //{
                    //    ExceptionMessage = Environment.NewLine + "Exception Message: " + ex.Message;
                    //}
                    //DisplayAlert("Exception! Calculate Currency Arbitrage", "Error While Calculating Currency Arbitrage From Hosted DB." + ExceptionMessage, "OK");
                }

                if (CurrencyArbitrageLogList == null || CurrencyArbitrageLogList.Count() == 0)
                {
                    CurrencyArbitrageLogList = CurrencyExchangeRateCalculatorLibrary.CalculateCurrencyArbitrage
                                               (
                        paramCurrencyExchangeRates: this.LatestCurrencyExchangeRatesList,
                        paramDate: latestDate,
                        paramBaseCurrencyCode: baseCurrencyCode,
                        paramTargetCurrencyCode: targetCurrencyCode,
                        paramDegree: degreeKey
                                               );
                }

                if (CurrencyArbitrageLogList != null && CurrencyArbitrageLogList.Count() > 0)
                {
                    string dateValue = this.LatestCurrencyExchangeRatesList.Select(type => type.Date).FirstOrDefault();

                    //Display Label
                    label_DisplayMessage = new Label()
                    {
                        LineBreakMode           = LineBreakMode.WordWrap,
                        Text                    = "Currency Arbitrage Rates (" + dateValue + ")",
                        IsEnabled               = false,
                        IsVisible               = true,
                        FontAttributes          = FontAttributes.Bold,
                        FontSize                = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment   = TextAlignment.Center,
                        TextColor               = Color.DarkBlue
                    };
                    stackLayout.Children.Add(label_DisplayMessage);

                    label_AdditionalDisplayMessage = new Label()
                    {
                        LineBreakMode           = LineBreakMode.WordWrap,
                        Text                    = "Base Currency: " + baseCurrencyDescription + Environment.NewLine + "Target Currency: " + targetCurrencyDescription,
                        IsEnabled               = false,
                        IsVisible               = true,
                        FontAttributes          = FontAttributes.Bold,
                        FontSize                = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment   = TextAlignment.Center,
                        TextColor               = Color.DarkBlue
                    };
                    stackLayout.Children.Add(label_AdditionalDisplayMessage);

                    //Box View Line
                    boxView_BlackLine_DisplayMessage = new BoxView()
                    {
                        Color         = Color.DarkBlue,
                        HeightRequest = 3,
                        IsEnabled     = false,
                        IsVisible     = true,
                    };
                    stackLayout.Children.Add(boxView_BlackLine_DisplayMessage);

                    //Grid - Header
                    gridHeader = new Grid()
                    {
                        //VerticalOptions = LayoutOptions.FillAndExpand,
                        ColumnDefinitions =
                        {
                            //new ColumnDefinition { Width = new GridLength (0.22, GridUnitType.Star) },
                            new ColumnDefinition {
                                Width = new GridLength(0.22, GridUnitType.Star)
                            },
                            //new ColumnDefinition { Width = new GridLength (0.22, GridUnitType.Star) },
                            new ColumnDefinition {
                                Width = new GridLength(0.17, GridUnitType.Star)
                            },
                            new ColumnDefinition {
                                Width = new GridLength(0.17, GridUnitType.Star)
                            }
                        }
                    };

                    int rowHeaderNumber = 0;

                    Color labelHeaderBackgroundColor = Color.Orange;

                    Label BaseCurrencyCodeHeaderLabel = new Label {
                        Text = "BaseCurrency", TextColor = Color.DarkBlue, BackgroundColor = labelHeaderBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)), FontAttributes = FontAttributes.Bold
                    };
                    Label IntermediateCurrencyCodeHeaderLabel = new Label {
                        Text = "IntermediateCurrency", TextColor = Color.DarkBlue, BackgroundColor = labelHeaderBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)), FontAttributes = FontAttributes.Bold
                    };
                    Label TargetCurrencyCodeHeaderLabel = new Label {
                        Text = "TargetCurrency", TextColor = Color.DarkBlue, BackgroundColor = labelHeaderBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)), FontAttributes = FontAttributes.Bold
                    };
                    Label ImpliedValueHeaderLabel = new Label {
                        Text = "ImpliedValue", TextColor = Color.DarkBlue, BackgroundColor = labelHeaderBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)), FontAttributes = FontAttributes.Bold
                    };
                    Label ActualValueHeaderLabel = new Label {
                        Text = "ActualValue", TextColor = Color.DarkBlue, BackgroundColor = labelHeaderBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)), FontAttributes = FontAttributes.Bold
                    };

                    gridHeader.RowDefinitions.Add(new RowDefinition {
                        Height = GridLength.Auto
                    });

                    //gridHeader.Children.Add(BaseCurrencyCodeHeaderLabel, 0, rowHeaderNumber);
                    //gridHeader.Children.Add(IntermediateCurrencyCodeHeaderLabel, 1, rowHeaderNumber);
                    //gridHeader.Children.Add(TargetCurrencyCodeHeaderLabel, 2, rowHeaderNumber);
                    //gridHeader.Children.Add(ImpliedValueHeaderLabel, 3, rowHeaderNumber);
                    //gridHeader.Children.Add(ActualValueHeaderLabel, 4, rowHeaderNumber);

                    gridHeader.Children.Add(IntermediateCurrencyCodeHeaderLabel, 0, rowHeaderNumber);
                    gridHeader.Children.Add(ImpliedValueHeaderLabel, 1, rowHeaderNumber);
                    gridHeader.Children.Add(ActualValueHeaderLabel, 2, rowHeaderNumber);

                    stackLayout.Children.Add(gridHeader);

                    //Highlight Top Profitable Currency Arbitrage Exchanges
                    int highlightedProfitableExchanges = 0;

                    //Grid - Details
                    gridDetails = new Grid()
                    {
                        //VerticalOptions = LayoutOptions.FillAndExpand,
                        ColumnDefinitions =
                        {
                            //new ColumnDefinition { Width = new GridLength (0.22, GridUnitType.Star) },
                            new ColumnDefinition {
                                Width = new GridLength(0.22, GridUnitType.Star)
                            },
                            //new ColumnDefinition { Width = new GridLength (0.22, GridUnitType.Star) },
                            new ColumnDefinition {
                                Width = new GridLength(0.17, GridUnitType.Star)
                            },
                            new ColumnDefinition {
                                Width = new GridLength(0.17, GridUnitType.Star)
                            }
                        }
                    };
                    int rowDetailNumber = 0;
                    foreach (CurrencyArbitrageLog eachValue in CurrencyArbitrageLogList)
                    {
                        string  BaseCurrencyCode          = eachValue.BaseCurrencyCode;
                        string  IntermediateCurrencyCodes = eachValue.IntermediateCurrencyCodes;
                        string  TargetCurrencyCode        = eachValue.TargetCurrencyCode;
                        decimal ImpliedValue = eachValue.ImpliedValue;
                        decimal ActualValue  = eachValue.ActualValue;

                        string BaseCurrencyDescription   = this.PreferredCurrencyCodesList.Where(type => type.CurrencyCode == BaseCurrencyCode).Select(type => type.CurrencyDescription).FirstOrDefault();
                        string TargetCurrencyDescription = this.PreferredCurrencyCodesList.Where(type => type.CurrencyCode == TargetCurrencyCode).Select(type => type.CurrencyDescription).FirstOrDefault();

                        string IntermediateCurrencyDescription = string.Empty;
                        foreach (string eachIntermediateCurrencyCode in eachValue.IntermediateCurrencyCodes.Split(' ').ToList())
                        {
                            if (string.IsNullOrWhiteSpace(IntermediateCurrencyDescription) == false)
                            {
                                IntermediateCurrencyDescription = IntermediateCurrencyDescription + Environment.NewLine;
                            }

                            IntermediateCurrencyDescription = IntermediateCurrencyDescription + this.PreferredCurrencyCodesList.Where(type => type.CurrencyCode == eachIntermediateCurrencyCode).Select(type => type.CurrencyDescription).FirstOrDefault();
                        }

                        Color labelBackgroundColor = Color.Default;
                        if (highlightedProfitableExchanges >= Constants.Constants.HighlightProfitableExchanges)
                        {
                            if (rowDetailNumber % 2 == 0) //For Even & Odd Number Rows - Set Set Different Row Background Color
                            {
                                labelBackgroundColor = Color.LightGray;
                            }
                            else
                            {
                                labelBackgroundColor = Color.LightSkyBlue;
                            }
                        }
                        else
                        {
                            labelBackgroundColor = Color.LightGreen;
                        }
                        highlightedProfitableExchanges = highlightedProfitableExchanges + 1;

                        Label BaseCurrencyCodeLabel = new Label {
                            Text = BaseCurrencyDescription, TextColor = Color.DarkBlue, BackgroundColor = labelBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
                        };
                        Label IntermediateCurrencyCodeLabel = new Label {
                            Text = IntermediateCurrencyDescription, TextColor = Color.DarkBlue, BackgroundColor = labelBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
                        };
                        Label TargetCurrencyCodeLabel = new Label {
                            Text = TargetCurrencyDescription, TextColor = Color.DarkBlue, BackgroundColor = labelBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
                        };
                        Label ImpliedValueLabel = new Label {
                            Text = ImpliedValue.ToString("N12"), TextColor = Color.DarkBlue, BackgroundColor = labelBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
                        };
                        Label ActualValueLabel = new Label {
                            Text = ActualValue.ToString("N12"), TextColor = Color.DarkBlue, BackgroundColor = labelBackgroundColor, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
                        };

                        gridDetails.RowDefinitions.Add(new RowDefinition {
                            Height = GridLength.Auto
                        });
                        //gridDetails.Children.Add(BaseCurrencyCodeLabel, 0, rowDetailNumber);
                        //gridDetails.Children.Add(IntermediateCurrencyCodeLabel, 1, rowDetailNumber);
                        //gridDetails.Children.Add(TargetCurrencyCodeLabel, 2, rowDetailNumber);
                        //gridDetails.Children.Add(ImpliedValueLabel, 3, rowDetailNumber);
                        //gridDetails.Children.Add(ActualValueLabel, 4, rowDetailNumber);

                        gridDetails.Children.Add(IntermediateCurrencyCodeLabel, 0, rowDetailNumber);
                        gridDetails.Children.Add(ImpliedValueLabel, 1, rowDetailNumber);
                        gridDetails.Children.Add(ActualValueLabel, 2, rowDetailNumber);

                        rowDetailNumber++;
                    }

                    //Scroll View
                    scrollView_Grid_CurrencyArbitrage = new ScrollView()
                    {
                        Orientation = ScrollOrientation.Both,
                        HorizontalScrollBarVisibility = ScrollBarVisibility.Always,
                        VerticalScrollBarVisibility   = ScrollBarVisibility.Always,
                        IsVisible = true,
                        IsEnabled = true,
                        Content   = gridDetails
                    };
                    stackLayout.Children.Add(scrollView_Grid_CurrencyArbitrage);
                }
                else
                {
                    //No Potential Currency Arbitrage Found From Latest ExchangeRates
                    label_DisplayMessage = new Label()
                    {
                        LineBreakMode           = LineBreakMode.WordWrap,
                        Text                    = "No Potential Currency Arbitrage Found",
                        IsEnabled               = false,
                        IsVisible               = true,
                        FontAttributes          = FontAttributes.Bold,
                        FontSize                = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment   = TextAlignment.Center,
                        TextColor               = Color.Red
                    };
                    stackLayout.Children.Add(label_DisplayMessage);

                    label_AdditionalDisplayMessage = new Label()
                    {
                        LineBreakMode           = LineBreakMode.WordWrap,
                        Text                    = "Base Currency: " + baseCurrencyDescription + Environment.NewLine + "Target Currency: " + targetCurrencyDescription,
                        IsEnabled               = false,
                        IsVisible               = true,
                        FontAttributes          = FontAttributes.Bold,
                        FontSize                = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment   = TextAlignment.Center,
                        TextColor               = Color.DarkBlue
                    };
                    stackLayout.Children.Add(label_AdditionalDisplayMessage);

                    boxView_BlackLine_DisplayMessage = new BoxView()
                    {
                        Color         = Color.Blue,
                        HeightRequest = 3,
                        IsEnabled     = false,
                        IsVisible     = true,
                    };
                    stackLayout.Children.Add(boxView_BlackLine_DisplayMessage);
                }
            }
            else if (ignoreMissingInputs == false)
            {
                //Invalid Inputs For Calculating Currency Arbitrage
                label_DisplayMessage = new Label()
                {
                    LineBreakMode           = LineBreakMode.WordWrap,
                    Text                    = "Invalid Inputs For Calculating Currency Arbitrage",
                    IsEnabled               = false,
                    IsVisible               = true,
                    FontAttributes          = FontAttributes.Bold,
                    FontSize                = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                    HorizontalTextAlignment = TextAlignment.Center,
                    VerticalTextAlignment   = TextAlignment.Center,
                    TextColor               = Color.Red
                };
                stackLayout.Children.Add(label_DisplayMessage);

                boxView_BlackLine_DisplayMessage = new BoxView()
                {
                    Color         = Color.Blue,
                    HeightRequest = 3,
                    IsEnabled     = false,
                    IsVisible     = true,
                };
                stackLayout.Children.Add(boxView_BlackLine_DisplayMessage);
            }
        }
        private void InitialCurrencyExchangeDatasetLoad()
        {
            try
            {
                //Initialize HistoricCurrencyExchangeRatesList
                this.HistoricCurrencyExchangeRatesList = new List <CurrencyExchangeRates>();
                this.ArchiveCurrencyExchangeRatesList  = new List <CurrencyExchangeRatesArchive>();

                try
                {
                    //Get Currency Exchange Rates From Hosted Database
                    this.HistoricCurrencyExchangeRatesList = CurrencyExchangeRateCalculatorLibrary.GetCurrencyExchangeRatesFromDatabase_HostedDB();

                    this.ArchiveCurrencyExchangeRatesList = CurrencyExchangeRateCalculatorLibrary.GetCurrencyExchangeRatesArchiveFromDatabase_HostedDB();
                    if (this.ArchiveCurrencyExchangeRatesList != null && this.ArchiveCurrencyExchangeRatesList.Count() > 0)
                    {
                        foreach (var eachArchiveCurrencyExchangeRates in this.ArchiveCurrencyExchangeRatesList)
                        {
                            if (this.HistoricCurrencyExchangeRatesList.Where(type => type.Date == eachArchiveCurrencyExchangeRates.Date &&
                                                                             type.BaseCurrencyCode == eachArchiveCurrencyExchangeRates.BaseCurrencyCode &&
                                                                             type.TargetCurrencyCode == eachArchiveCurrencyExchangeRates.TargetCurrencyCode)
                                .Any() == false)
                            {
                                this.HistoricCurrencyExchangeRatesList.Add(new CurrencyExchangeRates()
                                {
                                    Date               = eachArchiveCurrencyExchangeRates.Date,
                                    BaseCurrencyCode   = eachArchiveCurrencyExchangeRates.BaseCurrencyCode,
                                    TargetCurrencyCode = eachArchiveCurrencyExchangeRates.TargetCurrencyCode,
                                    ExchangeRate       = eachArchiveCurrencyExchangeRates.ExchangeRate
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //string ExceptionMessage = string.Empty;
                    //if (string.IsNullOrWhiteSpace(ex.Message) == false)
                    //{
                    //    ExceptionMessage = Environment.NewLine + "Exception Message: " + ex.Message;
                    //}
                    //DisplayAlert("Exception! Currency Exchange Dataset Load", "Error During Currency Exchange Dataset Load." + ExceptionMessage, "OK");
                }

                if (this.HistoricCurrencyExchangeRatesList == null || this.HistoricCurrencyExchangeRatesList.Count == 0)
                {
                    if (DatabaseServices.CheckIfTableRefreshLogExists(paramTableName: Constants.Constants.CurrencyExchangeRatesTable) == false)
                    {
                        //Get Historic Currency Exchange Rates Using WebAPI Call
                        this.HistoricCurrencyExchangeRatesList = CurrencyExchangeRateCalculatorLibrary.GetHistoricCurrencyExchangeRatesFromWebAPI();

                        //Save Historic Exchange Rates To Database
                        Task.Run(() => DatabaseServices.SaveCurrencyExchangeRatesToDatabase(this.HistoricCurrencyExchangeRatesList));
                    }
                    else
                    {
                        //Get Historic Currency Exchange Rates From SQLite Database
                        this.HistoricCurrencyExchangeRatesList = DatabaseServices.GetHistoricCurrencyExchangeRatesFromDatabase();
                    }
                }

                //Initialize LatestCurrencyExchangeRatesList
                this.LatestCurrencyExchangeRatesList = new List <CurrencyExchangeRates>();

                //Get Latest Currency Exchange Rates From HistoricCurrencyExchangeRatesList
                PopulateLatestFromHistoricExchangeRates();
            }
            catch (Exception ex)
            {
                string ExceptionMessage = string.Empty;
                if (string.IsNullOrWhiteSpace(ex.Message) == false)
                {
                    ExceptionMessage = Environment.NewLine + "Exception Message: " + ex.Message;
                }
                DisplayAlert("Exception! InitialCurrencyExchangeDatasetLoad", "Error During Currency Exchange Dataset Load." + ExceptionMessage, "OK");
            }
        }