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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations