Search in sources :

Example 16 with APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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 APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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 APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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 APIException

use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException 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 APIException

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

the class OrderManager method placeOrderOrderOnAPI.

/**
 * Execute a new Order
 * @param order
 * @return
 * @throws Exception
 */
private boolean placeOrderOrderOnAPI(final BitfinexOrder order) throws Exception {
    final CountDownLatch waitLatch = new CountDownLatch(1);
    final Consumer<ExchangeOrder> ordercallback = (o) -> {
        if (o.getCid() == order.getCid()) {
            waitLatch.countDown();
        }
    };
    registerCallback(ordercallback);
    try {
        placeOrder(order);
        waitLatch.await(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
        if (waitLatch.getCount() != 0) {
            throw new APIException("Timeout while waiting for order");
        }
        // Check for order error
        final boolean orderInErrorState = bitfinexApiBroker.getOrderManager().getOrders().stream().filter(o -> o.getCid() == order.getCid()).anyMatch(o -> o.getState() == ExchangeOrderState.STATE_ERROR);
        if (orderInErrorState) {
            throw new APIException("Unable to place order " + order);
        }
        return true;
    } catch (Exception e) {
        throw e;
    } finally {
        removeCallback(ordercallback);
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Logger(org.slf4j.Logger) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) Retryer(org.bboxdb.commons.Retryer) ExchangeOrderState(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState) ArrayList(java.util.ArrayList) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) CancelOrderGroupCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException)

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