Search in sources :

Example 1 with BitfinexOrderStatusResponse

use of org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse in project XChange by knowm.

the class BitfinexStreamingAdapters method adaptOrder.

static Order adaptOrder(BitfinexWebSocketAuthOrder authOrder) {
    BitfinexOrderStatusResponse[] orderStatus = { adaptOrderToRestResponse(authOrder) };
    OpenOrders orders = BitfinexAdapters.adaptOrders(orderStatus);
    if (orders.getOpenOrders().isEmpty()) {
        if (orders.getHiddenOrders().isEmpty()) {
            throw new IllegalStateException("No order in message");
        }
        return orders.getHiddenOrders().get(0);
    }
    return orders.getOpenOrders().get(0);
}
Also used : OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse)

Example 2 with BitfinexOrderStatusResponse

use of org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse 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);
    }
}
Also used : BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) BitfinexException(org.knowm.xchange.bitfinex.dto.BitfinexException) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse)

Example 3 with BitfinexOrderStatusResponse

use of org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse 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;
}
Also used : BitfinexNewOrderRequest(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexNewOrderRequest) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) BigDecimal(java.math.BigDecimal) BitfinexLimitOrder(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexLimitOrder) BitfinexReplaceOrderRequest(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexReplaceOrderRequest)

Example 4 with BitfinexOrderStatusResponse

use of org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse 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);
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) Order(org.knowm.xchange.dto.Order) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) StopOrder(org.knowm.xchange.dto.trade.StopOrder) Arrays(java.util.Arrays) OrderType(org.knowm.xchange.dto.Order.OrderType) Date(java.util.Date) BitfinexBalancesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse) LoggerFactory(org.slf4j.LoggerFactory) BitfinexLendLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLendLevel) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) BitfinexDepositWithdrawalHistoryResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse) Map(java.util.Map) BitfinexTrade(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTrade) BitfinexPublicTrade(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexPublicTrade) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) Wallet(org.knowm.xchange.dto.account.Wallet) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) MathContext(java.math.MathContext) BitfinexAccountInfosResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexAccountInfosResponse) BitfinexTradeResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse) Collection(java.util.Collection) Set(java.util.Set) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) Stream(java.util.stream.Stream) BitfinexLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLevel) Entry(java.util.Map.Entry) Optional(java.util.Optional) BitfinexTicker(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTicker) BitfinexDepth(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexDepth) Movement(org.knowm.xchange.bitfinex.v2.dto.account.Movement) CurrencyPairDeserializer(org.knowm.xchange.utils.jackson.CurrencyPairDeserializer) BitfinexUtils(org.knowm.xchange.bitfinex.v1.BitfinexUtils) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) Supplier(java.util.function.Supplier) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) TradeSortType(org.knowm.xchange.dto.marketdata.Trades.TradeSortType) BitfinexTickerFundingCurrency(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency) Order(org.knowm.xchange.dto.Order) BitfinexSymbolDetail(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexSymbolDetail) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) ExchangeMetaData(org.knowm.xchange.dto.meta.ExchangeMetaData) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Logger(org.slf4j.Logger) Fee(org.knowm.xchange.dto.account.Fee) BitfinexAccountFeesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexAccountFeesResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Trades(org.knowm.xchange.dto.marketdata.Trades) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) BitfinexTradingFeeResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) Trade(org.knowm.xchange.dto.marketdata.Trade) BitfinexOrderFlags(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderFlags) BitfinexTickerTraidingPair(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerTraidingPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) WalletHealth(org.knowm.xchange.dto.meta.WalletHealth) DateUtils(org.knowm.xchange.utils.DateUtils) ArrayList(java.util.ArrayList) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Date(java.util.Date) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) OrderType(org.knowm.xchange.dto.Order.OrderType) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) StopOrder(org.knowm.xchange.dto.trade.StopOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 5 with BitfinexOrderStatusResponse

use of org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse in project XChange by knowm.

the class BitfinexTradeService method getOrder.

@Override
public Collection<Order> getOrder(String... orderIds) throws IOException {
    try {
        List<Order> openOrders = new ArrayList<>();
        for (String orderId : orderIds) {
            BitfinexOrderStatusResponse orderStatus = getBitfinexOrderStatus(orderId);
            BitfinexOrderStatusResponse[] orderStatuses = new BitfinexOrderStatusResponse[1];
            if (orderStatus != null) {
                orderStatuses[0] = orderStatus;
                OpenOrders orders = BitfinexAdapters.adaptOrders(orderStatuses);
                openOrders.add(orders.getOpenOrders().get(0));
            }
        }
        return openOrders;
    } catch (BitfinexException e) {
        throw BitfinexErrorAdapter.adapt(e);
    }
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) ArrayList(java.util.ArrayList) BitfinexException(org.knowm.xchange.bitfinex.dto.BitfinexException) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse)

Aggregations

BitfinexOrderStatusResponse (org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse)8 BigDecimal (java.math.BigDecimal)4 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)4 BitfinexException (org.knowm.xchange.bitfinex.dto.BitfinexException)3 Order (org.knowm.xchange.dto.Order)3 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)3 ArrayList (java.util.ArrayList)2 BitfinexOrderType (org.knowm.xchange.bitfinex.v1.BitfinexOrderType)2 OrderType (org.knowm.xchange.dto.Order.OrderType)2 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)2 StopOrder (org.knowm.xchange.dto.trade.StopOrder)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 MathContext (java.math.MathContext)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1