Search in sources :

Example 56 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class TradeManagerTest method testTradeChannelHandler2.

/**
 * Test the trade channel handler
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testTradeChannelHandler2() throws APIException, InterruptedException {
    final String jsonString = "[0,\"tu\",[106655593,\"tBTCUSD\",1512247319827,5691690918,-0.002,10894,\"EXCHANGE MARKET\",10894,-1,-0.0392184,\"USD\"]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final TradeHandler tradeHandler = new TradeHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final CountDownLatch latch = new CountDownLatch(1);
    bitfinexApiBroker.getTradeManager().registerCallback((t) -> {
        try {
            Assert.assertFalse(t.isExecuted());
            Assert.assertEquals(106655593, t.getId());
            Assert.assertEquals(BitfinexCurrencyPair.BTC_USD, t.getCurrency());
            Assert.assertEquals(1512247319827l, t.getMtsCreate());
            Assert.assertEquals(5691690918l, t.getOrderId());
            Assert.assertEquals(-0.002, t.getExecAmount().doubleValue(), DELTA);
            Assert.assertEquals(10894, t.getExecPrice().doubleValue(), DELTA);
            Assert.assertEquals(BitfinexOrderType.EXCHANGE_MARKET, t.getOrderType());
            Assert.assertEquals(10894, t.getOrderPrice().doubleValue(), DELTA);
            Assert.assertFalse(t.isMaker());
            Assert.assertEquals(-0.0392184, t.getFee().doubleValue(), DELTA);
            Assert.assertEquals("USD", t.getFeeCurrency());
            Assert.assertTrue(t.toString().length() > 0);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
        latch.countDown();
    });
    tradeHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    latch.await();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) CountDownLatch(java.util.concurrent.CountDownLatch) TradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.TradeHandler) Test(org.junit.Test)

Example 57 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletSnapshot.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletSnapshot() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[[\"exchange\",\"ETH\",9,0,null],[\"exchange\",\"USD\",1826.56468323,0,null],[\"margin\",\"USD\",0,0,null],[\"exchange\",\"XRP\",0,0,null],[\"exchange\",\"EOS\",0,0,null],[\"exchange\",\"NEO\",0,0,null],[\"exchange\",\"LTC\",0,0,null],[\"exchange\",\"IOT\",0,0,null],[\"exchange\",\"BTC\",0,0,null]]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(9, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(1826.56468323, walletTable.get("exchange", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("margin", "USD").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertTrue(walletTable.get("margin", "USD").toString().length() > 0);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletUpdate.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletUpdate() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[\"exchange\",\"ETH\",9,0,null]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(1, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 59 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletManager method calculateWalletMarginBalance.

/**
 * Calculate the wallet margin balance for the given currency (e.g., BTC)
 *
 * @param symbol
 * @throws APIException
 */
public void calculateWalletMarginBalance(final String symbol) throws APIException {
    throwExceptionIfUnauthenticated();
    bitfinexApiBroker.sendCommand(new CalculateCommand("wallet_margin_" + symbol));
}
Also used : CalculateCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CalculateCommand)

Example 60 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletManager method calculateWalletFundingBalance.

/**
 * Calculate the wallet funding balance for the given currency (e.g., BTC)
 *
 * @param symbol
 * @throws APIException
 */
public void calculateWalletFundingBalance(final String symbol) throws APIException {
    throwExceptionIfUnauthenticated();
    bitfinexApiBroker.sendCommand(new CalculateCommand("wallet_funding_" + symbol));
}
Also used : CalculateCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CalculateCommand)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)33 Test (org.junit.Test)33 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)25 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)20 JSONArray (org.json.JSONArray)20 CountDownLatch (java.util.concurrent.CountDownLatch)14 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)12 HashMap (java.util.HashMap)11 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)10 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)9 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)9 Wallet (com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)9 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)8 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)8 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)8 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)8 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)7 PortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager)7 ArrayList (java.util.ArrayList)7