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