示例#1
0
        private void UpdateCoins(string cryptoCompareResponse, string cryptoCompareCoinsResponse, string coinMarketCapResponse, List <CoinConfig> coinConfigs)
        {
            List <CoinConfig> removeConfigs        = new List <CoinConfig>();
            decimal           totalPaid            = 0;
            decimal           totalOverall         = 0;
            decimal           totalNegativeProfits = 0;
            decimal           totalPostivieProfits = 0;
            int lineIndex = 0;

            if (string.IsNullOrWhiteSpace(cryptoCompareResponse) || string.IsNullOrWhiteSpace(cryptoCompareCoinsResponse) || string.IsNullOrWhiteSpace(coinMarketCapResponse))
            {
                MessageBox.Show("The API webservice is having issues at the moment. Please try again in a few minutes.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            _coinNames = MappingService.CryptoCompareCoinList(cryptoCompareCoinsResponse);
            _coins     = MappingService.MapCombination(cryptoCompareResponse, coinMarketCapResponse, coinConfigs);

            MainService.CheckAlerts(_coins);

            if (_loadLines)
            {
                _cleanReset = true;
                RemoveLines();
            }

            foreach (CoinConfig coinConfig in coinConfigs)
            {
                if (!_coins.Any(c => c.ShortName == coinConfig.Name))
                {
                    Task.Factory.StartNew(() => { MessageBox.Show($"Sorry, Crypto Compare and Coin Market Cap does not have any data for {coinConfig.Name}."); });
                    removeConfigs.Add(coinConfig);
                    continue;
                }
                Coin coin = _coins.Find(c => c.ShortName == coinConfig.Name);

                if ((_cleanReset && _loadLines) || (!_coinLines.Any(c => c.CoinName.ExtEquals(coin.ShortName) && c.CoinIndex == coinConfig.Index)))
                {
                    if (_resetStartupPrice)
                    {
                        coinConfig.StartupPrice = 0;
                    }

                    AddLine(coinConfig, coin, lineIndex);
                }

                lineIndex++;

                CoinLine line = (from c in _coinLines where c.CoinName.ExtEquals(coin.ShortName) && c.CoinIndex == coinConfig.Index select c).First();

                decimal bought = line.BoughtTextBox.Text.ConvertToDecimal();
                decimal paid   = line.PaidTextBox.Text.ConvertToDecimal();
                decimal total  = bought * coin.Price;
                decimal profit = total - paid;

                coinConfig.Bought = bought;
                coinConfig.Paid   = paid;

                totalPaid    += paid;
                totalOverall += paid + profit;

                if (profit >= 0)
                {
                    totalPostivieProfits += profit;
                }
                else
                {
                    totalNegativeProfits += profit;
                }

                var coinIndexLabel     = coinConfigs.Count(c => c.Name.ExtEquals(coinConfig.Name)) > 1 ? $"[{coinConfig.Index + 1}]" : string.Empty;
                var coinLabel          = coin.ShortName;
                var priceLabel         = $"{MainService.CurrencySymbol}{coin.Price.ConvertToString(8)}";
                var boughtLabel        = $"{MainService.CurrencySymbol}{bought.SafeDivision(paid).ConvertToString(8)}";
                var totalLabel         = $"{MainService.CurrencySymbol}{total:0.00}";
                var profitLabel        = $"{MainService.CurrencySymbol}{profit:0.00}";
                var ratioLabel         = paid != 0 ? $"{profit / paid:0.00}" : "0.00";
                var changeDollarLabel  = $"{MainService.CurrencySymbol}{(coin.Price - coinConfig.StartupPrice):0.000000}";
                var changePercentLabel = $"{coinConfig.StartupPrice.SafeDivision(coin.Price - coinConfig.StartupPrice) * 100:0.00}%";
                var change1HrLabel     = $"{coin.Change1HourPercent:0.00}%";
                var change24HrLabel    = $"{coin.Change24HourPercent:0.00}%";
                var change7DayLabel    = $"{coin.Change7DayPercent:0.00}%";

                Invoke((MethodInvoker) delegate
                {
                    line.CoinIndexLabel.Text         = coinIndexLabel;
                    line.CoinLabel.Text              = coinLabel;
                    line.PriceLabel.Text             = priceLabel;
                    line.BoughtPriceLabel.Text       = boughtLabel;
                    line.TotalLabel.Text             = totalLabel;
                    line.ProfitLabel.Text            = profitLabel;
                    line.RatioLabel.Text             = ratioLabel;
                    line.ChangeDollarLabel.Text      = changeDollarLabel;
                    line.ChangePercentLabel.Text     = changePercentLabel;
                    line.Change1HrPercentLabel.Text  = change1HrLabel;
                    line.Change24HrPercentLabel.Text = change24HrLabel;
                    line.Change7DayPercentLabel.Text = change7DayLabel;
                });
            }

            //Remove unsupported coins
            foreach (var coinConfig in removeConfigs)
            {
                _coinConfigs.Remove(coinConfig);
            }

            if (_cleanReset)
            {
                _loadLines         = false;
                _resetStartupPrice = false;
            }

            _refreshTime = DateTime.Now;
            UpdateStatus("Sleeping");
            SetHeight(coinConfigs.Count);

            var totalProfitColor         = totalOverall - totalPaid >= 0 ? ColorTranslator.FromHtml(UserConfigService.Theme.PositiveColor) : ColorTranslator.FromHtml(UserConfigService.Theme.NegativeColor);
            var totalProfitLabel         = $"{MainService.CurrencySymbol}{totalOverall - totalPaid:0.00}";
            var totalNegativeProfitLabel = $"{MainService.CurrencySymbol}{totalNegativeProfits:0.00}";
            var totalPositiveProfitLabel = $"{MainService.CurrencySymbol}{totalPostivieProfits:0.00}";
            var totalOverallLabel        = $"{MainService.CurrencySymbol}{totalOverall:0.00}";
            var totalInvested            = $"{MainService.CurrencySymbol}{totalPaid:0.00}";
            var profitPercentage         = $"{Math.Abs(((1 - totalPaid.SafeDivision(totalOverall))) * 100):0.00}%";

            Invoke((MethodInvoker) delegate
            {
                lblTotalProfit.ForeColor        = totalProfitColor;
                lblProfitPercentage.ForeColor   = totalProfitColor;
                lblTotalProfit.Text             = totalProfitLabel;
                lblTotalNegativeProfit.Text     = totalNegativeProfitLabel;
                lblTotalPositiveProfit.Text     = totalPositiveProfitLabel;
                lblOverallTotal.Text            = totalOverallLabel;
                lblTotalInvested.Text           = totalInvested;
                lblProfitPercentage.Text        = profitPercentage;
                alertsToolStripMenuItem.Enabled = true;
                coinsToolStripMenuItem.Enabled  = true;
            });
        }