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

            try
            {
                var balances = await _poloniexClient.Wallet.GetBalancesAsync();

                poloniexToWalletBalances = TradeConverter.PoloniexToWalletBalances(balances);
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting balances from poloniex: " + e.Message);
                throw;
            }

            var totalBtcBalance = 0m;

            foreach (var balance in poloniexToWalletBalances)
            {
                if (balance.BtcAmount == 0)
                {
                    continue;
                }

                var price = await GetPrice(balance.Currency);

                var boughtPrice = 0m;

                var lastTradeForPair1 = _databaseService.GetLastTradeForPair(balance.Currency, Constants.Poloniex, TradeSide.Buy);
                if (lastTradeForPair1 != null)
                {
                    boughtPrice = lastTradeForPair1.Limit;
                }

                try
                {
                    balance.PercentageChange = ProfitCalculator.PriceDifference(price, boughtPrice);
                }
                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.Price   = price;
                totalBtcBalance = totalBtcBalance + balance.BtcAmount;
            }

            var lastBalance  = _databaseService.GetBalance24HoursAgo(Constants.Poloniex);
            var dollarAmount = await _priceService.GetDollarAmount(totalBtcBalance);

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

            await _databaseService.AddWalletBalances(poloniexToWalletBalances);

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Poloniex, poloniexToWalletBalances));
        }
Example #2
0
        public async Task <BalanceInformation> GetBalance()
        {
            List <WalletBalance> bittrexBalances;

            try
            {
                var response = await _exchange.GetBalances();

                bittrexBalances = TradeConverter.BittrexToWalletBalances(response);
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting balances from bittrex: " + e.Message);
                throw;
            }

            var totalBtcBalance = 0m;

            foreach (var balance in bittrexBalances)
            {
                if (balance.Balance == 0)
                {
                    continue;
                }

                var marketPrice = await GetPrice(balance.Currency);

                decimal price;
                decimal btcAmount;
                decimal boughtPrice = 0m;

                switch (balance.Currency)
                {
                case "BTC":
                    btcAmount   = balance.Balance;
                    price       = 1;
                    boughtPrice = 1;
                    break;

                case "USDT":
                    price     = marketPrice;
                    btcAmount = (balance.Balance / price);
                    var lastTradeForPair =
                        _databaseService.GetLastTradeForPair(balance.Currency, Constants.Bittrex, TradeSide.Buy);
                    if (lastTradeForPair != null)
                    {
                        boughtPrice = lastTradeForPair.Limit;
                    }
                    break;

                default:
                    price     = marketPrice;
                    btcAmount = (price * balance.Balance);
                    var lastTradeForPair1 =
                        _databaseService.GetLastTradeForPair(balance.Currency, Constants.Bittrex, TradeSide.Buy);
                    if (lastTradeForPair1 != null)
                    {
                        boughtPrice = lastTradeForPair1.Limit;
                    }
                    break;
                }
                try
                {
                    balance.PercentageChange = ProfitCalculator.PriceDifference(price, boughtPrice);
                }
                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  = _databaseService.GetBalance24HoursAgo(Constants.Bittrex);
            var dollarAmount = await _priceService.GetDollarAmount(totalBtcBalance);

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

            await _databaseService.AddWalletBalances(bittrexBalances);

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Bittrex, bittrexBalances));
        }