public void GetBalances_ShouldUnauthorizedThrowException_IfNoApiKeyIsGiven() { var bittrex = new Bittrex(); Func <Task> action = async() => { var _ = await bittrex.GetBalances(); }; action.ShouldThrow <UnauthorizedException>(); }
public BittrexBridge(string apiKey, string apiSecret) { apiKeyStore = apiKey; ActiveOrders = new List <BittrexSharp.Domain.OpenOrder>(); OnHoldOrders = new List <BittrexSharp.Domain.OpenOrder>(); cancelling = new List <string>(); fFailedConection = fConnected = false; account = new Bittrex(apiKey, apiSecret); //TODO: Modify library so it uses securestring when handling apiSecret //TODO: Maybe change this task so it can use the "testConnection" method Task.Run(async() => { var response = await account.GetBalances(); if (!fFailedConection) { fFailedConection = !response.Success; } if (fFailedConection) { return; } fConnected = true; }); }
public void GetBalances_ShouldNotThrowException() { var bittrex = new Bittrex(DefaultApiKey, DefaultApiSecret); Func <Task> action = async() => { var _ = await bittrex.GetBalances(); }; action.ShouldNotThrow(); }
public async Task <List <BaseTypes.CurrencyBalance> > GetAllBalances(InvokePrint Print) { try { var bal = await bittrex.GetBalances(); List <BaseTypes.CurrencyBalance> balances = new List <BaseTypes.CurrencyBalance>(); foreach (var t in bal) { balances.Add(new BaseTypes.CurrencyBalance { Currency = t.Currency, Balance = t.Balance, Available = t.Available }); } return(balances); } catch (Exception ex) { System.Media.SystemSounds.Beep.Play(); Print("Ошибка BittrexApi GetAllBalances: " + ex.Message); return(null); } }
public static async Task <bool> testConnection(string apiKey, string apiSecret) { Bittrex TempAccount = new Bittrex(apiKey, apiSecret); var response = await TempAccount.GetBalances(); TempAccount = null; return(response.Success); }
public async Task <decimal> GetBalanceBtc() { var all = await bittrex.GetBalances(); decimal total = 0; decimal price = 0; foreach (var balance in all.Result.Where(x => x.Balance > 0)) { decimal estBtcValue = 0; if (balance.Currency == "BTC") { estBtcValue = balance.Balance; total += estBtcValue; } else if (balance.Currency == "USDT") { var market = await bittrex.GetMarketSummary("USDT-BTC"); price = market.Result.Last; estBtcValue = balance.Balance / price; total += estBtcValue; } else { try { var market = await bittrex.GetMarketSummary("BTC-" + balance.Currency); price = market.Result.Last; estBtcValue = price * balance.Balance; total += estBtcValue; } catch { // LOG } } } return(total); }
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)); }
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("BTC", 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)); }