Exemplo n.º 1
0
        public async Task <List <Withdrawal> > GetNewWithdrawals()
        {
            // no api call
            var list = new List <Withdrawal>();

            try
            {
                var binanceClient = GetApi();
                var lastChecked   = _databaseService.GetLastChecked("Binance.DepositCheck");
                using (var user = new BinanceApiUser(_config.Key, _config.Secret))
                {
                    var start            = Helpers.Helpers.DateTimeToUnixTimestamp(lastChecked);
                    var binanceDesposits = await binanceClient.GetWithdrawalsAsync(user, _generalConfig.TradingCurrency, null, start, 0, 10000000);

                    list = BinanceConverter.BinanceToWithdrawals(binanceDesposits);
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting withdrawals from binance: " + e.Message);
            }

            var newWithdrawals = await _databaseService.AddWithdrawals(list, Constants.Binance);

            await _databaseService.AddLastChecked("Binance.WithdrawalCheck", DateTime.Now);

            return(newWithdrawals);
        }
Exemplo n.º 2
0
        public async Task <List <Trade> > GetOrderHistory(DateTime lastChecked)
        {
            var list = new List <Trade>();

            try
            {
                var symbols = await _binanceWebsocketService.GetSymbolStringsAsync();

                foreach (var symbol in symbols)
                {
                    var response = await _binanceWebsocketService.GetAccountTradesAsync(symbol);

                    var ccy2 = symbol.Remove(symbol.Length - _generalConfig.TradingCurrency.Length);

                    var symlist = BinanceConverter.BinanceToTrades(response, _generalConfig.TradingCurrency, ccy2, _log);
                    list.AddRange(symlist);
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting trades from binance: " + e.Message);
            }

            return(list);
        }
Exemplo n.º 3
0
        public async Task <List <OpenOrder> > GetNewOpenOrders(DateTime lastChecked)
        {
            var openOrders = new List <OpenOrder>();

            try
            {
                var symbols = await _binanceWebsocketService.GetSymbolStringsAsync();

                foreach (var symbol in symbols)
                {
                    var response = await _binanceWebsocketService.GetOpenOrdersAsync(symbol);

                    var ccy2 = symbol.Remove(symbol.Length - _generalConfig.TradingCurrency.Length);
                    var list = BinanceConverter.BinanceToOpenOrders(response, _generalConfig.TradingCurrency, ccy2);

                    openOrders.AddRange(list);
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting openOrders from binance: " + e.Message);
            }

            var newOrders = await _databaseService.AddOpenOrders(openOrders);

            return(newOrders);
        }
Exemplo n.º 4
0
        public async Task <List <Trade> > GetOrderHistory(DateTime lastChecked)
        {
            var list = new List <Trade>();

            try
            {
                var binanceClient = GetApi();

                using (var user = new BinanceApiUser(_config.Key, _config.Secret))
                {
                    foreach (var symbol in _symbols)
                    {
                        var response = await binanceClient.GetAccountTradesAsync(user, symbol);

                        var ccy2 = symbol.Remove(symbol.Length - _generalConfig.TradingCurrency.Length);

                        var symlist = BinanceConverter.BinanceToTrades(response, _generalConfig.TradingCurrency, ccy2, _log);
                        list.AddRange(symlist);
                    }
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting trades from binance: " + e.Message);
            }

            return(list);
        }
Exemplo n.º 5
0
        public async Task <List <Withdrawal> > GetNewWithdrawals()
        {
            // no api call
            var list = new List <Withdrawal>();

            try
            {
                var binanceClient = GetApi();

                using (var user = new BinanceApiUser(_config.Key, _config.Secret))
                {
                    var binanceDesposits = await binanceClient.GetWithdrawalsAsync(user, "BTC");

                    list = BinanceConverter.BinanceToWithdrawals(binanceDesposits);
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting withdrawals from binance: " + e.Message);
            }

            var newWithdrawals = await _databaseService.AddWithdrawals(list, Constants.Binance);

            await _databaseService.AddLastChecked("Binance.WithdrawalCheck", DateTime.Now);

            return(newWithdrawals);
        }
Exemplo n.º 6
0
        public async Task <List <OpenOrder> > GetNewOpenOrders(DateTime lastChecked)
        {
            var openOrders = new List <OpenOrder>();

            try
            {
                var binanceClient = GetApi();
                using (var user = new BinanceApiUser(_config.Key, _config.Secret))
                {
                    foreach (var symbol in _symbols)
                    {
                        var response = await binanceClient.GetOpenOrdersAsync(user, symbol);

                        var ccy2 = symbol.Remove(symbol.Length - _generalConfig.TradingCurrency.Length);
                        var list = BinanceConverter.BinanceToOpenOrders(response, _generalConfig.TradingCurrency, ccy2);

                        openOrders.AddRange(list);
                    }
                }
            }
            catch (Exception e)
            {
                _log.LogError("Error in getting openOrders from binance: " + e.Message);
            }

            var newOrders = await _databaseService.AddOpenOrders(openOrders);

            return(newOrders);
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 8
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));
        }