Search in sources :

Example 1 with ConnectionCapabilities

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

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

the class OrderManager method placeOrderAndWaitUntilActive.

/**
 * Place an order and retry if Exception occur
 * @param order - new BitfinexOrder to place
 * @throws APIException
 * @throws InterruptedException
 */
public void placeOrderAndWaitUntilActive(final BitfinexOrder order) throws APIException, InterruptedException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to wait for order " + order + " connection has not enough capabilities: " + capabilities);
    }
    order.setApikey(bitfinexApiBroker.getApiKey());
    final Callable<Boolean> orderCallable = () -> placeOrderOrderOnAPI(order);
    // Bitfinex does not implement a happens-before relationship. Sometimes
    // canceling a stop-loss order and placing a new stop-loss order results
    // in an 'ERROR, reason is Invalid order: not enough exchange balance'
    // error for some seconds. The retryer tries to place the order up to
    // three times
    final Retryer<Boolean> retryer = new Retryer<>(ORDER_RETRIES, RETRY_DELAY_IN_MS, orderCallable);
    retryer.execute();
    if (retryer.getNeededExecutions() > 1) {
        logger.info("Nedded {} executions for placing the order", retryer.getNeededExecutions());
    }
    if (!retryer.isSuccessfully()) {
        final Exception lastException = retryer.getLastException();
        if (lastException == null) {
            throw new APIException("Unable to execute order");
        } else {
            throw new APIException(lastException);
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) Retryer(org.bboxdb.commons.Retryer) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException)

Example 3 with ConnectionCapabilities

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

the class OrderManager method placeOrder.

/**
 * Place a new order
 * @throws APIException
 */
public void placeOrder(final BitfinexOrder order) throws APIException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to place order " + order + " connection has not enough capabilities: " + capabilities);
    }
    logger.info("Executing new order {}", order);
    final OrderCommand orderCommand = new OrderCommand(order);
    bitfinexApiBroker.sendCommand(orderCommand);
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)

Example 4 with ConnectionCapabilities

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

the class AuthCallbackHandler method authSuccessfully.

/**
 * Auth was successfully
 *
 * @param bitfinexApiBroker
 * @param jsonObject
 * @param connectionReadyLatch
 */
private void authSuccessfully(final BitfinexApiBroker bitfinexApiBroker, final JSONObject jsonObject, final CountDownLatch connectionReadyLatch) {
    bitfinexApiBroker.setAuthenticated(true);
    final ConnectionCapabilities capabilities = new ConnectionCapabilities(jsonObject);
    bitfinexApiBroker.setCapabilities(capabilities);
    if (connectionReadyLatch != null) {
        connectionReadyLatch.countDown();
    }
}
Also used : ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)

Example 5 with ConnectionCapabilities

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

the class OrderManager method cancelOrderGroup.

/**
 * Cancel the given order group
 * @param cid
 * @param date
 * @throws APIException
 */
public void cancelOrderGroup(final int id) throws APIException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to cancel order group " + id + " connection has not enough capabilities: " + capabilities);
    }
    logger.info("Cancel order group {}", id);
    final CancelOrderGroupCommand cancelOrder = new CancelOrderGroupCommand(id);
    bitfinexApiBroker.sendCommand(cancelOrder);
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) CancelOrderGroupCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand)

Aggregations

ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)7 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 CancelOrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand)2 Retryer (org.bboxdb.commons.Retryer)2 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)1 AuthCallbackHandler (com.github.jnidzwetzki.bitfinex.v2.callback.command.AuthCallbackHandler)1 CancelOrderGroupCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand)1 OrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1