private void AddCoin(CoinConfig coin, CoinData downloadedCoin, int index)
        {
            //Store the intial coin price at startup
            if(coin.SetStartupPrice)
                coin.StartupPrice = downloadedCoin.Price;

            //Create the gui line
            CoinGuiLine newLine = new CoinGuiLine(downloadedCoin.ShortName, index);

            //Set the bought and paid amounts
            newLine.BoughtTextBox.Text = coin.bought.ToString();
            newLine.PaidTextBox.Text = coin.paid.ToString();

            //Add the line elements to gui
            MethodInvoker invoke = delegate
            {
                Height += 25;
                Controls.Add(newLine.CoinLabel);
                Controls.Add(newLine.PriceLabel);
                Controls.Add(newLine.BoughtTextBox);
                Controls.Add(newLine.TotalLabel);
                Controls.Add(newLine.PaidTextBox);
                Controls.Add(newLine.ProfitLabel);
                Controls.Add(newLine.ChangeDollarLabel);
                Controls.Add(newLine.ChangePercentLabel);
                Controls.Add(newLine.Change24HrPercentLabel);
            };
            Invoke(invoke);

            //Add line to list
            _coinGuiLines.Add(newLine);
        }
Exemple #2
0
        public void UpdateCoins(string response)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateCoinDelegates(UpdateCoins), response);
                return;
            }

            //Overall values
            decimal totalPaid            = 0;
            decimal overallTotal         = 0;
            decimal totalNegativeProfits = 0;
            decimal totalPostivieProfits = 0;

            //Index of coin gui line
            int index = 0;

            //Deserialize settings
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };

            //Deserialize response and map to generic coin
            List <CoinData> coins = new List <CoinData>();

            switch (_api)
            {
            case API_COIN_MARKET_CAP:
                var coinsCoinMarketCap = JsonConvert.DeserializeObject <List <ApiCoinMarketCap> >(response, settings);
                coins = coinsCoinMarketCap.Select(c => Mappings.MapCoinMarketCap(c)).ToList();
                break;

            case API_COIN_CAP:
                var coinsCoinCap = JsonConvert.DeserializeObject <List <ApiCoinCap> >(response, settings);
                coins = coinsCoinCap.Select(c => Mappings.MapCoinCap(c)).ToList();
                break;
            }

            //Create list of coin names
            _coinNames = coins.OrderBy(c => c.ShortName).Select(c => c.ShortName).ToList();

            //Loop through all coins from config
            foreach (CoinConfig coin in _coinConfigs)
            {
                CoinData downloadedCoin;

                //Parse coins, if coin doesnt exist set to 0
                downloadedCoin = coins.Any(c => c.ShortName == coin.coin)
                    ? coins.Single(c => c.ShortName == coin.coin)
                    : new CoinData {
                    ShortName = coin.coin, CoinIndex = coin.coinIndex, Change1HourPercent = 0, Change24HourPercent = 0, Price = 0
                };

                //Check if gui lines need to be loaded
                if (_loadGuiLines)
                {
                    AddCoin(coin, downloadedCoin, index);
                }

                //Incremenet coin line index
                index++;

                //Check if coinguiline exists for coinConfig
                if (!_coinGuiLines.Any(cg => cg.CoinName.Equals(downloadedCoin.ShortName)))
                {
                    RemoveDelegate remove = new RemoveDelegate(Remove);
                    BeginInvoke(remove);
                    return;
                }

                //Get the gui line for coin
                CoinGuiLine line = (from c in _coinGuiLines where c.CoinName.Equals(downloadedCoin.ShortName) && c.CoinIndex == coin.coinIndex select c).First();

                //Calculate
                decimal bought        = Convert.ToDecimal(line.BoughtTextBox.Text);
                decimal paid          = Convert.ToDecimal(line.PaidTextBox.Text);
                decimal boughtPrice   = bought == 0 ? 0 : paid / bought;
                decimal total         = bought * downloadedCoin.Price;
                decimal profit        = total - paid;
                decimal changeDollar  = downloadedCoin.Price - coin.StartupPrice;
                decimal changePercent = coin.StartupPrice == 0 ? 0 : ((downloadedCoin.Price - coin.StartupPrice) / coin.StartupPrice) * 100;

                //Update total profits
                if (profit >= 0)
                {
                    totalPostivieProfits += profit;
                }
                else
                {
                    totalNegativeProfits += profit;
                }

                //Add to totals
                totalPaid    += paid;
                overallTotal += paid + profit;

                //Update gui
                line.CoinLabel.Show();
                line.CoinIndexLabel.Text         = _coinConfigs.Count(c => c.coin.Equals(coin.coin)) > 1 ? $"[{coin.coinIndex + 1}]" : string.Empty;
                line.CoinLabel.Text              = downloadedCoin.ShortName;
                line.PriceLabel.Text             = $"${downloadedCoin.Price}";
                line.BoughtPriceLabel.Text       = $"${boughtPrice:0.000000}";
                line.TotalLabel.Text             = $"${total:0.00}";
                line.ProfitLabel.Text            = $"${profit:0.00}";
                line.ChangeDollarLabel.Text      = $"${changeDollar:0.000000}";
                line.ChangePercentLabel.Text     = $"{changePercent:0.00}%";
                line.Change1HrPercentLabel.Text  = $"{downloadedCoin.Change1HourPercent:0.00}%";
                line.Change24HrPercentLabel.Text = $"{downloadedCoin.Change24HourPercent:0.00}%";
            }

            //Update gui
            lblOverallTotal.Text        = $"${overallTotal:0.00}";
            lblTotalProfit.ForeColor    = overallTotal - totalPaid >= 0 ? Color.Green : Color.Red;
            lblTotalProfit.Text         = $"${overallTotal - totalPaid:0.00}";
            lblTotalNegativeProfit.Text = $"${totalNegativeProfits:0.00}";
            lblTotalPositiveProfit.Text = $"${totalPostivieProfits:0.00}";
            lblStatus.Text = "Status: Sleeping";
            _refreshTime   = DateTime.Now;

            //Sleep and rerun
            _loadGuiLines = false;
        }