use of org.knowm.xchange.bitfinex.v1.BitfinexOrderType in project XChange by knowm.
the class BitfinexTradeService method placeLimitOrder.
@Override
public String placeLimitOrder(LimitOrder limitOrder) throws IOException {
try {
BitfinexOrderType type = BitfinexAdapters.adaptOrderFlagsToType(limitOrder.getOrderFlags());
BitfinexOrderStatusResponse newOrder = placeBitfinexLimitOrder(limitOrder, type);
return String.valueOf(newOrder.getId());
} catch (BitfinexException e) {
throw BitfinexErrorAdapter.adapt(e);
}
}
use of org.knowm.xchange.bitfinex.v1.BitfinexOrderType in project XChange by knowm.
the class BitfinexTradeServiceRaw method sendLimitOrder.
private BitfinexOrderStatusResponse sendLimitOrder(LimitOrder limitOrder, BitfinexOrderType bitfinexOrderType, long replaceOrderId) throws IOException {
String pair = BitfinexUtils.toPairStringV1(limitOrder.getCurrencyPair());
String type = (limitOrder.getType().equals(Order.OrderType.BID) || limitOrder.getType().equals(Order.OrderType.EXIT_ASK)) ? "buy" : "sell";
String orderType = bitfinexOrderType.toString();
boolean isHidden;
if (limitOrder.hasFlag(BitfinexOrderFlags.HIDDEN)) {
isHidden = true;
} else {
isHidden = false;
}
boolean isPostOnly;
if (limitOrder.hasFlag(BitfinexOrderFlags.POST_ONLY)) {
isPostOnly = true;
} else {
isPostOnly = false;
}
BitfinexOrderStatusResponse response;
if (replaceOrderId == Long.MIN_VALUE) {
// order entry
BigDecimal ocoAmount = limitOrder instanceof BitfinexLimitOrder ? ((BitfinexLimitOrder) limitOrder).getOcoStopLimit() : null;
BitfinexNewOrderRequest request = new BitfinexNewOrderRequest(String.valueOf(exchange.getNonceFactory().createValue()), pair, limitOrder.getOriginalAmount(), limitOrder.getLimitPrice(), "bitfinex", type, orderType, isHidden, isPostOnly, ocoAmount);
response = decorateApiCall(() -> bitfinex.newOrder(apiKey, payloadCreator, signatureCreator, request)).withRateLimiter(rateLimiter(BITFINEX_RATE_LIMITER)).call();
} else {
// order amend
boolean useRemaining = limitOrder.hasFlag(BitfinexOrderFlags.USE_REMAINING);
BitfinexReplaceOrderRequest request = new BitfinexReplaceOrderRequest(String.valueOf(exchange.getNonceFactory().createValue()), replaceOrderId, pair, limitOrder.getOriginalAmount(), limitOrder.getLimitPrice(), "bitfinex", type, orderType, isHidden, isPostOnly, useRemaining);
response = decorateApiCall(() -> bitfinex.replaceOrder(apiKey, payloadCreator, signatureCreator, request)).withRateLimiter(rateLimiter(BITFINEX_RATE_LIMITER)).call();
}
if (limitOrder instanceof BitfinexLimitOrder) {
BitfinexLimitOrder bitfinexOrder = (BitfinexLimitOrder) limitOrder;
bitfinexOrder.setResponse(response);
}
return response;
}
use of org.knowm.xchange.bitfinex.v1.BitfinexOrderType in project XChange by knowm.
the class BitfinexTradeServiceRaw method placeBitfinexOrderMulti.
public BitfinexNewOrderMultiResponse placeBitfinexOrderMulti(List<? extends Order> orders, BitfinexOrderType bitfinexOrderType) throws IOException {
BitfinexNewOrder[] bitfinexOrders = new BitfinexNewOrder[orders.size()];
for (int i = 0; i < bitfinexOrders.length; i++) {
Order o = orders.get(i);
if (o instanceof LimitOrder) {
LimitOrder limitOrder = (LimitOrder) o;
String pair = BitfinexUtils.toPairStringV1(limitOrder.getCurrencyPair());
String type = (limitOrder.getType().equals(OrderType.BID) || limitOrder.getType().equals(OrderType.EXIT_ASK)) ? "buy" : "sell";
String orderType = bitfinexOrderType.toString();
bitfinexOrders[i] = new BitfinexNewOrder(pair, "bitfinex", type, orderType, limitOrder.getOriginalAmount(), limitOrder.getLimitPrice());
} else if (o instanceof MarketOrder) {
MarketOrder marketOrder = (MarketOrder) o;
String pair = BitfinexUtils.toPairStringV1(marketOrder.getCurrencyPair());
String type = (marketOrder.getType().equals(OrderType.BID) || marketOrder.getType().equals(OrderType.EXIT_ASK)) ? "buy" : "sell";
String orderType = bitfinexOrderType.toString();
bitfinexOrders[i] = new BitfinexNewOrder(pair, "bitfinex", type, orderType, marketOrder.getOriginalAmount(), BigDecimal.ONE);
}
}
BitfinexNewOrderMultiRequest request = new BitfinexNewOrderMultiRequest(String.valueOf(exchange.getNonceFactory().createValue()), bitfinexOrders);
return decorateApiCall(() -> bitfinex.newOrderMulti(apiKey, payloadCreator, signatureCreator, request)).withRateLimiter(rateLimiter(BITFINEX_RATE_LIMITER)).call();
}
use of org.knowm.xchange.bitfinex.v1.BitfinexOrderType in project XChange by knowm.
the class BitfinexAdapters method adaptOrders.
public static OpenOrders adaptOrders(BitfinexOrderStatusResponse[] activeOrders) {
List<LimitOrder> limitOrders = new ArrayList<>();
List<Order> hiddenOrders = new ArrayList<>();
for (BitfinexOrderStatusResponse order : activeOrders) {
OrderType orderType = order.getSide().equalsIgnoreCase("buy") ? OrderType.BID : OrderType.ASK;
OrderStatus status = adaptOrderStatus(order);
CurrencyPair currencyPair = adaptCurrencyPair(order.getSymbol());
Date timestamp = convertBigDecimalTimestampToDate(order.getTimestamp());
Supplier<MarketOrder> marketOrderCreator = () -> new MarketOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
Supplier<LimitOrder> limitOrderCreator = () -> new LimitOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
Supplier<StopOrder> stopOrderCreator = () -> new StopOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), null, order.getAvgExecutionPrice(), order.getExecutedAmount(), status);
LimitOrder limitOrder = null;
StopOrder stopOrder = null;
MarketOrder marketOrder = null;
Optional<BitfinexOrderType> bitfinexOrderType = Arrays.stream(BitfinexOrderType.values()).filter(v -> v.getValue().equals(order.getType())).findFirst();
if (bitfinexOrderType.isPresent()) {
switch(bitfinexOrderType.get()) {
case FILL_OR_KILL:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
break;
case MARGIN_FILL_OR_KILL:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_LIMIT:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_STOP:
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_STOP_LIMIT:
stopLimitWarning();
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case MARGIN_TRAILING_STOP:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
break;
case STOP:
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
break;
case STOP_LIMIT:
stopLimitWarning();
stopOrder = stopOrderCreator.get();
stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
break;
case TRAILING_STOP:
limitOrder = limitOrderCreator.get();
limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
break;
case LIMIT:
limitOrder = limitOrderCreator.get();
break;
case MARGIN_MARKET:
case MARKET:
marketOrder = marketOrderCreator.get();
break;
default:
log.warn("Unhandled Bitfinex order type [{}]. Defaulting to limit order", order.getType());
limitOrder = limitOrderCreator.get();
break;
}
} else {
log.warn("Unknown Bitfinex order type [{}]. Defaulting to limit order", order.getType());
limitOrder = limitOrderCreator.get();
}
if (limitOrder != null) {
limitOrders.add(limitOrder);
} else if (stopOrder != null) {
hiddenOrders.add(stopOrder);
} else if (marketOrder != null) {
hiddenOrders.add(marketOrder);
}
}
return new OpenOrders(limitOrders, hiddenOrders);
}
Aggregations