Search in sources :

Example 26 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class HuobiAdapters method adaptWallet.

public static Wallet adaptWallet(Map<String, HuobiBalanceSum> huobiWallet) {
    List<Balance> balances = new ArrayList<>(huobiWallet.size());
    for (Map.Entry<String, HuobiBalanceSum> record : huobiWallet.entrySet()) {
        try {
            Currency currency = adaptCurrency(record.getKey());
            if (currency == null) {
                // Avoid creating Balance objects with null currency.
                continue;
            }
            Balance balance = new Balance(currency, record.getValue().getTotal(), record.getValue().getAvailable(), record.getValue().getFrozen());
            balances.add(balance);
        } catch (ExchangeException e) {
        // It might be a new currency. Ignore the exception and continue with other currency.
        }
    }
    return Wallet.Builder.from(balances).build();
}
Also used : HuobiCurrency(org.knowm.xchange.huobi.dto.marketdata.HuobiCurrency) Currency(org.knowm.xchange.currency.Currency) ArrayList(java.util.ArrayList) HuobiBalanceSum(org.knowm.xchange.huobi.dto.account.HuobiBalanceSum) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) Balance(org.knowm.xchange.dto.account.Balance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class HuobiPrivateApiIntegration method getBalanceTest.

@Test
public void getBalanceTest() throws IOException {
    AccountService accountService = exchange.getAccountService();
    Balance balance = accountService.getAccountInfo().getWallet().getBalance(Currency.USDT);
    System.out.println(balance.toString());
    assertThat(balance).isNotNull();
}
Also used : AccountService(org.knowm.xchange.service.account.AccountService) HuobiAccountService(org.knowm.xchange.huobi.service.HuobiAccountService) Balance(org.knowm.xchange.dto.account.Balance) Test(org.junit.Test)

Example 28 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class TestSimulatedExchange method testBalanceIsReleasedOnCancel.

@Test
public void testBalanceIsReleasedOnCancel() throws IOException {
    // When
    String orderId = exchange.getTradeService().placeLimitOrder(new LimitOrder.Builder(BID, BTC_USD).limitPrice(new BigDecimal(10)).originalAmount(new BigDecimal("0.7")).build());
    exchange.getTradeService().cancelOrder(orderId);
    Balance baseBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(BTC);
    // THen
    assertThat(baseBalance.getTotal()).isEqualTo(INITIAL_BALANCE);
    assertThat(baseBalance.getFrozen()).isEqualTo(ZERO);
    assertThat(baseBalance.getAvailable()).isEqualTo(INITIAL_BALANCE);
}
Also used : Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 29 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class TestSimulatedExchange method testTradingMarketAsk.

@Test
public void testTradingMarketAsk() throws IOException {
    // When
    exchange.getTradeService().placeMarketOrder(new MarketOrder.Builder(ASK, BTC_USD).originalAmount(new BigDecimal("0.7")).build());
    OrderBook orderBook = exchange.getMarketDataService().getOrderBook(BTC_USD);
    Ticker ticker = exchange.getMarketDataService().getTicker(BTC_USD);
    Balance baseBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(BTC);
    Balance counterBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(USD);
    // Then
    assertThat(orderBook.getAsks()).hasSize(4);
    assertThat(orderBook.getBids()).hasSize(5);
    assertThat(ticker.getAsk()).isEqualTo(new BigDecimal(98));
    assertThat(ticker.getBid()).isEqualTo(new BigDecimal(96));
    assertThat(ticker.getLast()).isEqualTo(new BigDecimal(96));
    assertThat(getTradeHistory(exchange).getTrades()).hasSize(3);
    BigDecimal expectedUsdProceeds = new BigDecimal(97).multiply(new BigDecimal("0.40")).add(new BigDecimal(96).multiply(new BigDecimal("0.30")));
    assertThat(baseBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.subtract(new BigDecimal("0.70")));
    assertThat(baseBalance.getTotal()).isEqualTo(INITIAL_BALANCE.subtract(new BigDecimal("0.70")));
    assertThat(baseBalance.getFrozen()).isEqualTo(ZERO);
    assertThat(counterBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.add(expectedUsdProceeds));
    assertThat(counterBalance.getTotal()).isEqualTo(INITIAL_BALANCE.add(expectedUsdProceeds));
    assertThat(counterBalance.getFrozen()).isEqualTo(ZERO);
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Ticker(org.knowm.xchange.dto.marketdata.Ticker) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 30 with Balance

use of org.knowm.xchange.dto.account.Balance in project XChange by knowm.

the class TestSimulatedExchange method testTradingMarketBid.

@Test
public void testTradingMarketBid() throws IOException {
    // When
    exchange.getTradeService().placeMarketOrder(new MarketOrder.Builder(BID, BTC_USD).originalAmount(new BigDecimal("0.56")).build());
    OrderBook orderBook = exchange.getMarketDataService().getOrderBook(BTC_USD);
    Ticker ticker = exchange.getMarketDataService().getTicker(BTC_USD);
    Balance baseBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(BTC);
    Balance counterBalance = exchange.getAccountService().getAccountInfo().getWallet().getBalance(USD);
    // THen
    assertThat(orderBook.getAsks()).hasSize(3);
    assertThat(orderBook.getBids()).hasSize(6);
    assertThat(ticker.getAsk()).isEqualTo(new BigDecimal(99));
    assertThat(ticker.getBid()).isEqualTo(new BigDecimal(97));
    assertThat(ticker.getLast()).isEqualTo(new BigDecimal(99));
    assertThat(getTradeHistory(exchange).getTrades()).hasSize(3);
    BigDecimal expectedUsdCost = new BigDecimal(98).multiply(new BigDecimal("0.3")).add(new BigDecimal(99).multiply(new BigDecimal("0.26")));
    assertThat(baseBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.add(new BigDecimal("0.56")));
    assertThat(baseBalance.getTotal()).isEqualTo(INITIAL_BALANCE.add(new BigDecimal("0.56")));
    assertThat(baseBalance.getFrozen()).isEqualTo(ZERO);
    assertThat(counterBalance.getAvailable()).isEqualTo(INITIAL_BALANCE.subtract(expectedUsdCost));
    assertThat(counterBalance.getTotal()).isEqualTo(INITIAL_BALANCE.subtract(expectedUsdCost));
    assertThat(counterBalance.getFrozen()).isEqualTo(ZERO);
}
Also used : OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Ticker(org.knowm.xchange.dto.marketdata.Ticker) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Balance (org.knowm.xchange.dto.account.Balance)99 BigDecimal (java.math.BigDecimal)51 ArrayList (java.util.ArrayList)50 AccountInfo (org.knowm.xchange.dto.account.AccountInfo)50 Wallet (org.knowm.xchange.dto.account.Wallet)38 Currency (org.knowm.xchange.currency.Currency)37 Test (org.junit.Test)23 Map (java.util.Map)14 Date (java.util.Date)13 HashMap (java.util.HashMap)10 List (java.util.List)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)5 Ticker (org.knowm.xchange.dto.marketdata.Ticker)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Arrays (java.util.Arrays)4 HashSet (java.util.HashSet)4 Collection (java.util.Collection)3