Search in sources :

Example 6 with ExchangeOrder

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

Example 7 with ExchangeOrder

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

the class OrderManager method cancelOrderOnAPI.

/**
 * Cancel the order on the API
 * @param id
 * @return
 * @throws APIException
 * @throws InterruptedException
 */
private boolean cancelOrderOnAPI(final long id) throws APIException, InterruptedException {
    final CountDownLatch waitLatch = new CountDownLatch(1);
    final Consumer<ExchangeOrder> ordercallback = (o) -> {
        if (o.getOrderId() == id && o.getState() == ExchangeOrderState.STATE_CANCELED) {
            waitLatch.countDown();
        }
    };
    registerCallback(ordercallback);
    try {
        logger.info("Cancel order: {}", id);
        cancelOrder(id);
        waitLatch.await(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
        if (waitLatch.getCount() != 0) {
            throw new APIException("Timeout while waiting for 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)

Example 8 with ExchangeOrder

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

the class NotificationHandler method handleErrorNotification.

/**
 * Handle the error notification
 *
 * @param bitfinexApiBroker
 * @param order
 * @param state
 * @param stateValue
 */
private void handleErrorNotification(final BitfinexApiBroker bitfinexApiBroker, final JSONArray order, final String state, final String stateValue) {
    final ExchangeOrder exchangeOrder = new ExchangeOrder();
    exchangeOrder.setApikey(bitfinexApiBroker.getApiKey());
    exchangeOrder.setCid(order.getLong(2));
    exchangeOrder.setSymbol(order.getString(3));
    exchangeOrder.setState(ExchangeOrderState.STATE_ERROR);
    logger.error("State for order {} is {}, reason is {}", exchangeOrder.getOrderId(), state, stateValue);
    bitfinexApiBroker.getOrderManager().updateOrder(exchangeOrder);
}
Also used : ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 9 with ExchangeOrder

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

the class OrderHandler method handleOrderCallback.

/**
 * Handle a single order callback
 * @param bitfinexApiBroker
 * @param orderArray
 * @throws APIException
 */
private void handleOrderCallback(BitfinexApiBroker bitfinexApiBroker, final JSONArray order) throws APIException {
    final ExchangeOrder exchangeOrder = new ExchangeOrder();
    exchangeOrder.setApikey(bitfinexApiBroker.getApiKey());
    exchangeOrder.setOrderId(order.getLong(0));
    exchangeOrder.setGroupId(order.optInt(1, -1));
    exchangeOrder.setCid(order.optLong(2, -1));
    exchangeOrder.setSymbol(order.getString(3));
    exchangeOrder.setCreated(order.getLong(4));
    exchangeOrder.setUpdated(order.getLong(5));
    exchangeOrder.setAmount(order.getBigDecimal(6));
    exchangeOrder.setAmountAtCreation(order.getBigDecimal(7));
    exchangeOrder.setOrderType(BitfinexOrderType.fromString(order.getString(8)));
    final ExchangeOrderState orderState = ExchangeOrderState.fromString(order.getString(13));
    exchangeOrder.setState(orderState);
    exchangeOrder.setPrice(order.optBigDecimal(16, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAvg(order.optBigDecimal(17, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceTrailing(order.optBigDecimal(18, BigDecimal.valueOf(-1)));
    exchangeOrder.setPriceAuxLimit(order.optBigDecimal(19, BigDecimal.valueOf(-1)));
    exchangeOrder.setNotify(order.getInt(23) == 1 ? true : false);
    exchangeOrder.setHidden(order.getInt(24) == 1 ? true : false);
    bitfinexApiBroker.getOrderManager().updateOrder(exchangeOrder);
}
Also used : ExchangeOrderState(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 10 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.

the class PortfolioManager method placeNewEntryOrders.

/**
 * Place the new entry orders
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void placeNewEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    // Check current limits and position sizes
    for (final BitfinexCurrencyPair currency : entries.keySet()) {
        final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
        final CurrencyEntry entry = entries.get(currency);
        final double entryPrice = entry.getEntryPrice();
        final double positionSize = entry.getPositionSize();
        if (positionSize < currency.getMinimumOrderSize()) {
            logger.info("Not placing order for {}, position size is too small {}", currency, positionSize);
            continue;
        }
        // Old order present
        if (order != null) {
            if (hasEntryOrderChanged(order, entryPrice, positionSize)) {
                logger.info("Entry order for {}, values changed (amount: {} / {}} (price: {} / {})", currency, order.getAmount(), positionSize, order.getPrice(), entryPrice);
                cancelOrder(order);
            } else {
                logger.info("Not placing a new order for {}, old order still active", currency);
                continue;
            }
        }
        final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSize).withPrice(entryPrice).setPostOnly().build();
        placeOrder(newOrder);
    }
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Aggregations

ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)12 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)6 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)6 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)5 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)4 ExchangeOrderState (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState)4 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)3 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Consumer (java.util.function.Consumer)3 Test (org.junit.Test)3 CancelOrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand)2 CancelOrderGroupCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand)2 OrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand)2 Callable (java.util.concurrent.Callable)2 TimeUnit (java.util.concurrent.TimeUnit)2 Retryer (org.bboxdb.commons.Retryer)2 Logger (org.slf4j.Logger)2