/// ######################################################################################### /// Update a card's charts private async Task UpdateCard(int i) { try { string crypto = App.pinnedCoins[i]; vm.PriceCards[i].Info.CurrencySym = App.currencySymbol; /// Save the current timeSpan for navigating to another page vm.PriceCards[i].Chart.TimeSpan = timeSpan; /// Colors var brush = vm.PriceCards[i].Chart.ChartStroke; brush = ColorConstants.GetCoinBrush(crypto); vm.PriceCards[i].Chart.ChartStroke = brush; /// Get Historic and create List of ChartData for the chart var histo = await Ioc.Default.GetService <ICryptoCompare>().GetHistoric_(crypto, timeUnit, limit, aggregate); var chartData = new List <ChartPoint>(); foreach (var h in histo) { chartData.Add(new ChartPoint() { Date = h.DateTime, Value = h.Average, Volume = h.volumeto, High = h.high, Low = h.low, Open = h.open, Close = h.close }); } vm.PriceCards[i].Chart.ChartData = chartData; var temp = GraphHelper.AdjustLinearAxis(new ChartStyling(), timeSpan); vm.PriceCards[i].Chart.LabelFormat = temp.LabelFormat; vm.PriceCards[i].Chart.Minimum = temp.Minimum; vm.PriceCards[i].Chart.MajorStepUnit = temp.MajorStepUnit; vm.PriceCards[i].Chart.MajorStep = temp.MajorStep; vm.PriceCards[i].Chart.TickInterval = temp.TickInterval; /// Calculate min-max to adjust axis var MinMax = GraphHelper.GetMinMaxOfArray(chartData.Select(d => d.Value).ToList()); vm.PriceCards[i].Chart.PricesMinMax = GraphHelper.OffsetMinMaxForChart(MinMax.Min, MinMax.Max); vm.PriceCards[i].Chart.VolumeMax = GraphHelper.GetMaxOfVolume(chartData); /// Calculate the price difference double oldestPrice = histo[0].Average; double newestPrice = histo[histo.Count - 1].Average; vm.PriceCards[i].Info.Prices = (oldestPrice, newestPrice); /// Sum total volume from historic vm.PriceCards[i].Info.VolumeToTotal = histo.Sum(h => h.volumeto); vm.PriceCards[i].Info.VolumeFromTotal = histo.Sum(h => h.volumefrom); double total = histo.Sum(h => h.volumeto); /// Show that loading is done vm.PriceCards[i].Info.IsLoading = false; } catch (Exception) { } }
private async Task UpdateCoin() { var crypto = vm.Coin.Name; vm.CurrencySymbol = Currencies.GetCurrencySymbol(App.currency); /// Colors vm.Chart.ChartStroke = ColorConstants.GetCoinBrush(crypto); /// Get Historic and create List of ChartData for the chart (plus LinearAxis) var histo = await Ioc.Default.GetService <ICryptoCompare>().GetHistoric_(crypto, timeUnit, limit, aggregate); var chartData = new List <ChartPoint>(); foreach (var h in histo) { chartData.Add(new ChartPoint() { Date = h.DateTime, Value = h.Average, Volume = h.volumeto, High = h.high, Low = h.low, Open = h.open, Close = h.close }); } vm.Chart.ChartData = chartData; var temp = GraphHelper.AdjustLinearAxis(new ChartStyling(), timeSpan); vm.Chart.LabelFormat = temp.LabelFormat; vm.Chart.Minimum = temp.Minimum; vm.Chart.MajorStepUnit = temp.MajorStepUnit; vm.Chart.MajorStep = temp.MajorStep; vm.Chart.TickInterval = temp.TickInterval; vm.Coin.VolumeToTotal = histo.Sum(x => x.volumeto); /// Calculate min-max to adjust axis var MinMax = GraphHelper.GetMinMaxOfArray(chartData.Select(d => d.Value).ToList()); vm.Chart.PricesMinMax = GraphHelper.OffsetMinMaxForChart(MinMax.Min, MinMax.Max); vm.Chart.VolumeMax = GraphHelper.GetMaxOfVolume(chartData); /// Calculate the price difference double oldestPrice = histo.FirstOrDefault()?.Average ?? 0; double newestPrice = histo.LastOrDefault()?.Average ?? 0; vm.Coin.Prices = (oldestPrice, newestPrice); vm.Coin.IsLoading = false; }
/// ############################################################################################### /// Update the chart of the historic values of the portfolio private async Task UpdatePortfolioChart() { var nPurchases = vm.Portfolio.Count; if (nPurchases == 0) { return; } /// Optimize by only getting historic for different cryptos var uniqueCryptos = new HashSet <string>(vm.Portfolio.Select(x => x.Crypto)).ToList(); /// Get historic for each unique crypto, get invested qty, and multiply /// to get the worth of each crypto to the user's wallet var cryptoQties = new List <double>(); var cryptoWorth = new List <List <double> >(); var histos = new List <List <HistoricPrice> >(nPurchases); foreach (var crypto in uniqueCryptos) { var histo = await Ioc.Default.GetService <ICryptoCompare>().GetHistoric_(crypto, timeUnit, limit, aggregate); var cryptoQty = vm.Portfolio.Where(x => x.Crypto == crypto).Sum(x => x.CryptoQty); cryptoWorth.Add(histo.Select(x => x.Average * cryptoQty).ToList()); histos.Add(histo); } /// There might be young cryptos that didnt exist in the past, so take the common minimum var minCommon = histos.Min(x => x.Count); if (minCommon == 1) { return; } /// Check if all arrays are equal length, if not, remove the leading values var sameLength = cryptoWorth.All(x => x.Count == cryptoWorth[0].Count); if (!sameLength) { for (int i = 0; i < histos.Count; i++) { histos[i] = histos[i].Skip(Math.Max(0, histos[i].Count() - minCommon)).ToList(); } } var worth_arr = new List <double>(); var dates_arr = histos[0].Select(kk => kk.DateTime).ToList(); for (int i = 0; i < minCommon; i++) { worth_arr.Add(cryptoWorth.Select(x => x[i]).Sum()); } /// Create List for chart var chartData = new List <ChartPoint>(); for (int i = 0; i < minCommon; i++) { chartData.Add(new ChartPoint { Date = dates_arr.ElementAt(i), Value = (float)worth_arr[i], Volume = 0 }); } vm.Chart.ChartData = chartData; var temp = GraphHelper.AdjustLinearAxis(new ChartStyling(), timeSpan); vm.Chart.LabelFormat = temp.LabelFormat; vm.Chart.Minimum = temp.Minimum; vm.Chart.MajorStepUnit = temp.MajorStepUnit; vm.Chart.MajorStep = temp.MajorStep; vm.Chart.TickInterval = temp.TickInterval; vm.Chart.ChartStroke = (vm.TotalDelta >= 0) ? ColorConstants.GetColorBrush("pastel_green") : ColorConstants.GetColorBrush("pastel_red"); /// Calculate min-max to adjust axis var MinMax = GraphHelper.GetMinMaxOfArray(chartData.Select(d => d.Value).ToList()); vm.Chart.PricesMinMax = GraphHelper.OffsetMinMaxForChart(MinMax.Min, MinMax.Max); vm.Chart.VolumeMax = GraphHelper.GetMaxOfVolume(chartData); vm.Chart.VolumeMax = (vm.Chart.VolumeMax == 0) ? 10 : vm.Chart.VolumeMax; vm.Chart.IsLoading = false; }