Search in sources :

Example 16 with Order

use of org.knowm.xchange.dto.Order in project XChange by knowm.

the class BityAdapters method adaptTrade.

public static UserTrade adaptTrade(BityOrder order) {
    BityInputTransaction inputT = order.getBityInputTransactions().get(0);
    BityOutputTransaction outputT = order.getBityOutputTransactions().get(0);
    BigDecimal fee = inputT.getPaymentProcessorFee();
    BigDecimal price = inputT.getAmount().divide(outputT.getAmount(), 6, RoundingMode.HALF_UP);
    CurrencyPair currencyPair = new CurrencyPair(outputT.getCurrency(), inputT.getCurrency());
    Order.OrderType orderType = order.getCategory().contains("BUY") ? Order.OrderType.BID : Order.OrderType.ASK;
    BigDecimal amount = outputT.getAmount();
    Date date = order.getTimestampCreated();
    String orderId = order.getResourceUri();
    return new UserTrade.Builder().type(orderType).originalAmount(amount).currencyPair(currencyPair).price(price).timestamp(date).id(orderId).orderId(orderId).feeAmount(fee).feeCurrency(currencyPair.counter).build();
}
Also used : Order(org.knowm.xchange.dto.Order) BityOrder(org.knowm.xchange.bity.dto.account.BityOrder) BityInputTransaction(org.knowm.xchange.bity.dto.account.BityInputTransaction) BigDecimal(java.math.BigDecimal) Date(java.util.Date) BityOutputTransaction(org.knowm.xchange.bity.dto.account.BityOutputTransaction) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 17 with Order

use of org.knowm.xchange.dto.Order in project XChange by knowm.

the class CoinmateAdapters method adaptOpenOrders.

public static List<LimitOrder> adaptOpenOrders(CoinmateOpenOrders coinmateOpenOrders) throws CoinmateException {
    List<LimitOrder> ordersList = new ArrayList<>(coinmateOpenOrders.getData().size());
    for (CoinmateOpenOrdersEntry entry : coinmateOpenOrders.getData()) {
        Order.OrderType orderType;
        // TODO
        if ("BUY".equals(entry.getType())) {
            orderType = Order.OrderType.BID;
        } else if ("SELL".equals(entry.getType())) {
            orderType = Order.OrderType.ASK;
        } else {
            throw new CoinmateException("Unknown order type");
        }
        LimitOrder limitOrder = new LimitOrder(orderType, entry.getAmount(), CoinmateUtils.getPair(entry.getCurrencyPair()), Long.toString(entry.getId()), new Date(entry.getTimestamp()), entry.getPrice());
        ordersList.add(limitOrder);
    }
    return ordersList;
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) CoinmateOrder(org.knowm.xchange.coinmate.dto.trade.CoinmateOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) ArrayList(java.util.ArrayList) CoinmateOpenOrdersEntry(org.knowm.xchange.coinmate.dto.trade.CoinmateOpenOrdersEntry) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) Date(java.util.Date)

Example 18 with Order

use of org.knowm.xchange.dto.Order in project XChange by knowm.

the class CoinmateAdapters method adaptOrder.

/**
 * Adapt a single order.
 *
 * @param coinmateOrder The raw order
 * @param orderByIdFetcher function to fetch order by id - needed to fetch market orders generated
 *     by stop-loss orders.
 * @return
 */
