示例#1
0
        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("BTC", 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));
        }
示例#2
0
        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.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.Poloniex, 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.Poloniex);

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

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

            await _databaseService.AddWalletBalances(poloniexToWalletBalances);

            return(new BalanceInformation(currentBalance, lastBalance, Constants.Poloniex, poloniexToWalletBalances));
        }