Exemplo n.º 1
0
        public async Task GetBalances(Prices prices)
        {
            var myPrices = new List <Tuple <string, decimal, decimal> >();

            // Get the Binance balances
            var result = await _binanceApi.GetAccountInfo();

            var accountBalances = result.Balances.Where(x => x.Total > 0).ToList();

            if (accountBalances.Count > 0)
            {
                var priceResult = await _binanceApi.GetAllPricesAsync();

                foreach (var value in accountBalances)
                {
                    try
                    {
                        if (value.Asset == "BTC")
                        {
                            myPrices.Add(new Tuple <string, decimal, decimal>(value.Asset,
                                                                              value.Total * prices.Eur.Last,
                                                                              value.Total * prices.Usd.Last));
                        }
                        else
                        {
                            var currentPrice = priceResult.FirstOrDefault(y => y.Symbol == value.Asset + "BTC");

                            if (currentPrice == null)
                            {
                                myPrices.Add(new Tuple <string, decimal, decimal>(value.Asset, 0, 0));
                            }
                            else
                            {
                                myPrices.Add(new Tuple <string, decimal, decimal>(value.Asset,
                                                                                  value.Total * currentPrice.Price * prices.Eur.Last,
                                                                                  value.Total * currentPrice.Price * prices.Usd.Last));
                            }
                        }
                    }
                    catch (Exception)
                    {
                        myPrices.Add(new Tuple <string, decimal, decimal>(value.Asset, 0, 0));
                    }
                }

                Console.WriteLine(accountBalances.ToStringTable <BinanceBalance>(
                                      new[] { "Currency", "Available", "Balance", "Estimated EUR Value", "Estimated USD Value" },
                                      x => x.Asset, x => x.Free.ToString("0.00000000"),
                                      x => x.Total.ToString("0.00000000"), x => myPrices.FirstOrDefault(y => y.Item1 == x.Asset)?.Item2.ToString("0.00 EUR"),
                                      x => (myPrices.FirstOrDefault(y => y.Item1 == x.Asset)?.Item3.ToString("0.00 USD"))));


                Console.WriteLine($"\tTOTAL BALANCE: {(myPrices.Sum(x => x.Item2)):0.00} EUR / " + $"{(myPrices.Sum(x => x.Item3)):0.00} USD");
            }
            else
            {
                Console.WriteLine("No balances found...");
            }
        }