Search in sources :

Example 51 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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 52 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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 53 with BitfinexApiBroker

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

the class CandlestickHandlerTest method testCandlestickUpdateAndNotify.

/**
 * Test the parsing of one candlestick
 * @throws APIException
 */
@Test
public void testCandlestickUpdateAndNotify() throws APIException {
    final String callbackValue = "[15134900000,15996,15997,16000,15980,318.5139342]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
    final AtomicInteger counter = new AtomicInteger(0);
    tickerManager.registerCandlestickCallback(symbol, (s, c) -> {
        counter.incrementAndGet();
        Assert.assertEquals(symbol, s);
        Assert.assertEquals(15996, c.getOpen().doubleValue(), DELTA);
        Assert.assertEquals(15997, c.getClose().doubleValue(), DELTA);
        Assert.assertEquals(16000, c.getHigh().doubleValue(), DELTA);
        Assert.assertEquals(15980, c.getLow().doubleValue(), DELTA);
        Assert.assertEquals(318.5139342, c.getVolume().doubleValue(), DELTA);
    });
    final CandlestickHandler candlestickHandler = new CandlestickHandler();
    candlestickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    Assert.assertEquals(1, counter.get());
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) Test(org.junit.Test)

Example 54 with BitfinexApiBroker

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

the class CandlestickHandlerTest method testCandlestickSnapshotUpdateAndNotify.

/**
 * Test the parsing of a candlestick snapshot
 * @throws APIException
 */
@Test
public void testCandlestickSnapshotUpdateAndNotify() throws APIException {
    final String callbackValue = "[[15134900000,15996,15997,16000,15980,318.5139342],[15135100000,15899,15996,16097,15890,1137.180342268]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
    final AtomicInteger counter = new AtomicInteger(0);
    tickerManager.registerCandlestickCallback(symbol, (s, c) -> {
        Assert.assertEquals(symbol, s);
        final int counterValue = counter.getAndIncrement();
        if (counterValue == 0) {
            Assert.assertEquals(15996, c.getOpen().doubleValue(), DELTA);
            Assert.assertEquals(15997, c.getClose().doubleValue(), DELTA);
            Assert.assertEquals(16000, c.getHigh().doubleValue(), DELTA);
            Assert.assertEquals(15980, c.getLow().doubleValue(), DELTA);
            Assert.assertEquals(318.5139342, c.getVolume().doubleValue(), DELTA);
        } else if (counterValue == 1) {
            Assert.assertEquals(15899, c.getOpen().doubleValue(), DELTA);
            Assert.assertEquals(15996, c.getClose().doubleValue(), DELTA);
            Assert.assertEquals(16097, c.getHigh().doubleValue(), DELTA);
            Assert.assertEquals(15890, c.getLow().doubleValue(), DELTA);
            Assert.assertEquals(1137.180342268, c.getVolume().doubleValue(), DELTA);
        } else {
            throw new IllegalArgumentException("Illegal call, expected 2 candlesticks");
        }
    });
    final CandlestickHandler candlestickHandler = new CandlestickHandler();
    candlestickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    Assert.assertEquals(2, counter.get());
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) Test(org.junit.Test)

Example 55 with BitfinexApiBroker

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

the class CommandsCallbackTest method testSubscribeAndUnsubscribeCallback.

/**
 * Test the subscribed callback
 * @throws APIException
 */
@Test
public void testSubscribeAndUnsubscribeCallback() throws APIException {
    final String jsonString = "{\"event\":\"subscribed\",\"channel\":\"ticker\",\"chanId\":30,\"symbol\":\"tNEOUSD\",\"pair\":\"NEOUSD\"}";
    final JSONObject jsonObject = new JSONObject(jsonString);
    final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
    final SubscribedCallback subscribedCallback = new SubscribedCallback();
    subscribedCallback.handleChannelData(bitfinexApiBroker, jsonObject);
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) instanceof BitfinexTickerSymbol);
    final String unsubscribedJsonString = "{\"event\":\"unsubscribed\",\"status\":\"OK\",\"chanId\":30}";
    final JSONObject jsonUnsubscribedObject = new JSONObject(unsubscribedJsonString);
    final UnsubscribedCallback unsubscribedCallback = new UnsubscribedCallback();
    unsubscribedCallback.handleChannelData(bitfinexApiBroker, jsonUnsubscribedObject);
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONObject(org.json.JSONObject) SubscribedCallback(com.github.jnidzwetzki.bitfinex.v2.callback.command.SubscribedCallback) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) UnsubscribedCallback(com.github.jnidzwetzki.bitfinex.v2.callback.command.UnsubscribedCallback) Test(org.junit.Test)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)46 Test (org.junit.Test)36 JSONArray (org.json.JSONArray)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)15 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)15 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)12 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)12 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)11 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)11 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)9 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)9 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)9 ExecutorService (java.util.concurrent.ExecutorService)9 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)7 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)7 Assert (org.junit.Assert)7 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6