Search in sources :

Example 16 with BitfinexApiBroker

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

the class OrderManagerTest method testOrderChannelHandler3.

/**
 * Test the order channel handler - posclose order
 * @throws APIException
 */
@Test
public void testOrderChannelHandler3() throws APIException {
    final String jsonString = "[0,\"on\",[6827301913,null,null,\"tXRPUSD\",1515069803530,1515069803530,-60,-60,\"MARKET\",null,null,null,0,\"ACTIVE (note:POSCLOSE)\",null,null,0,3.2041,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 17 with BitfinexApiBroker

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

the class OrderManagerTest method testPlaceOrderUnauth.

/**
 * Test the placement of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(expected = APIException.class)
public void testPlaceOrderUnauth() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.NO_CAPABILITIES);
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 12).build();
    orderManager.placeOrderAndWaitUntilActive(order);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 18 with BitfinexApiBroker

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

the class OrderManagerTest method testPlaceOrder.

/**
 * Test the placement of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(timeout = 60000)
public void testPlaceOrder() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 1).build();
    final Runnable r = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return;
        }
        final ExchangeOrder exchangeOrder = new ExchangeOrder();
        exchangeOrder.setCid(order.getCid());
        exchangeOrder.setState(ExchangeOrderState.STATE_ACTIVE);
        orderManager.updateOrder(exchangeOrder);
    };
    // Cancel event
    (new Thread(r)).start();
    orderManager.placeOrderAndWaitUntilActive(order);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 19 with BitfinexApiBroker

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

the class CommandsCallbackTest method testAuthCommandCallback1.

/**
 * Test the auth callback
 * @throws APIException
 */
@Test
public void testAuthCommandCallback1() throws APIException {
    final String authCallback = "{\"event\":\"auth\",\"status\":\"OK\",\"chanId\":0,\"userId\":1015301,\"auth_id\":\"d5c6a71c-6164-40dd-b57a-92fb59d42975\",\"caps\":{\"orders\":{\"read\":1,\"write\":1},\"account\":{\"read\":1,\"write\":0},\"funding\":{\"read\":1,\"write\":1},\"history\":{\"read\":1,\"write\":0},\"wallets\":{\"read\":1,\"write\":1},\"withdraw\":{\"read\":0,\"write\":0},\"positions\":{\"read\":1,\"write\":1}}}";
    final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
    final JSONObject jsonObject = new JSONObject(authCallback);
    final AuthCallbackHandler authCallbackHandler = new AuthCallbackHandler();
    Assert.assertFalse(bitfinexApiBroker.isAuthenticated());
    Assert.assertEquals(ConnectionCapabilities.NO_CAPABILITIES, bitfinexApiBroker.getCapabilities());
    authCallbackHandler.handleChannelData(bitfinexApiBroker, jsonObject);
    Assert.assertTrue(bitfinexApiBroker.isAuthenticated());
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    Assert.assertTrue(capabilities.isHavingOrdersReadCapability());
    Assert.assertTrue(capabilities.isHavingOrdersWriteCapability());
    Assert.assertTrue(capabilities.isHavingAccountReadCapability());
    Assert.assertFalse(capabilities.isHavingAccountWriteCapability());
    Assert.assertTrue(capabilities.isHavingFundingReadCapability());
    Assert.assertTrue(capabilities.isHavingFundingWriteCapability());
    Assert.assertTrue(capabilities.isHavingHistoryReadCapability());
    Assert.assertFalse(capabilities.isHavingHistoryWriteCapability());
    Assert.assertTrue(capabilities.isHavingWalletsReadCapability());
    Assert.assertTrue(capabilities.isHavingWalletsWriteCapability());
    Assert.assertFalse(capabilities.isHavingWithdrawReadCapability());
    Assert.assertFalse(capabilities.isHavingWithdrawWriteCapability());
    Assert.assertTrue(capabilities.isHavingPositionReadCapability());
    Assert.assertTrue(capabilities.isHavingPositionWriteCapability());
    Assert.assertTrue(capabilities.toString().length() > 10);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONObject(org.json.JSONObject) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) AuthCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.command.AuthCallbackHandler) Test(org.junit.Test)

Example 20 with BitfinexApiBroker

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

the class TickHandlerTest method testTickUpdateAndNotify.

/**
 * Test the parsing of one tick
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testTickUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[26123,41.4645776,26129,33.68138507,2931,0.2231,26129,144327.10936387,26149,13139]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.BTC_USD;
    final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currencyPair);
    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 CountDownLatch latch = new CountDownLatch(1);
    tickerManager.registerTickCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            Assert.assertEquals(26129, c.getOpen().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getClose().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getHigh().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getLow().doubleValue(), DELTA);
            Assert.assertEquals(BitfinexTick.INVALID_VOLUME.doubleValue(), c.getVolume().doubleValue(), DELTA);
        } catch (Throwable e) {
            System.out.println(e);
            throw e;
        }
        latch.countDown();
    });
    Assert.assertEquals(-1, tickerManager.getHeartbeatForSymbol(symbol));
    Assert.assertEquals(null, tickerManager.getLastTick(symbol));
    final TickHandler tickHandler = new TickHandler();
    final long now = System.currentTimeMillis();
    tickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    // Tick callbacks are handled async
    latch.await();
    Assert.assertTrue(now <= tickerManager.getHeartbeatForSymbol(symbol));
    Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
    Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler) 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