Search in sources :

Example 41 with BitfinexApiBroker

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

the class IntegrationTest method testReconnect.

/**
 * Test the session reconnect
 * @throws APIException
 * @throws InterruptedException
 */
@Test
public void testReconnect() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    bitfinexClient.connect();
    final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
    final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
    orderbookManager.subscribeTicker(symbol);
    Thread.sleep(1000);
    bitfinexClient.reconnect();
    // Await at least 2 callbacks
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
        latch.countDown();
    };
    orderbookManager.registerTickCallback(symbol, callback);
    latch.await();
    Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
    orderbookManager.unsubscribeTicker(symbol);
    Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
    bitfinexClient.close();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 42 with BitfinexApiBroker

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

the class OrderManagerTest method testOrderSubmissionFailed.

/**
 * Test order submit failed
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testOrderSubmissionFailed() throws APIException, InterruptedException {
    final String jsonString = "[0,\"n\",[null,\"on-req\",null,null,[null,null,1513970684865000,\"tBTCUSD\",null,null,0.001,0.001,\"EXCHANGE MARKET\",null,null,null,null,null,null,null,12940,null,null,null,null,null,null,0,null,null],null,\"ERROR\",\"Invalid order: minimum size for BTC/USD is 0.002\"]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final CountDownLatch latch = new CountDownLatch(1);
    final Consumer<ExchangeOrder> orderCallback = (e) -> {
        Assert.assertEquals(ExchangeOrderState.STATE_ERROR, e.getState());
        Assert.assertEquals(API_KEY, e.getApikey());
        Assert.assertEquals(1513970684865000l, e.getCid());
        Assert.assertEquals(BitfinexCurrencyPair.BTC_USD.toBitfinexString(), e.getSymbol());
        latch.countDown();
    };
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    bitfinexApiBroker.getOrderManager().registerCallback(orderCallback);
    final NotificationHandler notificationHandler = new NotificationHandler();
    notificationHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    latch.await();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) NotificationHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.NotificationHandler) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) Test(org.junit.Test) BitfinexOrderBuilder(com.github.jnidzwetzki.bitfinex.v2.BitfinexOrderBuilder) ExchangeOrderState(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState) Executors(java.util.concurrent.Executors) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Consumer(java.util.function.Consumer) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) BitfinexOrderType(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrderType) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Assert(org.junit.Assert) ExecutorService(java.util.concurrent.ExecutorService) JSONArray(org.json.JSONArray) OrderHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) NotificationHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.NotificationHandler) JSONArray(org.json.JSONArray) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) Test(org.junit.Test)

Example 43 with BitfinexApiBroker

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

the class OrderManagerTest method testCancelOrder.

/**
 * Test the cancelation of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(timeout = 60000)
public void testCancelOrder() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final Runnable r = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return;
        }
        final ExchangeOrder exchangeOrder = new ExchangeOrder();
        exchangeOrder.setOrderId(12);
        exchangeOrder.setState(ExchangeOrderState.STATE_CANCELED);
        orderManager.updateOrder(exchangeOrder);
    };
    // Cancel event
    (new Thread(r)).start();
    orderManager.cancelOrderAndWaitForCompletion(12);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 44 with BitfinexApiBroker

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

the class OrderManagerTest method testOrderChannelHandler1.

/**
 * Test the order channel handler - single order
 * @throws APIException
 */
@Test
public void testOrderChannelHandler1() throws APIException {
    final String jsonString = "[0,\"on\",[6784335053,null,1514956504945000,\"tIOTUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final OrderHandler orderHandler = new OrderHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    Assert.assertTrue(orderManager.getOrders().isEmpty());
    orderHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    Assert.assertEquals(1, orderManager.getOrders().size());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) OrderHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler) Test(org.junit.Test)

Example 45 with BitfinexApiBroker

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

the class OrderManagerTest method buildMockedBitfinexConnection.

/**
 * Build a mocked bitfinex connection
 * @return
 */
private BitfinexApiBroker buildMockedBitfinexConnection() {
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    Mockito.when(bitfinexApiBroker.getApiKey()).thenReturn(API_KEY);
    Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
    Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.ALL_CAPABILITIES);
    final OrderManager orderManager = new OrderManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getOrderManager()).thenReturn(orderManager);
    return bitfinexApiBroker;
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) ExecutorService(java.util.concurrent.ExecutorService) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)

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