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