예제 #1
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;
                }

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

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

                case "USDT":
                    price = await GetPrice(balance.Currency);

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

                default:
                    price = await GetPrice(balance.Currency);

                    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 + btcAmount;
            }

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

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

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Bittrex, bittrexBalances));
        }
예제 #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;
                }

                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.Bittrex, 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.Bittrex);

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

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

            await _databaseService.AddWalletBalances(bittrexBalances);

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