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