public async Task <BalanceInformation> GetBalance()
        {
            List <WalletBalance> balances;

            try
            {
                var accountInfo = await _binanceWebsocketService.GetAccountInfoAsync();

                if (accountInfo == null)
                {
                    throw new Exception("Account info not had get from binance");
                }

                balances = BinanceConverter.BinanceToWalletBalances(accountInfo.Balances);
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting balances from binance: " + e.Message);
                throw;
            }

            var totalBtcBalance = 0m;

            foreach (var balance in balances)
            {
                decimal price;
                decimal btcAmount;
                decimal averagePrice = 0m;

                if (balance.Currency == _generalConfig.TradingCurrency)
                {
                    btcAmount = balance.Balance;
                    price     = 0m;
                }
                else if (balance.Currency == "USDT")
                {
                    var marketPrice = await GetPrice("USDT", _generalConfig.TradingCurrency);

                    btcAmount = balance.Balance / marketPrice;
                    price     = 0m;
                }
                else
                {
                    var marketPrice = await GetPrice(_generalConfig.TradingCurrency, balance.Currency);

                    price        = marketPrice;
                    btcAmount    = (price * balance.Balance);
                    averagePrice =
                        await _databaseService.GetBuyAveragePrice(_generalConfig.TradingCurrency, balance.Currency, Constants.Binance, balance.Balance);
                }

                balance.PercentageChange = ProfitCalculator.PriceDifference(price, averagePrice);
                balance.BtcAmount        = btcAmount;
                balance.Price            = price;
                totalBtcBalance          = totalBtcBalance + balance.BtcAmount;
            }

            var lastBalance = await _databaseService.GetBalance24HoursAgo(Constants.Binance);

            var dollarAmount = await GetDollarAmount(_generalConfig.TradingCurrency, totalBtcBalance);

            var currentBalance = await _databaseService.AddBalance(totalBtcBalance, dollarAmount, Constants.Binance);

            await _databaseService.AddWalletBalances(balances);

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Binance, balances));
        }
Beispiel #2
0
        public async Task <BalanceInformation> GetBalance()
        {
            List <WalletBalance> balances;

            try
            {
                var binanceClient = GetApi();
                using (var user = new BinanceApiUser(_config.Key, _config.Secret))
                {
                    var accountInfo = await binanceClient.GetAccountInfoAsync(user);

                    balances = BinanceConverter.BinanceToWalletBalances(accountInfo.Balances);
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting balances from binance: " + e.Message);
                throw;
            }

            var totalBtcBalance = 0m;

            foreach (var balance in balances)
            {
                decimal price;
                decimal btcAmount;
                decimal averagePrice = 0m;

                if (balance.Currency == _generalConfig.TradingCurrency)
                {
                    btcAmount = balance.Balance;
                    price     = 0m;
                }
                else if (balance.Currency == "USDT")
                {
                    var marketPrice = await GetPrice("USDT", _generalConfig.TradingCurrency);

                    btcAmount = balance.Balance / marketPrice;
                    price     = 0m;
                }
                else
                {
                    var marketPrice = await GetPrice(_generalConfig.TradingCurrency, balance.Currency);

                    price        = marketPrice;
                    btcAmount    = (price * balance.Balance);
                    averagePrice =
                        await _databaseService.GetBuyAveragePrice(_generalConfig.TradingCurrency, balance.Currency, Constants.Binance, balance.Balance);
                }
                try
                {
                    balance.PercentageChange = ProfitCalculator.PriceDifference(price, averagePrice);
                }
                catch
                {
                    // There maybe a divide by 0 issue if we couldn't find the last trade. Its fine. Just print zero
                    balance.PercentageChange = 0;
                }
                balance.BtcAmount = btcAmount;
                balance.Price     = price;
                totalBtcBalance   = totalBtcBalance + balance.BtcAmount;
            }

            var lastBalance = await _databaseService.GetBalance24HoursAgo(Constants.Binance);

            var dollarAmount = await GetDollarAmount(_generalConfig.TradingCurrency, totalBtcBalance);

            var currentBalance = await _databaseService.AddBalance(totalBtcBalance, dollarAmount, Constants.Binance);

            await _databaseService.AddWalletBalances(balances);

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Binance, balances));
        }