public static Order adaptOrder(CoinmateOrder coinmateOrder, Function<String, CoinmateOrder> orderByIdFetcher) throws CoinmateException {
    Order.OrderType orderType;
    if ("BUY".equals(coinmateOrder.getType())) {
        orderType = Order.OrderType.BID;
    } else if ("SELL".equals(coinmateOrder.getType())) {
        orderType = Order.OrderType.ASK;
    } else {
        throw new CoinmateException("Unknown order type");
    }
    Order.OrderStatus orderStatus;
    if ("CANCELLED".equals(coinmateOrder.getStatus())) {
        if (coinmateOrder.getStopLossOrderId() != null) {
            // this is a stopped order
            orderStatus = Order.OrderStatus.STOPPED;
        } else {
            orderStatus = Order.OrderStatus.CANCELED;
        }
    } else if ("FILLED".equals(coinmateOrder.getStatus())) {
        orderStatus = Order.OrderStatus.FILLED;
    } else if ("PARTIALLY_FILLED".equals(coinmateOrder.getStatus())) {
        orderStatus = Order.OrderStatus.PARTIALLY_FILLED;
    } else if ("OPEN".equals(coinmateOrder.getStatus())) {
        orderStatus = Order.OrderStatus.NEW;
    } else {
        orderStatus = Order.OrderStatus.UNKNOWN;
    }
    BigDecimal originalAmount;
    BigDecimal remainingAmount;
    BigDecimal averagePrice;
    if (orderStatus == Order.OrderStatus.STOPPED) {
        // fetch generated market order and get its average price and amount
        CoinmateOrder marketOrderRaw = orderByIdFetcher.apply(coinmateOrder.getStopLossOrderId());
        if (marketOrderRaw == null) {
            throw new CoinmateException("Failed to fetch market order generated by stoploss order.");
        }
        averagePrice = marketOrderRaw.getAvgPrice();
        originalAmount = marketOrderRaw.getOriginalAmount();
        remainingAmount = marketOrderRaw.getRemainingAmount();
    } else {
        averagePrice = coinmateOrder.getAvgPrice();
        originalAmount = coinmateOrder.getOriginalAmount();
        remainingAmount = coinmateOrder.getRemainingAmount();
    }
    // TODO: we can probably use `orderTradeType` to distinguish between Market and Limit order
    Order order = new MarketOrder(orderType, originalAmount, null, Long.toString(coinmateOrder.getId()), new Date(coinmateOrder.getTimestamp()), averagePrice, getCumulativeAmount(originalAmount, remainingAmount), null, orderStatus, null);
    return order;
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) Order(org.knowm.xchange.dto.Order) CoinmateOrder(org.knowm.xchange.coinmate.dto.trade.CoinmateOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) CoinmateOrder(org.knowm.xchange.coinmate.dto.trade.CoinmateOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 19 with Order

use of org.knowm.xchange.dto.Order in project XChange by knowm.

the class CoinmateTradeService method getOrder.

@Override
public Collection<Order> getOrder(OrderQueryParams... orderQueryParams) throws IOException {
    ArrayList<Order> result = new ArrayList<>(orderQueryParams.length);
    for (OrderQueryParams orderQueryParam : orderQueryParams) {
        CoinmateOrders response = this.getCoinmateOrderById(orderQueryParam.getOrderId());
        Order order = CoinmateAdapters.adaptOrder(response.getData(), orderId -> {
            try {
                return this.getCoinmateOrderById(orderId).getData();
            } catch (IOException ex) {
                return null;
            }
        });
        result.add(order);
    }
    return result;
}
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) CoinmateOrders(org.knowm.xchange.coinmate.dto.trade.CoinmateOrders) IOException(java.io.IOException) OrderQueryParams(org.knowm.xchange.service.trade.params.orders.OrderQueryParams)

Example 20 with Order

use of org.knowm.xchange.dto.Order in project XChange by knowm.

the class GeminiAdaptersTest method testAdaptLimitOrderFilled.

@Test
public void testAdaptLimitOrderFilled() throws IOException {
    // Read in the JSON from the example resources
    InputStream is = GeminiAdaptersTest.class.getResourceAsStream("/org/knowm/xchange/gemini/v1/order/example-limit-order-status.json");
    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    GeminiOrderStatusResponse geminiOrderStatusResponse = mapper.readValue(is, GeminiOrderStatusResponse.class);
    Order order = GeminiAdapters.adaptOrder(geminiOrderStatusResponse);
    assertThat(order.getId()).isEqualTo("44375901");
    assertThat(order.getAveragePrice()).isEqualTo(new BigDecimal("400.00"));
    assertThat(order.getCumulativeAmount()).isEqualTo(new BigDecimal("3"));
    assertThat(order.getCurrencyPair()).isEqualTo(CurrencyPair.BTC_USD);
    assertThat(order.getStatus()).isEqualTo(Order.OrderStatus.FILLED);
    assertThat(LimitOrder.class.isAssignableFrom(order.getClass()));
}
Also used : Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) InputStream(java.io.InputStream) GeminiOrderStatusResponse(org.knowm.xchange.gemini.v1.dto.trade.GeminiOrderStatusResponse) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) GeminiWalletJSONTest(org.knowm.xchange.gemini.v1.dto.account.GeminiWalletJSONTest)

Aggregations

Order (org.knowm.xchange.dto.Order)102 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)84 MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)53 BigDecimal (java.math.BigDecimal)51 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)38 ArrayList (java.util.ArrayList)37 Test (org.junit.Test)34 Date (java.util.Date)32 StopOrder (org.knowm.xchange.dto.trade.StopOrder)30 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)23 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)22 OrderType (org.knowm.xchange.dto.Order.OrderType)19 InputStream (java.io.InputStream)16 List (java.util.List)15 ExchangeException (org.knowm.xchange.exceptions.ExchangeException)12 UserTrades (org.knowm.xchange.dto.trade.UserTrades)11 TradeService (org.knowm.xchange.service.trade.TradeService)11 IOException (java.io.IOException)10 Map (java.util.Map)10 CoinbaseProOrder (org.knowm.xchange.coinbasepro.dto.trade.CoinbaseProOrder)10